Professional Documents
Culture Documents
Java Handout 10
Java Handout 10
My y: 10
My color: dark grey
Things I can do:
move
report x
; My x: 5
report y
My y: 20
My color: black
Things I can do:
My x: 17
move
My y: 25
report x
report y
My color: light grey
Things I can do:
; move
report x
report y
Franz Marc, Rehe im Walde (II), 1913-14
<kostis@cs.ntua.gr>
<nickie@softlab.ntua.gr>
Java 2
Java
:
(object)
My x: 10
My y: 50
My color: black ..
(fields) Things I can do:
move
,
report x
(methods) report y
(instance)
(class)
Java 3 Java 4
Java Java
Java (strings)
(reference types) : String
"Hello there"
String,
Java 7 Java 8
int: +, -, *, /, %, +
String
Java
1+2*3 7
Java
15/7 2 "123"+"456" "123456"
15%7 1 "The answer is " + 42 "The answer is 42"
-(6*7) -42 "" + (1.0/3.0) "0.3333333333333333"
1 + "2" "12"
double: +, -, *, /, "1" + 2 + 3 "123"
2 + 2 + "2" "42"
Java
6.0*7.0 42.0
15.0/7.0 2.142857142857143
Java 9 Java 10
Boolean
Java 11 Java 12
, Rvalues Lvalues
(side effect) a = b: a b
,
a = 1 , 1 = a ;
ML (pure) = :
.. a, 1, a+1, f() ( void), ..
Java =
: .. a d[2], 1 a+1
rvalue lvalue
Java 13 Java 14
Java
Java Java
a = a+b a += b x = y y
a = a-b a -= b x
a = a*b a *= b
Java
a+(x=b)+c a, b c x,
b
Java Java (a=d)+(b=d)+(c=d) d a, b
a = a+1 a++ c,
d
a = a-1 a--
a=b=c c a
b, c
Java 15 Java 16
(instance method)
Java
s.length() String s
s.equals(r) true s r
Java false
a++ a a r.equals(s)
++a a a
a-- a a r.toUpperCase() String
String r
--a a a
r.charAt(3) 3
String r (,
)
r.toUpperCase().charAt(3) 3
String r
Java 17 Java 18
(class methods) :
<method-call> ::= <reference-expression>.<method-name>
(<parameter-list>)
:
<method-call> ::= <class-name>.<method-name>
(<parameter-list>)
Java
String.valueOf(1==2) "false" ,
:
String.valueOf(6*7) "42"
<method-call> ::= <method-name>(<parameter-list>)
String.valueOf(1.0/3.0) "0.3333333333333333"
Java 19 Java 20
new
<creation-expression> ::= new <class-name>
(<parameter-list>)
(garbage collection)
(constructor)
Java
new String() String
new String(s) String
String s
new String(chars) String
chars : , --;
Java 21 Java 22
Java
, char int
( )
15 int double
: .. *
+
: .. <
!= Java
'a'+'b' 195
1/3 0
null
String 1/3.0 0.3333333333333333
( ) 1/2+0.0 0.0
1/(2+0.0) 0.5
Java 23 Java 24
(expression statements) (compound statements)
Java 25 Java 26
if
while (a != b) a b
if (a < b) b -= a; ,
statement else a -= b; . ( .)
while (time > 0) { time
simulate(); ,
time--; simulate
}
( time.
, ) while (true) work(); work
, .
while Java do for loops
Java 30
Java 29
return
return
(
void)
return
Java 32
Java 31
: ConsCell
/**
/** * Accessor for the head of this ConsCell.
* A ConsCell is an element in a linked list of * @return the int contents of this cell
* ints. */
*/ public int getHead() {
public class ConsCell { return head;
private int head; // the first item in the list }
private ConsCell tail; // rest of the list, or null
/**
/** * Accessor for the tail of this ConsCell.
* Construct a new ConsCell given its head and tail. * @return the next ConsCell in the list, or null
* @param h the int contents of this cell */
* @param t the next ConsCell in the list, or null public ConsCell getTail() {
*/ return tail;
public ConsCell(int h, ConsCell t) { }
head = h; }
tail = t;
}
Java 34
Java 33
/**
ConsCell * An IntList is a list of ints.
*/
public class IntList {
private ConsCell start; // list head, or null
cons ML
/**
Java : * Construct a new IntList given its first ConsCell.
* @param s the first ConsCell in the list, or null
ML :: , - */
public IntList(ConsCell s) {
Java start = s;
ConsCell }
/**
* Get our length. ML: val a = nil;
* @return our int length
*/
val b = 2::a;
public int length() { val c = 1::b;
int len = 0; val x = (length a) + (length b) + (length c);
ConsCell cell = start;
while (cell != null) { // while not at end of list
len++;
cell = cell.getTail(); Java: IntList a = new IntList(null);
} IntList b = a.cons(2);
return len; IntList c = b.cons(1);
} int x = a.length() + b.length() + c.length();
}
Java 37
Java 38
(reference) C C++,
:
public IntList(ConsCell s) { (pointer)
start = s;
} ,
IntList Java , ,
start
( )
Java 39 Java 40
, Java C++
Java 41 Java 42
C++ Java
C++ Java
IntList* p; IntList p;
p = new IntList(0);
p->length();
p = new IntList(null);
p.length(); Java
p = q; p = q;
IntList p(0);
p.length();
p = q;
Java 44
Java 43
IntList
: System.out /**
* Print ourself to System.out.
: */
public void print() {
print(x) x, System.out.print("[");
println(x) x ConsCell a = start;
while (a != null) {
System.out.print(a.getHead());
a = a.getTail();
if (a != null) System.out.print(",");
}
System.out.println("]");
}
Java 45 Java 46
main Driver
main :
class Driver {
public static void main(String[] args) { public static void main(String[] args) {
IntList a = new IntList(null);
} IntList b = a.cons(2);
IntList c = b.cons(1);
int x = a.length() + b.length() + c.length();
a.print();
b.print();
c.print();
static System.out.println(x);
}
(class method). ! }
Java 47 Java 48
, :
ConsCell.java, IntList.java Driver.java
( = + .java)
javac
javac Driver.java
compiler .class
Java launcher ( java)
main .class
Java 49