Java Tutorial: Write Once, Run Anywhere
Java Tutorial: Write Once, Run Anywhere
Java - General
Java
is:
Java - General
Java
How it works!
Compile-time Environment
Run-time Environment
Class
Loader
Bytecode
Verifier
Java
Source
(.java)
Java
Compiler
Java
Bytecodes
move locally
or through
network
Java
Interpreter
Just in
Time
Compiler
Runtime System
Java
Bytecode
(.class )
Operating System
Hardware
Java
Class
Libraries
Java
Virtual
machine
How it works!
Java - Security
Pointer
Object-Oriented
Java
supports OOD
Polymorphism
Inheritance
Encapsulation
Java
Java Advantages
Hello world
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Hello World! I am new to Java.");
}
Initialisation
Assignment
Flow of Control
Java executes one statement after the other
in the order they are written
Many Java statements are flow control
statements:
Alternation:
if, if else, switch
Looping:
for, while, do while
Escapes:
break, continue, return
Or, alternatively:
if ( x < 10 ) { x = 10; }
Relational Operators
==
!=
>=
<=
>
<
Equal (careful)
Not equal
Greater than or equal
Less than or equal
Greater than
Less than
If else
Nested if else
if ( myVal > 100 ) {
if ( remainderOn == true) {
myVal = mVal % 100;
}
else {
myVal = myVal / 100.0;
}
}
else
{
System.out.print(myVal is in range);
}
else if
A Warning
WRONG!
if( i == j )
if ( j == k )
System.out.print(
i equals k);
else
System.out.print(
i is not equal
to j);
CORRECT!
if( i == j ) {
if ( j == k )
System.out.print(
i equals k);
}
else
System.out.print(
i is not equal to
j);
// Correct!
Loop n times
for ( i = 0; i < n; n++ ) {
// this code body will execute n times
// ifrom 0 to n-1
}
Nested for:
for ( j = 0; j < 10; j++ ) {
for ( i = 0; i < 20; i++ ){
// this code body will execute 200 times
}
}
while loops
while(response == 1) {
System.out.print( ID = + userID[n]);
n++;
response = readInt( Enter );
}
do { } while loops
do {
System.out.print( ID = + userID[n] );
n++;
response = readInt( Enter );
}while (response == 1);
Break
Continue
Arrays
myArray
=
Declaring Arrays
int myArray[];
declares myArray to be an array of integers
myArray = new int[8];
sets up 8 integer-sized spaces in memory,
labelled myArray[0] to myArray[7]
int myArray[] = new int[8];
combines the two statements in one line
Assigning Values
Arrays of Objects
int [] arrayOfInts;
int arrayOfInts [];
equivalent
Encapsulation
Objects hide their
functions (methods) and
data (variables)
Inheritance
Each subclass inherits
all variables of its
superclass
manual
Polymorphism
draw()
car
Superclass
auto
matic
Subclasses
draw()
Methods
Method Signatures
Public/private
Using objects
Constructors
class Date {
long time;
Date( ) {
time = currentTime( );
}
Date( String date ) {
time = parseDate( date );
} ...
The line
plum = new Fruit();
Overloading
Can
By
Inheritance
class Animal {
float weight;
...
void eat( ) {
...
} ...
}
class Mammal extends Animal {
int heartRate; // inherits weight
...
void breathe( ) { ...
} // inherits eat( ) }
interface Driveable {
boolean startEngine( );
void stopEngine( );
float accelerate( float acc );
boolean turn( Direction dir ); }
class Automobile implements Driveable {
...
public boolean startEngine( ) {
if ( notTooCold )
engineRunning = true;
...
}
public void stopEngine( ) {
engineRunning = false;
}
public float accelerate( float acc )
{
...
}
public boolean turn( Direction dir )
{
...
}
...
}
Stream Manipulation
Example:
51
Filters
Once a stream (e.g., file) has been opened, we
can attach filters
Filters make reading/writing more efficient
Most popular filters:
For objects:
ObjectInputStream, ObjectOutputStream
53
54
Object serialization
Write objects to a file, instead of writing primitive
types.
Use the ObjectInputStream, ObjectOutputStream
classes, the same way that filters are used.
56
57
58