Java TM Introduction Renaud Florquin Isabelle Leclercq FloConsult SPRL http://www.floconsult.be mailto:info@floconsult.be
Java Technical Virtues Write once, run anywhere Get started quickly Write less code Write better code Develop programs more quickly Avoid platform dependencies with 100% pure Java Distribute software more easily 2
Java: History (1) J2ME 1.2 Java 2 Platform... oak Java 1.0 J2EE Java 1.2 J2SE (Java) 1.2 J2SE 1.3 J2EE 1.3 J2SE 1.4 J2EE 1.4 1996 1997 1998 1999 2000 2001 2002 2003 2004 3
Java: Application production Compile AND interpret Compiler makes Java Bytecodes (*.class) Interpreter (JVM) executes Bytecodes myprogram.java class myprogram {... } javac java Java Virtual Machine myprogram.class 03 3b 84 00 01 1a 05 68 3b a7 ff f9 istore_0 iinc 0, 1 iload_0 iconst_2 imul istore_0 goto -7 OS 4
Java Virtual Machine Interprets Java Byte Code myprogram.class 03 3b 84 00 01 1a 05 68 3b a7 ff f9 Class loader Class verifier JIT istore_0 iinc 0, 1 iload_0 iconst_2 imul istore_0 goto -7 Primordial Class loader Heap Execution Engine Native Method Loader Native Method Area Security Manager OS 5
Compilation phases myprogram.java class myprogram {... } tokens Parser Syntax tree Scanner Constrainer Code Generator Compiler Symbol Table position int...... myprogram.class 03 3b 84 00 01 1a 05 68 3b a7 ff f9 istore_0 iinc 0, 1 iload_0 iconst_2 imul istore_0 goto -7 6
Compilation phases selection-statement if (logical-expression) statement expression ; if ( logical-expression ) expression-statement Parser assignment-expression ; a <= 10 val = a Scanner if ( a <= 10 ) val = a ; i f ( a < = 1 0 ) v a l = a ; 7
Types Java programming language is a strongly typed language Everything that has a value also has a type. In Java there are two kinds of types: Primitive Types (Built-in data types). Reference Types (Everything else, including objects) 8
Primitive Types Primitive boolean char byte Description Either true or false 16-bit Unicode character ('\u0000'.. '\uffff') 8-bit signed two's complement integer [-2 7.. +2 7-1] Wrapper class Boolean Character Byte short int long float double 16-bit signed two's complement integer[-2 15.. +2 15-1] 32-bit signed two's complement integer[-2 31.. +2 31-1] 64-bit signed two's complement integer[-2 63.. +2 63-1] 32-bit IEEE 754-1985 floating-point number 64-bit IEEE 754-1985 floating-point number Short Integer Long Float Double 9
Reference Types Everything that is not a primitive type is a reference type Three kinds of reference types: Classes String java.lang.integer Interfaces Serializable Arrays int[] String[] 10
Array of What? You can have an array of anything, but all the elements of an array are of the same type. any primitive type. any reference type. You need to declare the type of an array (the type of each element). You also need to explicitly create an array just declaring the type is not enough. 11
Creating an Array Use the new operator: foo = new int[4]; We can combine the declaration and creation int[] foo = new int[4]; foo foo[0] foo[1] foo[2] foo[3] 12
Creating an Array Reference type: names = new String[4]; // or String[] names = new String[4]; How many objects are created? names names[0] names[1] names[2] names[3] null null null null 13
Array Length The length of an array is available as a field of the array. byte[] buffer = new byte[100]; for (int i=0; i < buffer.length; i++) { } buffer[i] = i; 14
Array Initializers You can use an array literal to create an array you assign the literal to the array variable. Example: Array Initialializers int[] foo = {4, 3, 2, 1, 0}; for (int i =0; i < foo.length; i++) { System.out.println("Foo[" + i + "]=" + foo[i]); } int[][] pascalstriangle = { { 1 }, { 1, 1}, { 1, 2, 1}, { 1, 3, 3, 1}, }; 15
Initial Values of Variables For the class variables and instance variables: boolean char byte, short, int, long float false '\u0000' Object reference null Parameters Provided by the invoker 0 +0.0f double +0.0 Local Variable Must be explicit (verified by the compiler) 16
Variables public static final int maxnumber = 42 ; modifiers type identifier initializer public void foo(final int maxnumber ) {...} 17
Assignment Expressions General form lvalue = expression Overwrites the current value of an object (lvalue, l standing for left) with a new value (expression) The type of the expression must match the type of the object (caution: implicit conversion) The type and the value of an Assignment expression are the type and the value of the lvalue 18
Assignment Expressions Compound assignment int i = 0; lvalue op= expression Equivalent to: lvalue = lvalue op expression Example: i = i + 3; i += 2; // shortcut assignment a = b = 2; 19
Widening Implicit conversions double 64 bits float 32 bits long 64 bits int 32 bits short 16 bits byte 8 bits Incompatible char 16 bits No conversion for booleans 20
Conversion during numeric ops For unary operators: ++, --: no conversion +, -, ^: byte, short, char promoted to int For binary operators: If one operand double: other promoted double before operation Else, if one is float: other promoted float before operation Else, if one is long: other promoted long before operation Else, both promoted int before operation 21
Object ref. implicit conversion Super interface Super class Interface Sub class Interface Class 22
Conversion to String All objects may be converted to a String Implicit call of tostring() e.g. String s = + 2 s = 2, type String 23
Expression evaluation Operator Precedence []. () expr++ expr-- st priority ++expr --expr +expr -expr ~! new * / %... = +=... st priority 3 + 4/2 * 10 =? (3 + 4/2) * 10 =? 24
Compound Statements Example 1 2 3 4 public class Test { public void amethod() { int j = 2; // j is defined for level 2-3-4 { int i = 0; // i is defined for level 3-4 while (i < 10) { System.out.println("Value: " + i*j); i++; } } } } 25
Class Definition Class Members Fields (= attributes) Methods Nested classes and interfaces Class Modifiers public: publicly accessible otherwise only accessible within its own package abstract: incomplete class (no instance) final: cannot be subclassed Window + position: Point = (0,0) - visible: boolean + display() + create(position: Point) 26
Access control (2) private Default (package) protected public 27
Modifiers Modifiers Class Package Subclass private No specifier protected public World static final transient Volatile Class variable (shared by every instance) The variable's value cannot be changed The variable is not part of the persistent state (should not be serialized). The variable can be changed asynchronously. 28
static Fields One instance of a field shared by all objects of a class (class variable) How to access a static field? Via the class name Via a reference to an object Example public class Window { public static nbrwindow;... } System.out.println(Window.nbrWindow); Window w = new Window(); System.out.println(w.nbrWindow); 29
final Fields Immutable property of a class (static+final) Initialization Via Initializer Via static initialization block (see further) Immutable property of an object (final). Initialization Via Initializer Constructor Via initialization block (see further) 30
Inheritance Example (1) Example class Node { private final String name; private Object value = null; public Node(String name, Object value) {...} public String getname() { return name; }... } class ColorNode extends Node { private Color c; public ColorNode(String name, Object value, Color c) { super(name, value); } this.c = c; } public Color getcolor() { return c; }... Inheritance Superclass constructor invocation 31
Inheritance Example (2) ColorNode adds attributes and methods to Node: Node + name:string + value : Object + getname(): String ColorNode ColorNode + c: Color + getcolor(): Color + name:string + value : Object + c: Color + getname(): String + getcolor(): Color 32
No multiple inheritance No multiple inheritance of classes in Java But: multiple inheritance of interfaces allowed <<Interface>> 33
Interface Keywords summary <<interface>> Window + position: Point = (0,0) Visibility: public (implicit), (package) Others: abstract (implicit) Visibility: public (implicit) Others: static (implicit), final (implicit) + display() + create(position: Point) Visibility: public (implicit) Others: abstract (implicit) 34
Inner Classes Example public class Stack { private Vector items;... public Iterator getiterator() { return new StackIterator(); } class StackIterator implements Iterator { int currentitem = items.size() - 1; public boolean hasnext() {...}... } } astack:stack :StackIterator Iterator 35
Bibliography The Java Program m ing Language 3 rd Edition by Ken Arnold, James Gosling, David Holmes; 705 p, June 2000, Addison-Wesley, ISBN 0-20170-433-1 Java(TM) Language Specification 2 d Edition by Bill Joy, Guy Steele, James Gosling, Gilad Bracha; 544 p, June 2000, Addison-Wesley, ISBN 0-20131-008-2 The Java Tutorial 3 d Edition by Mary Campione and Kathy Walrath; 580 p, January 2000, Addison-Wesley, ISBN 0-20170-393-9 The JDK 1.4 Tutorial by Gregory Travis; 408 p, March 2002, Manning publications Company, ISBN 1930110456 The Java(TM) Developers Alm anach 4 th Edition, volum e 1 by Patrick Chan; 1024 p, March 2002, Addison-Wesley, ISBN 0-20175-280-8 36