Java Tutorial: Write Once, Run Anywhere
Java Tutorial: Write Once, Run Anywhere
Class
Loader Java
Class
Bytecode Libraries
Java Verifier
Source
(.java)
Just in
Java Java
Time
Bytecodes Interpreter Java
Compiler
move locally Virtual
or through machine
Java network
Compiler
Runtime System
Java
Bytecod Operating System
e
(.class )
Hardware
How it works…!
• Java is independent only for one reason:
• Only depends on the Java Virtual Machine (JVM),
• code is compiled to bytecode, which is
interpreted by the resident JVM,
• JIT (just in time) compilers attempt to increase
speed.
Java - Security
• Pointer denial - reduces chances of virulent programs
corrupting host & buffer overflow attacks
• Applets even more restricted -
• May not
• run local executable,
• Read or write to local file system,
• Communicate with any server other than the originating server.
Object-Oriented
• Java supports
• Polymorphism
• Inheritance
• Encapsulation
• Java programs contain nothing but definitions and
instantiations of classes
• Everything is encapsulated in a class!
Java Advantages
• Portable - Write Once, Run Anywhere
• Security has been well thought through
• Robust memory management
• Designed for network programming
• Multi-threaded (multiple simultaneous tasks)
• Dynamic & extensible (loads of libraries)
• Classes stored in separate files
• Loaded only when needed
Basic Java Syntax
Primitive Types and
Variables
• boolean, char, byte, short, int, long, float, double etc.
• These basic (or primitive) types are the only types that are
not objects (due to performance issues).
• This means that you don’t use the new operator to create
a primitive variable.
• Declaring primitive variables:
float initVal;
int retVal, index = 2;
double gamma = 1.2, brightness
boolean valueOk = false;
Initialisation
• If no value is assigned prior to use, then the
compiler will give an error
• Java sets primitive variables to zero or false in
the case of a boolean variable
• All object references are initially set to null
• An array of anything is an object
• Set to null on declaration
• Elements to zero false or null on creation
Declarations
int index = 1.2; // compiler error
boolean retOk = 1; // compiler error
double fiveFourths = 5 / 4; // no error!
float ratio = 5.8f; // correct
double fiveFourths = 5.0 / 4.0; // correct
== Equal (careful)
!= Not equal
>= Greater than or equal
<= Less than or equal
> Greater than
< Less than
If… else
• The if … else statement evaluates an expression and
performs one action if that evaluation is true or a
different action if it is false.
if (x != oldx) {
System.out.print(“x was changed”);
}
else {
System.out.print(“x is unchanged”);
}
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
• Useful for choosing between alternatives:
if ( n == 1 ) {
// execute code block #1
}
else if ( j == 2 ) {
// execute code block #2
}
else {
// if all previous tests have failed, execute
code block #3
}
A Warning…
WRONG! CORRECT!
if( i == j ) if( i == j ) {
if ( j == k )
if ( j == k )
System.out.print(
System.out.print( “i equals k”);
“i equals }
k”); else
else
System.out.print( System.out.print(“
i is not equal to
“i is not equal
j”); //
to j”);
Correct!
The switch Statement
switch ( n ) {
case 1:
// execute code block #1
break;
case 2:
// execute code block #2
break;
default:
// if all previous tests fail then
//execute code block #4
break;
}
The for loop
• 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 {
System.out.print( “ID =” + userID[n] );
n++;
response = readInt( “Enter ” );
}while (response == 1);
draw() draw()
Simple Class and Method
Class Fruit{
int grams;
int cals_per_gram;
int total_calories() {
return(grams*cals_per_gram);
}
}
Methods
• A method is a named sequence of code that can be
invoked by other Java code.
• A method takes some parameters, performs some
computations and then optionally returns a value (or
object).
• Methods can be used as part of an expression
statement.