Advanced Programming in Java CM1: the Language
Advanced Programming in Java
CM1: the Language
Arthur Bit-Monnot
INSA 4IR
Arthur Bit-Monnot | INSA 4IR 1 / 47
Advanced Programming in Java CM1: the Language | Structure and Objectives of the Course
Section 1
Structure and Objectives of the Course
Arthur Bit-Monnot | INSA 4IR 2 / 47
Advanced Programming in Java CM1: the Language | Structure and Objectives of the Course
Course Objectives
Build skills to scale up the development process.
Objectives:
provide tools for software development at medium scale (small team of contributors)
boost general programming skills, based on the Java language
understand the life cycle of a Java program in the JVM (JIT, garbage collector, . . . )
Arthur Bit-Monnot | INSA 4IR 3 / 47
Advanced Programming in Java CM1: the Language | Structure and Objectives of the Course
Course Objectives
Build skills to scale up the development process.
Objectives:
provide tools for software development at medium scale (small team of contributors)
boost general programming skills, based on the Java language
understand the life cycle of a Java program in the JVM (JIT, garbage collector, . . . )
Taught through Java but mostly transferrable to other languages
Arthur Bit-Monnot | INSA 4IR 3 / 47
Advanced Programming in Java CM1: the Language | Structure and Objectives of the Course
Topics Covered
Build configuration
tests and automatization
dependency management
error handling / thread safety
reproducibility
code structure (package, public/private)
sources management
release and versioning (interface stability)
Arthur Bit-Monnot | INSA 4IR 4 / 47
Advanced Programming in Java CM1: the Language | Structure and Objectives of the Course
Context: Training Unit
2 phases:
1 independent training for each sub-area (java, UML, . . . )
2 A common project: the ChatSystem
Arthur Bit-Monnot | INSA 4IR 5 / 47
Advanced Programming in Java CM1: the Language | Structure and Objectives of the Course
Planning: Advanced Programming, Phase 1
Courses (3 CM)
The Java Language
The Java Virtual Machine
Development process
Labs (6 TD)
Threads / Concurrency
Networking (TCP / UDP)
Graphical User Interface (GUI)
Arthur Bit-Monnot | INSA 4IR 6 / 47
Advanced Programming in Java CM1: the Language | Structure and Objectives of the Course
Planning: Advanced Programming, Phase 2
Application Project: Decentralized ChatSystem
Contact discovery
Message exchange
History management
Graphical User Interface
Arthur Bit-Monnot | INSA 4IR 7 / 47
Advanced Programming in Java CM1: the Language | Structure and Objectives of the Course
Planning: Advanced Programming, Phase 2.1
Focus on Contact Discovery
Involves:
1 Conception of the module (3 TD UML)
2 Implementation of the module (4 TP Prog)
3 Project Management (2 TD)
=> Code Milestone
Arthur Bit-Monnot | INSA 4IR 8 / 47
Advanced Programming in Java CM1: the Language | Structure and Objectives of the Course
Planning: Advanced Programming, Phase 2.2
Building on your “Contact discovery” struggles
Present solutions to your problems in contact discovery:
build configuration
tests
logging
design patterns
thread safety
...
Format:
Code Repository + Pre-recorded live coding (2CM, autonomous)
Feedback / Evaluation formative (1 TD)
Arthur Bit-Monnot | INSA 4IR 9 / 47
Advanced Programming in Java CM1: the Language | Structure and Objectives of the Course
Planning: Advanced Programming, Phase 2.3
Full application
Involves:
1 Complete Conception (4 TD UML)
2 Implementation (6 TP Prog)
3 Project Management (2 TD)
=> Final Report + Code
Arthur Bit-Monnot | INSA 4IR 10 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Section 2
Java in the Realm of Programming Languages
Arthur Bit-Monnot | INSA 4IR 11 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Object-Oriented Programming
Programs are composed of datatypes and
code
in OOP, datatypes and code are public class Counter {
grouped into the same construct: a private int count = 0;
class
public void increase() {
[Link]++;
The instance of a class is called an object
}
an object has fields, defined in the class
(count)
public int get() {
an object has methods, defined in the
return [Link];
class (increase, get)
}
}
Methods have privileged access to the
object’s fields.
Arthur Bit-Monnot | INSA 4IR 12 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Quizz
public class Counter { What are the methods available for a
private int count = 0; Counter instance?
public void increase() {
[Link]++;
}
public int get() {
return [Link];
}
}
Arthur Bit-Monnot | INSA 4IR 13 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Quizz
public class Counter { What are the methods available for a
private int count = 0; Counter instance?
increase()
public void increase() { get()
[Link]++;
}
public int get() {
return [Link];
}
}
Arthur Bit-Monnot | INSA 4IR 13 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Quizz
public class Counter { What are the methods available for a
private int count = 0; Counter instance?
increase()
public void increase() { get()
[Link]++; clone()
} equals()
finalize()
public int get() { ...
return [Link];
} Inherited from the [Link]
} top-level class.
Arthur Bit-Monnot | INSA 4IR 13 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Inheritance the heart of OOP
All (decently recent) programming languages have mechanism to couple datatypes and code
BUT inheritance is the crucial feature that sets OOP apart.
Key benefits:
code sharing between related datatypes
specialization of high-level behavior
Many drawbacks: OOP is not a global optimal in the programming language design space
Arthur Bit-Monnot | INSA 4IR 14 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
At the top was [Link]
public class Counter {
Is implicitly rewritten to
public class Counter extends [Link] {
Define the common capabilities of all objects in a program.
> [Link] documentation
Arthur Bit-Monnot | INSA 4IR 15 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
At the top was [Link]
Printing: Synchronization (concurrency)
toString() notify() / notifyAll()
wait()
Identity:
equals() Runtime introspection:
hashCode() getClass()
Copy Lifecycle management
clone() finalize() (deprecated)
Arthur Bit-Monnot | INSA 4IR 16 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Building well-behaved objects: String representation
public class MyInt { MyInt a = new MyInt(1);
public final int n; MyInt b = new MyInt(2);
MyInt(int n) {
this.n = n; // without toString
} [Link](a) // MyInt@8efb846
[Link](b) // MyInt@2a84aee7
Arthur Bit-Monnot | INSA 4IR 17 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Building well-behaved objects: String representation
public class MyInt { MyInt a = new MyInt(1);
public final int n; MyInt b = new MyInt(2);
MyInt(int n) {
this.n = n; // without toString
} [Link](a) // MyInt@8efb846
[Link](b) // MyInt@2a84aee7
@Override
// with toString
public String toString() {
[Link](a) // 1
return [Link]();
[Link](b) // 2
}
Arthur Bit-Monnot | INSA 4IR 17 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Dynamic Dispatch for inheritance handling
public static void print(Object o) {
[Link]("object: " + [Link]());
}
print(new Object()); // object: Object@4efe845
print(new MyInt(3)); // object: 3
print("hello"); // object: hello
Arthur Bit-Monnot | INSA 4IR 18 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Dynamic Dispatch for inheritance handling
public static void print(Object o) {
[Link]("object: " + [Link]());
}
print(new Object()); // object: Object@4efe845
print(new MyInt(3)); // object: 3
print("hello"); // object: hello
The print function works for any object.
How can it select the right toString() method to call?
Arthur Bit-Monnot | INSA 4IR 18 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Runtime Reflection
var a = new MyInt(1);
var clazz = [Link]();
[Link](clazz); class test$MyInt
[Link]("Fields:");
for (var field : [Link]()) { Fields:
[Link](" " + field); private final int test$MyInt.n
}
[Link]("Methods:"); Methods:
for (var method : [Link]()) { public int test$[Link]()
[Link](" " + method);
}
[Link]("Super Class: " Super Class: class [Link]
+ [Link]());
Arthur Bit-Monnot | INSA 4IR 19 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Runtime Reflection for Meta-Programming
Any java object provide access, at runtime, to its Class object and all metadata associated
name
fields
methods
superclasses
...
This is called runtime reflection
Example usage: > JSON encoder for any java object (in 30 lines)
toJson(new Point(13, 18));
// { x: 13, y: 18 }
Arthur Bit-Monnot | INSA 4IR 20 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Anatomy of a Java Object: Enabler for Runtime Reflection
MyInt a = new MyInt(3); Each java object has a memory space
MyInt b = a; allocated on the heap.
Contains:
a header (12/16 bytes)
a: * header 3 the object’s fields values
b: *
A java program manipulates references (~
pointers) to the object’s memory space.
So what’s in the header?
Arthur Bit-Monnot | INSA 4IR 21 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
The Java Object Header
The java object header is composed of two parts1
a pointer to the class object
4/8 bytes of flags used internally by the JVM (synchronization, garbage collection)
p: * *class flags 3
header name: "MyInt" field: "n" ...
> [Link] documentation
1
may depend on the implementation of the JVM
Arthur Bit-Monnot | INSA 4IR 22 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Virtual Table for Dynamic Dispatch
In the class object, there is a virtual table that associates each method available on the class
to its implementation:
Example virtual table for our MyInt object
Method Implementation
equals Object::equals (@21ab324d)
hashCode Object::hashCode (@47b3f24d)
toString MyInt::toString (@63c3ef9)
Arthur Bit-Monnot | INSA 4IR 23 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Dynamic Dispatch, putting it all together
public static void print(Object o) {
[Link]("object: " + [Link]());
}
To invoke [Link](), the JVM must:2
dereference the pointer o, to access the object’s memory space in the heap
dereference the class pointer in the object’s header, to access the class’ metadata
find the toString method in the virtual table
execute the code at the address pointed at in the virtual table
Called dynamic dispatch as it allows to dynamically decide which function to call based on
the runtime class of an object.
2
In the worst case, many optimizations may avoid part of these costs
Arthur Bit-Monnot | INSA 4IR 24 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Dynamic Dispatch, caveats
Dynamic dispatch is a corner stone of OOP
⇒ required any time a method can be overridden
Arthur Bit-Monnot | INSA 4IR 25 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Dynamic Dispatch, caveats
Dynamic dispatch is a corner stone of OOP
⇒ required any time a method can be overridden
Caveats:
finding the method to call is costly (two extra indirections)
can be greatly mitigated by caching
prevents compiler optimization (inlining)
partially mitigated by specific compiler techniques (devirtualization)
Impose an inefficient memory layout
Arthur Bit-Monnot | INSA 4IR 25 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Memory Overhead of Java Objects
MyInt a = new MyInt(7);
a represents the number 7, a 4 bytes integer.
What is the memory overhead of having be a java object?
3
Conservative estimate, may be 8 bytes on 64bits systems without compressed oops
Arthur Bit-Monnot | INSA 4IR 26 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Memory Overhead of Java Objects
MyInt a = new MyInt(7);
a represents the number 7, a 4 bytes integer.
What is the memory overhead of having be a java object?
a: * *class flags 7
pointer to object memory space: 4 bytes3
header:
flags: 8 bytes:
class pointer: 4 bytes
3
Conservative estimate, may be 8 bytes on 64bits systems without compressed oops
Arthur Bit-Monnot | INSA 4IR 26 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Memory Overhead of Java Objects
In the best case, there is an overhead of 16 bytes per object.
With MyInt that is 80% of memory overhead
Worse, the payload is hidden behind a pointer !!!!
requires additional memory access, and potential cache-miss4
4
Recall that an uncached memory read is 100 times slower than an arithmetic operation
Arthur Bit-Monnot | INSA 4IR 27 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Memory Overhead of Java Objects
In the best case, there is an overhead of 16 bytes per object.
With MyInt that is 80% of memory overhead
Worse, the payload is hidden behind a pointer !!!!
requires additional memory access, and potential cache-miss4
The penalty is dramatic for basic types.
cannot be escaped if you want to maintain runtime polymorphism (~ dynamic dispatch)
4
Recall that an uncached memory read is 100 times slower than an arithmetic operation
Arthur Bit-Monnot | INSA 4IR 27 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
“Everything is an Object”. . . except primitive types
Java deeply adheres to the “everything is an object” moto. . .
but provides an escape hatch for performance critical code
Primitives types:
int i = 4;
int / long
i: 4
float / double
bool
char long l = 4;
Principle: l: 4
a primitive type only holds a value
Arthur Bit-Monnot | INSA 4IR 28 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Boxing: primitive ↔ object interoperability
public static void print(Object o) {
[Link]("object: " + o);
}
int n = 7;
print(n); // problem: print only accepts object references
Arthur Bit-Monnot | INSA 4IR 29 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Boxing: primitive ↔ object interoperability
public static void print(Object o) {
[Link]("object: " + o);
}
int n = 7;
print(n); // problem: print only accepts object references
// the above is implicitly translated as
int n = 7;
Integer nBoxed = new Integer(n);
print(nBoxed);
Called boxing: encapsulation of a pure value into an object
enables (costly) interoperability of primitive types with normal java code
Arthur Bit-Monnot | INSA 4IR 29 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Boxing: available for all primitives
Primitive type Boxed type
int [Link]
long [Link]
float [Link]
double [Link]
boolean [Link]
char [Link]
Arthur Bit-Monnot | INSA 4IR 30 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Java Objects, a small recap
a java program manipulates references to objects
each object has its own identity and memory space on the heap
the class of the object can be retrieved at runtime thanks to the class pointer in the
object header
enables runtime reflection: analysis of the object’s metadata
enables dynamic dispatch, choice at runtime of which method to execute based on the
object’s runtime class
Arthur Bit-Monnot | INSA 4IR 31 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Building well-behaved objects: String representation
public class MyInt {
public final int n;
MyInt(int n) {
this.n = n; MyInt a = new MyInt()
} [Link](a) // 1
[Link](b) // 2
@Override
public String toString() {
return [Link]();
}
Arthur Bit-Monnot | INSA 4IR 32 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Building well-behaved objects: Equality
Two kinds of equality tests in java:
a == b: test whether two objects have the same identity (~ pointer equality)
[Link](b): test whether two object are logically equivalent
provided with a dummy default in [Link]
can (and should) be overridden
> Object::equals documentation
Arthur Bit-Monnot | INSA 4IR 33 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Building well-behaved objects: Equality
The equals method should return true if two objects are equivalent and be:
reflexive: [Link](x)
symmetric: [Link](y) implies [Link](x)
transitive: [Link](y) and [Link](z) implies [Link](z)
consistent: stable result when changes to data
return false if passed a null value
be consistent with hashCode()
Arthur Bit-Monnot | INSA 4IR 34 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Building well-behaved objects: hashCode
The hash-code is a 32 bits signature of an object which can be changed by overriding
Object::hashCode
public int hashCode() { ... }
> Object::hashCode documentation
Properties:
Two equivalent objects (as witnessed by equals) MUST have the same hashcode
IDEALLY two distinct objects SHOULD have a different hashcode
Arthur Bit-Monnot | INSA 4IR 35 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Building well-behaved objects: hashCode
Exercise: how to get (well distributed) hashes for strings ?
"a", "b", "ab", "aab", "ba", ...
Arthur Bit-Monnot | INSA 4IR 36 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Building well-behaved objects: hashCode
Exercise: how to get (well distributed) hashes for strings ?
"a", "b", "ab", "aab", "ba", ...
int hash(String str) {
int result = 1; // 1: meaningful hashCode even if we get an array of 0
for (char c : str) {
result = 31 * result + (int) c; // 31: magic prime number
}
return result;
}
// built-in method for combining hashes of several objects
[Link]("Arthur", 35, "INSA Toulouse", 12.34)
Arthur Bit-Monnot | INSA 4IR 36 / 47
Advanced Programming in Java CM1: the Language | Java in the Realm of Programming Languages
Building well-behaved objects: Equality & Hash
public class MyInt {
public final int n;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != [Link]()) return false;
MyInt myInt = (MyInt) o;
return n == myInt.n;
}
@Override
public int hashCode() {
return [Link](n);
}
}
Arthur Bit-Monnot | INSA 4IR 37 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
Section 3
The Standard Library (stdlib)
Arthur Bit-Monnot | INSA 4IR 38 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
What’s a standard library?
stdlib: a collection of code that is part of the language definition
Purpose:
code sharing: provide common code that most programs use
file handling, collections, . . .
interoperability: ensure that programs agree on fundamental types
Because it is part of the language, the compiler knows the types of the stdlib
enables syntactic sugar
Arthur Bit-Monnot | INSA 4IR 39 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
Syntactic Sugar for StdLib
var i = new MyInt(3);
[Link]("num: " + i);
Arthur Bit-Monnot | INSA 4IR 40 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
Syntactic Sugar for StdLib
var i = new MyInt(3);
[Link]("num: " + i);
The compiler knows:
that "num: " is a [Link]
that i is an instance of [Link]
that [Link] has the toString() method
and can interpret the above code as:
[Link]("num: " + [Link]());
Arthur Bit-Monnot | INSA 4IR 40 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
What’s in the java standard library
language fundamentals: Object, String, Integer, . . .
concurrency: Thread, locks, atomics
collections: lists, sets, maps
input/output: files
networking: TCP, UDP
graphical user interface
Arthur Bit-Monnot | INSA 4IR 41 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
StdLib: Arrays
An Array is a fixed-length block of memory
Contains:
a header (like all java objects)
a field indicating the length of the array
one space for each element of the array
Exists for
primitive types (int[], float[], . . . )
Object types (Object[], MyInt[], . . . )
int[]: * header 3 (length) 7 3 4
Arthur Bit-Monnot | INSA 4IR 42 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
StdLib: The Collection Library
Arthur Bit-Monnot | INSA 4IR 43 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
The Iterable Interface
> Iterable documentation
public void printAll(Iterable<?> iterable) {
for (Object item : iterable) {
[Link](item)
}
}
Arthur Bit-Monnot | INSA 4IR 44 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
The Iterable Interface
> Iterable documentation
public void printAll(Iterable<?> iterable) {
for (Object item : iterable) {
[Link](item)
}
}
// desugared
public void printAll(Iterable<?> iterable) {
Iterator<?> iterator = [Link]();
while ([Link]()) {
Object item = [Link]();
[Link](item)
}
}
Arthur Bit-Monnot | INSA 4IR 44 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
Generics
Most collections are generic, they may contain any reference type (not primitive types)
In List<T>, T is a type parameter.
It indicates what is the class of all objects contained in the list.
List<MyInt> ints = new ArrayList();
[Link](new MyInt(7));
In a receiver position, you may indicate ? as a type parameter:
indicates that your program will work regardless of the type
public void printAll(Iterable<?> iterable) {
Arthur Bit-Monnot | INSA 4IR 45 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
Different collections
list: sequence of values
set: group of unordered, unique values
queue: collection of values for which there is an extraction order
map: dictionary associating keys to values
Arthur Bit-Monnot | INSA 4IR 46 / 47
Advanced Programming in Java CM1: the Language | The Standard Library (stdlib)
Case Study: HashSet<Integer>
class MyInt {
public final int n;
Set<T>: A collection that contains no
private MyInt(int n) {
duplicate elements. this.n = n;
}
insert(T t) public boolean equals(Object o) {
if (this == o) return true;
contains(T t) if (o == null || getClass() != [Link]())
remove(T t) MyInt myInt = (MyInt) o;
return n == myInt.n;
}
HashSet<T>: a Set<T> backed by a hash public int hashCode() {
table return n; // terrible implementation
}
}
Correction: [Link]
Arthur Bit-Monnot | INSA 4IR 47 / 47