This document discusses the evolution of the Java type system, including changes in Java 5-8 and future projects. Java 5 introduced generics and wildcards. Java 8 added generalized type inference and type annotations. The Checker Framework provides type checkers for properties like nullness and tainting. Project Valhalla aims to add value types and generic specialization to Java.
3. Java 5 Type System
Parametric polymorphism
public class Sample<T> {
Bounded wildcards
Intersection types
Type erasure
www.luxoft.com
private T data;
public T getData() { ... }
}
Collection<? extends Serializable>
public class Box<T extends Comparable & Clonable> { ...
4. Java 8 Type System
JEP 101: Generalized Target-Type Inference
- Add support for method type-parameter inference in method context
- Add support for method type-parameter inference in chained calls
JSR 308: Annotations on Java Types
www.luxoft.com
19. Value Types use cases
Numerics
Native types
Algebraic data types
Tuples
Cursors
Flatting
www.luxoft.com
20. Generic Specialization
Goal: support generics over primitives and value types
Specialization approaches
• ahead-of-time (Scala)
• on-demand (C#)
class Box<T> {
www.luxoft.com
private final T t;
public Box(T t) { this.t = t; }
public T get() { return t; }
}
22. Bytecode marked up to preserve erasure information (proposed)
class Box extends java.lang.Object{
private final java.lang.Object*T t;
public Box(java.lang.Object*T);
www.luxoft.com
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: aload_0
5: aload_1*T
6: putfield #2; //Field t:Ljava/lang/Object*T;
9: return
public java.lang.Object* get();
Code:
0: aload_0
1: getfield #2; //Field t:Ljava/lang/Object*T;
4: areturn*T
}
23. Bytecode after specialization
class Box${T=int} extends java.lang.Object{
private final int t;
public Box${T=int}(int);
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: aload_0
5: iload_1
6: putfield #2; //Field t:int;
9: return
public int get();
Code:
0: aload_0
1: getfield #2; //Field t:int;
4: ireturn
}
www.luxoft.com
24. Links
1. https://www.oracle.com/javaone/sessions/index.html Daniel Smith. Type Inference in Java SE 8.
2. http://2013.javapoint.ru/talks/21/ Александр Ильин. Type annotations in Java 8. И почему это хорошо.
3. http://types.cs.washington.edu/checker-framework/ The Checker Framework
4. http://mail.openjdk.java.net/pipermail/valhalla-dev/2014-July/000000.html Welcome to Valhalla
5. http://cr.openjdk.java.net/~jrose/values/values-0.html John Rose, Brian Goetz, and Guy Steele. State of
the Values. April 2014: Infant Edition
6. http://cr.openjdk.java.net/~briangoetz/valhalla/specialization.html Brian Goetz. State of Specialization.
Jule 2014: Infant Edition
www.luxoft.com