Académique Documents
Professionnel Documents
Culture Documents
Java Review
08/03/2007
Java
Bytecode
Java interpreter
translates bytecode to
machine code in JRE
javac Hello.java
Java compiler
Hello.java
ello
H
java
JRE for
Linux
Hello.class
jav
aH
ello
JRE for
Windows
08/03/2007
Methods in Java
The main method has a specific signature.
Example: Hello world! Program in Java
public class Hello
{
public static void main(String args[])
{
System.out.println(Hello world!);
}
Notice no semi-colon at the end!
}
08/03/2007
08/03/2007
Data Types
FEO3
int
Date ref
08/03/2007
FEO3
Date
obj
Arrays
arrayRef
DFO7
int [ ] ref
DFO7
0
0 Array
5 obj
0
0
All arrays have a length property that gives you the number
of elements in the array.
08/03/2007
Arrays (cont.)
pArray
CDO8
Point [ ]
ref
08/03/2007
CDO8
null
null Array
AB12 obj
null
null
AB12
Point
obj
Multidimensional Arrays
twodim2
int [ ] [ ]
ref
Array
obj of
array
refs
Each element is
an int [] ref
08/03/2007
Array
obj
Array
obj
3 4
Array
obj
5 6
2 3
7 8
10
Packages
java.lang, java.util, proj1
Constants
PI, MAX_NUMBER
08/03/2007
11
Comments
C style
C++ style
Javadoc
/* multi-liner comments */
// one liner comments
/**
This is an example of a javadoc comment. These
comments can be converted to part of the pages you
see in the API.
*/
08/03/2007
12
Access Control
Modifier
Same class
Same
package
Subclass
Universe
private
default
protected
public
08/03/2007
13
08/03/2007
14
Classes
String toString()
boolean equals(Object o)
08/03/2007
15
Inheritance in Java
08/03/2007
16
Polymorphism
08/03/2007
17
Polymorphism (cont.)
08/03/2007
18
name;
abstract void type();
String toString(){ return name;}
Node(String name){
this.name = name;
}
}
08/03/2007
19
08/03/2007
20
Inner Classes
08/03/2007
21
22
Interfaces
08/03/2007
23
Interface Example
08/03/2007
24
Interfaces (cont.)
08/03/2007
25
08/03/2007
26
Generics
08/03/2007
27
08/03/2007
After:
List<Integer> c = new ArrayList<Integer>();
c.add(new Integer(34));
Integer i = c.get(0);
28
Implementing Generic
Classes
08/03/2007
29
30
Donts of Generic
Programming
08/03/2007
31
Dos of Generic
Programming
Angular brackets are not used if the type parameter is the type for a
parameter of the constructor.
public Cell3(T prisoner );
08/03/2007
32
08/03/2007
33
Natural Order
08/03/2007
34
The Comparable<T>
Interface
The Comparable<T> interface defines just one method
to define the natural order of objects of type T
compareTo returns
08/03/2007
35
Comparable Example
import java.util.*;
public class Fraction implements Comparable<Fraction>
{
private int n;
private int d;
public Fraction(int n, int d){ this.n = n; this.d = d;}
public int compareTo(Fraction f)
{
Casting required
double d1 = (double) n/d;
double d2 = (double)f.n/f.d;
for floating point
if (d1 == d2)
division
return 0;
else if (d1 < d2)
return -1;
return 1;
}
public String toString() { return n + / + d; }
}
08/03/2007
36
Sort Example
public class FractionTest
{
public static void main(String []args)
{
Fraction [] array = {new Fraction(2,3),
new Fraction (4,5), new Fraction(1,6);
Arrays.sort(array);
for(Fraction f :array)
System.out.println(f);
}
}
08/03/2007
37
08/03/2007
38
08/03/2007
39
More Bounding
08/03/2007
40
Generic Sorting
public class Sort
{
public static <T extends Comparable<T>>
void bubbleSort(T[] a)
{
for (int i = 0; i< a.length - 1; i++)
for (int j = 0; j < a.length -1 - i; j++)
if (a[j+1].compareTo(a[j]) < 0)
{
T tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
08/03/2007
41
08/03/2007
42
43