Objectives Key Features of Java Technology Define class and application Writing a simple Java Application Java Virtual Machine’s function Garbage Collection Code Security
Objectives
Key Features of Java Technology Define class and application Writing a simple Java Application Java Virtual Machine’s function Garbage Collection Code Security
What is the Java Technology?
A programming Language A Development environment An Application environment A deployment environment
Primary Goals of Java Technology
Provides an easy to use language by Avoiding the pitfalls of other languages such as
pointers, memory management etc Object Oriented Enable users to create streamlined and clear code Provides an interpreted environment
Speed of developmentCode Portability
Primary Goals (contd..)
Enables users to run more than one thread Loads classes Dynamically Furnishes better security
The following features fulfill these goals The Java Virtual Machine Garbage Collection Code Security
// TestGreeting.java
// Sample "Hello World" Application
public class TestGreeting{
public static void main(String[] args) {
Greeting hello = new Greeting("Hello");
hello.greet("World");
}
}
A Basic Java Application
A Basic Java Application-Contd..
//Greeting.java
public class Greeting{
private String salutation;
Greeting(String s){
salutation = s;
}
public void greet(String whom){
System.out.println(salutation+” “+whom);
}
}
Compiling and Running
Compiling TestGreeting.javajavac TestGreeting.java
Greeting.java is compiled automatically Running an application
Java TestGeeting
The Java Virtual Machine
Provides hardware platform specifications Reads compiled byte codes that are platform
independent Is implemented as software or hardware Is implemented in a Java technology
development tool or a web browser
JVM
Unix DOS JavaOS
TestGreeting.java Greeting.java
TestGreeting.class Greeting.class
javacAlso compiles
JVM JVM
Can run on multiple platforms
java
Com
pil
eR
un
tim
e
Garbage Collection
Java Provides a system level thread to track memory allocation
Garbage Collection Checks for and frees memory no longer needed Is done automatically Can vary across JVM implementations
Code Security
TestGreeting.java
TestGreeting.class
javac
Class Loader
Byte code verifier
Interpreter
Runtime
JIT Code
Generator
Hardware
Network
Network
Compile Runtime
Java Runtime Environment
JVM Performs 3 main tasks Loads Code – Performed by class loader Verifies Code – Performed by bytecode verifier Executes Code – Performed by runtime
interpreter
Class Loader
Loads all classes necessary for the execution of a program
Maintain classes of the local file system in separate “namespace”
Prevents spoofing
Bytecode Verifier
Ensures that The code adheres to the JVM specification The code does not violate system integrity The code causes no operand stack overflows or
underflows The parameter types for al operational code are
correct No illegal data conversions have occurred
Object Oriented Programming
Objectives
Define modeling concepts Abstraction, encapsulation and Packages
Define class, member, attribute, method, constructor and package
Use access modifiers private and public
Invoke a method on a particular Object
Abstraction
Functions – Write an algorithm once to be used in many situations
Objects – Group a related set of attributes and behaviors into a class
Frameworks and API’s – Large group of objects that support a complex activity
Classes as Blueprints for Objects
A blueprint is a description from which many physical devices are constructed
A class is a description of an object A class describes the data each object includes A class describes the behaviors that each object
exhibits
Classes support 3 key features of OOP•Encapsulation •Inheritance •Polymorphism
Declaring Java Classes
Examplepublic class Vehicle{
private double maxLoad;
public void setMaxLoad(double val)
{
maxLoad=val;
}
}
Declaring Attributes
Examplepublic class Foo{
public int x;
private float y = 1000.0F;
private String name=“Fred Smith”;
}
Declaring Methods
Examplespublic class Thing{private int x;public int getX(){
return x;}public void setX(int new_x){
x=new_x;}
}
Accessing Object Members
The “dot” notation <object>.<member> is used to access object members including attributes and methods
Examplething1.setX(47);
thing1.x=47;
Information Hiding
The Problem
MyDate
+day : int+month : int+year : int
Client code has direct access to internal dataMyDate d = new MyDate();
d.day=32; //invalid day
d.month=2;d.day=30;//wrong
d.day=d.day+1; //no check
Information Hiding The Solution:
MyDate
-day: int-month: int-year: int+getDay(): int +getMonth(): int+getYear(): int+setDay(d: int): boolean+setMonth(m: int)+setYear(y: int)-validDay(d: int): boolean
Client code must use setters / getters to create internal data
MyDate d = new MyDate();
d.setDay(32);
//invalid returns false
d.setMonth(2);d.setDay(30);
//setDay returns false
d.setDay(d.getDay()+1);
//will return false if wrap occurs
Encapsulation
Hides implementation details
Forces the user to use an interface to access data
Makes the code more maintainable
MyDate
-date: long
+getDay(): int +getMonth(): int+getYear(): int+setDay(d: int)+setMonth(m: int)+setYear(y: int)-validDay(d: int): boolean
Declaring Constructors
Examplepublic class Thing{
private int x;
public Thing(){
x=47;
}
public Thing(int new_x){
x=new_x;
}
}
The Default Constructor
There is always at least one constructor in every class
If no constructors are provided a default constructor will be present The default constructor takes no arguments The default constructor has no body
Enables you to create object instances without having to write a constructor
Source File Layout
Examplepackage shipping.reports.web;import shipping.domain.*;import java.util.List;import java.io.*;public class VehicleCapacityRepport{private List vehicles;public void generateReport(Writer op){
---}
}
Packages
Packages help manage large Software Systems Packages can contain classes and sub-packages
shipping
GUI
reports
domain
Company Vehicle
Truck RiverBarge
The package Statement
Examplepackage shipping.reports.web;
Specify the package declaration at the beginning of the source file
Only one package declaration per source file If no package declaration is declared, the class
belongs to the default package Package names must be hierarchical and separated
by dots
The import Statement
Precedes all class declarations Tells the compiler where to find classes to
use Examples
import java.io.Writer; //Specify a class to access
import java.io.*; //all classes in the package
Directory Layout and Packages
Packages are stored in the Directory tree containing the package name
Example
shipping/domain/
Company.classVehicle.classRiverBarge.classTruck.class
GUI/reports/
VehicleCapacityReport.class
Terminology Recap
Class – The source-code blueprint for a run-time object Object – An instance of a class Attribute – A data element of an object
AKA: data member, instance variable, data field Method – A behavioral element of an object
AKA: algorithm, function, procedure Constructor – A method-like construct used to initialize
an object Package – A group of classes and/or sub-packages