12 Java
12 Java
Computer
system:
Java vs. C
¢ Reconnecting to Java
§ Back to CSE143!
§ But now you know a lot more about what really happens when we
execute programs
Data in Java
¢ Integers, floats, doubles, pointers – same as C
§ Yes, Java has pointers – they are called ‘references’ – however, Java
references are much more constrained than C’s general pointers
¢ Null is typically represented as 0
¢ Characters and strings
¢ Arrays
¢ Objects
Data in Java
¢ Arrays
§ Every element initialized to 0 or null
§ Length specified in immutable field at start of array (int – 4 bytes)
§ array.length returns value of this field
§ Since it has this info, what can it do?
int array[5]:
C ?? ?? ?? ?? ??
0 4 20 24
Java 5 00 00 00 00 00
Data in Java
¢ Arrays
§ Every element initialized to 0 or null
§ Length specified in immutable field at start of array (int – 4 bytes)
§array.length returns value of this field
§ Every access triggers a bounds-check
§ Code is added to ensure the index is within bounds
§ Exception if out-of-bounds
Bounds-checking sounds slow, but:
int array[5]: 1. Length is likely in cache.
2. Compiler may store length in register
C ?? ?? ?? ?? ?? for loops.
3. Compiler may prove that some checks
0 4 20 24 are redundant.
Java 5 00 00 00 00 00
Data in Java
¢ Characters and strings
§ Two-byte Unicode instead of ASCII
§ Represents most of the world’s alphabets
§ String not bounded by a ‘\0’ (null character)
§ Bounded by hidden length field at beginning of string
C: ASCII 43 53 45 33 35 31 \0
0 1 4 7 16
Java: Unicode 6 00 43 00 53 00 45 00 33 00 35 00 31
i a p i a p
3 int[3]
0 4 16 20 0 4 8 12
Autumn 2013 Java vs. C 0 4 16 8
University of Washington
Pointers/References
¢ Pointers in C can point to any memory address
¢ References in Java can only point to [the starts of] objects
§ And can only be dereferenced to access a field or element of that object
s n p s n p
0 4 8 12 x
Autumn 2013 Java vs. C 11
University of Washington
Java objects
class Point { fields
double x;
double y;
Point() { constructor
x = 0;
y = 0;
}
method
boolean samePlace(Point p) {
return (x == p.x) && (y == p.y);
}
}
… creation
Point p = new Point();
…
Java objects
Point object
p
vtable pointer
header x y
Java Methods
¢ Static methods are just like functions.
¢ Instance methods
§ can refer to this;
§ have an implicit first parameter for this; and
§ can be overridden in subclasses.
¢The code to run when calling an instance method (e.g.,
p.samePlace(q)) is chosen at run-time by lookup in the vtable.
Java: C pseudo-translation:
Point p = new Point(); Point* p = calloc(1,sizeof(Point));
p->header = ...;
p->vtable = &Point_vtable;
p->vtable[0](p);
Method dispatch
Point object
p
vtable pointer
header x y
Java: C pseudo-translation:
Point p = new Point(); Point* p = calloc(1,sizeof(Point));
p->header = ...;
p->vtable = &Point_vtable;
p->vtable[0](p);
Subclassing
class PtSubClass extends Point{
int aNewField;
boolean samePlace(Point p2) {
return false;
}
void sayHi() {
System.out.println("hello");
}
}
Subclassing
class PtSubClass extends Point{
int aNewField;
boolean samePlace(Point p2) {
return false;
}
void sayHi() {
System.out.println("hello");
}
} aNewField tacked on at end
vtable x y aNewField
Dynamic dispatch
Point object
vtable pointer
header x y
Point vtable
Java: C pseudo-translation:
Point p = ???; // works regardless of what p is
return p.samePlace(q); return p->vtable[1](p, q);
Autumn 2013 Java vs. C 19
University of Washington
Agenda
¢ Inside
¢ HW4 grades/feedback are up
¢ Lab 5 due tonight! Go, go, go!
§ If I’m not in the office today, I might be in the basement labs
¢ Tomorrow: Review Session
§ bring your own questions
¢ Final exam topics/materials
§ See past exams (website)
§ See topic manifest (website: last Friday’s slides)
¢ Today
§ Finish up Java
§ Brief tour of Parallel Processing
§ 351 Conclusions :(
Autumn 2013 Java vs. C 20
University of Washington
Java
Lisp
Interpreted
Bytecode Ahead-of-time
compiler compiler
compile time
Virtual Machine Language
run time
Java bytecode
¢ like assembly code for JVM, Holds pointer ‘this’
operand stack
constant
pool
bytecode: iload 1
iload 2
//
//
push 1st argument from table onto stack
push 2nd argument from table onto stack
iadd // pop top 2 elements from stack, add together, and
// push result back onto stack
istore 3 // pop result and put it into third slot in table
http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings
Autumn 2013 Java vs. C 26
University of Washington
Java Bytecode }
public int getEmployeeNumber();
Method Employee(java.lang.String,int)
0 aload_0
1 invokespecial #3 <Method java.lang.Object()>
4 aload_0
5 aload_1
6 putfield #5 <Field java.lang.String name>
9 aload_0
10 iload_2
11 putfield #4 <Field int idNumber>
javac Employee.java 14 aload_0
javap -c Employee 15 aload_1
16 iload_2
17 invokespecial #6 <Method void
storeData(java.lang.String, int)>
20 return