I refer to Java Generics and Collections as the Book and the Book's authors as the Authors.
I would upvote this question more than once as the Book makes a poor job of explaining the principle IMO.
Statement
Principle of Truth in Advertising:
the reified type of an array must be a subtype of the erasure of its static type.
Further referred to as the Principle.
How does the principle help?
- Follow it and the code will compile and run without exceptions
- Do not follow it and the code will compile, but throw an exception at runtime.
Vocabulary
What is a static type?
Should be called the reference type.
Provided A
and B
are types, in the following code
A ref = new B();
A
is the static type of ref
(B
is the dynamic type of ref
). Academia parlance term.
What is the reified type of an array?
Reification means type information available at runtime. Arrays are said to be reifiable because the VM knows their component type (at runtime).
In arr2 = new Number[30]
, the reified type of arr2
is Number[]
an array type with component type Number
.
What is the erasure of a type?
Should be called the runtime type.
The virtual machine's view (the runtime view) of a type parameter.
Provided T
is a type parameter, the runtime view of the following code
<T extends Comparable<T>> void stupidMethod(T[] elems) {
T first = elems[0];
}
will be
void stupidMethod(Comparable[] elems) {
Comparable first = elems[0];
}
That makes Comparable
the runtime type of T
. Why Comparable
? Because that's the leftmost bound of T
.
What kind of code do I look at so that the Principle is relevant?
The code should imply assignment to a reference of array type. Either the lvalue or the rvalue should involve a type parameter.
e.g. provided T
is a type parameter
T[] a = (T[])new Object[0]; // type parameter T involved in lvalue
or
String[] a = toArray(s); // type parameter involved in rvalue
// where the signature of toArray is
<T> T[] toArray(Collection<T> c);
The principle is not relevant where there are no type parameters involved in either lvalue or rvalue.
Example 1 (Principle followed)
<T extends Number> void stupidMethod(List<T>elems) {
T[] ts = (T[]) new Number[0];
}
Q1: What is the reified type of the array ts
is referencing?
A1: Array creation provides the answer: an array with component type Number
is created using new
. Number[]
.
Q2: What is the static type of ts
?
A2: T[]
Q3: What is the erasure of the static type of ts
?
A3: For that we need the erasure of T
. Given that T extends Number
is bounded, T
's erasure type is its leftmost boundary - Number
. Now that we know the erasure type for T
, the erasure type for ts
is Number[]
Q4: Is the Principle followed?
A4: restating the question. Is A1 a subtype of A3? i.e. is Number[]
a subtype of Number[]
? Yes => That means the Principle is followed.
Example 2 (Principle not followed)
<T extends Number> void stupidMethod(List<T>elems) {
T[] ts = (T[]) new Object[0];
}
Q1: What is the reified type of the array ts
is referencing?
A1: Array creation using new
, component type is Object
, therefore Object[]
.
Q2: What is the static type of ts
?
A2: T[]
Q3: What is the erasure of the static type of ts
?
A3: For that we need the erasure of T
. Given that T extends Number
is bounded, T
's erasure type is its leftmost boundary - Number
. Now that we know the erasure type for T
, the erasure type for ts
is Number[]
Q4: Is the Principle followed?
A4: restating the question. Is A1 a subtype of A3? i.e. is Object[]
a subtype of Number[]
? No => That means the Principle is not followed.
Expect an exception to be thrown at runtime.
Example 3 (Principle not followed)
Given the method providing an array
<T> T[] toArray(Collection<T> c){
return (T[]) new Object[0];
}
client code
List<String> s = ...;
String[] arr = toArray(s);
Q1: What is the reified type of the array returned by the providing method?
A1: for that you need too look in the providing method to see how it's initialized - new Object[...]
. That means the reified type of the array returned by the method is Object[]
.
Q2: What is the static type of arr
?
A2: String[]
Q3: What is the erasure of the static type of ts
?
A3: No type parameters involved. The type after erasure is the same as the static type String[]
.
Q4: Is the Principle followed?
A4: restating the question. Is A1 a subtype of A3? i.e. is Object[]
a subtype of String[]
? No => That means the Principle is not followed.
Expect an exception to be thrown at runtime.
Example 4 (Principle followed)
Given the method providing an array
<T> T[] toArray(Collection<T> initialContent, Class<T> clazz){
T[] result = (T[]) Array.newInstance(clazz, initialContent);
// Copy contents to array. (Don't use this method in production, use Collection.toArray() instead)
return result;
}
client code
List<Number> s = ...;
Number[] arr = toArray(s, Number.class);
Q1: What is the reified type of the array returned by the providing method?
A1: array created using reflection with component type as received from the client. The answer is Number[]
.
Q2: What is the static type of arr
?
A2: Number[]
Q3: What is the erasure of the static type of ts
?
A3: No type parameters involved. The type after erasure is the same as the static type Number[]
.
Q4: Is the Principle followed?
A4: restating the question. Is A1 a subtype of A3? i.e. is Number[]
a subtype of Number[]
? Yes => That means the Principle is followed.
What's in a funny name?
Ranting here. Truth in advertising may mean selling what you state you are selling.
In
lvalue = rvalue
we have rvalue
as the provider and lvalue
as the receiver.
It might be that the Authors thought of the provider as the Advertiser.
Referring to the providing method in Example 3 above,
<T> T[] toArray(Collection<T> c){
return (T[]) new Object[0];
}
the method signature
<T> T[] toArray(Collection<T> c);
may be read as an advertisement: Give me a List of Long
s and I will give you an array of Long
s.
However looking in the method body, the implementation shows that the method is not being truthful, as the array it creates and returns is an array of Object
s.
So toArray
method in Example 3 lies in its marketing campaigns.
In Example 4, the providing method is being truthful as the statement in the signature (Give me a collection and its type parameter as a class literal and I will give you an array with that component type) matches with what happens in the body.
Examples 3 and 4 have method signatures to act as advertisement.
Examples 1 and 2 do not have an explicit advertisement (method signature). The advertisement and the provision are intertwined.
Nevertheless, I could think of no better name for the Principle. That is a hell of a name.
Closing remarks
I consider the statement of the principle unnecessarily cryptic due to use of terms like static type and erasure type. Using reference type and runtime type/type after erasure, respectively, would make it considerably easier to grasp to the Java layman (like yours truly).
The Authors state the Book is the best on Java Generics [0]. I think that means the audience they address is a broad one and therefore more examples for the principles they introduce would be very helpful.
[0] https://youtu.be/GOMovkQCYD4?t=53