You are on page 1of 13

Java My x: 20

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

public class Point { char: 0..216-1, 'a', '\n', ,


private int x,y; field definitions
Unicode
private Color myColor;
byte: -27..27-1
public int currentX() {
return x; short: -215..215-1
}
int: -231..231-1,
public int currentY() { long: -263..263-1, L
return y;
} float: IEEE 32-bit standard, F

public void move(int newX, int newY) {


double: IEEE 64-bit standard,
x = newX; (.., 1.2, 1.2e-5, 1e3)
y = newY;
} boolean: true false
} : void null
method definitions
Java 5 Java 6

Java (strings)


(reference types) : String

My data: Hello there


Mylength: 11
, .. Point Things I cando:
report my length
(interface) report my ithchar
, .. Point[] int[] make anuppercase
version of
myself
etc.


"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

: <, <=, >=, && ||,


>, ( andalso orelse ML)
== != !, not ML
, double a?b:c, if a then b else c ML
( ML)
Java
Java 1 <= 2 && 2 <= 3 true
1 <= 2 true
1 < 2 || 1 > 2 true
1 == 2 false
1 < 2 ? 3 : 4 3
true != false true

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 method calls)

(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)

, <compound-statement> ::= { <statement-list> }


< statement-list> ::= <statement> <statement-list> | <empty>

Java
<expression-statement> ::= <expression> ;
, , {
a = 0; a,
Java b = 1; b.
}
, .. x == y
{
Java a++; a,
0 speed. b++; b,
speed = 0; c++; c.
a++; a 1. }

inTheRed = cost > balance; cost { } .
balance,
inTheRed true, false.

Java 25 Java 26

if

<declaration-statement> ::= <declaration> ; <if-statement> ::= if (<expression>) <statement>


<declaration> ::= <type> <variable-name> | if (<expression>) <statement> else <statement>
| <type> <variable-name> = <expression>
else

boolean done = false; done Java


boolean,
false. if (i > 0) i--; i,
Point p; p .
Point. ( .)
if (a < b) b -= a; a b
{ else a -= b; .
int temp = a;
a = b; a b. if (reset) { reset true,
b = temp; a = b = 0; a b
} reset = false; reset false.
}
Java 27 Java 28
while
Java
<while-statement> ::= while (<expression>) <statement>
while (a < 100) a += 5; a 100,
expression false 5 a.

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-statement> ::= return <expression>;


| 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 }

ML length . Java /**


* Cons the given element h onto us and return the
* resulting IntList.
* @param h the head int for the new list
, null * @return the IntList with head h, and us as tail
*/
public IntList cons (int h) {
return new IntList(new ConsCell(h,start));
}
Java 36
Java 35
IntList

/**
* 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 C++ C++



:
a->x x a
C C++
(.. a.x x a
) Java
Java ,
: :
a.x x a

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

You might also like