I have this code, which fails to compile:
import java.util.ArrayList;
import java.util.List;
public class TypeSafetyTest {
public static void main(String[] args) {
TypeSafetyTest test = new TypeSafetyTest();
test.run();
}
private void run() {
Car car = new Car();
List<String> wheelWeights = car.getWheelWeights();
}
private class Car {
List<Double> wheelWeights = new ArrayList<Double>();
public List<Double> getWheelWeights() {
return wheelWeights;
}
public void setWheelWeights(List<Double> wheelWeights) {
this.wheelWeights = wheelWeights;
}
}
}
It gives an “Incompatible types” error on the line:
List<String> wheelWeights = car.getWheelWeights();
However, if I change the line:
private class Car {
to
private class Car<T> {
then the code compiles successfully with the warning “Unchecked assignment” on the line that used to have the compile error. Why is this so? I was expecting it to give a compile error in both cases.
One issue I see is:
returns
DoubletypeList, but you are assigning it toList<String>The reason why you are not seeing compile error when you change
Car<T>is, because your instance creation is raw type, which is telling compiler that ignore type safety at compile time, due to backward compatability.If you change your object like this, then you will see compiler throwing the message again.
Please refer JLS 4.8 for more information.
Here is example from JLS:
It is not possible to access Inner as a partially raw type (a “rare” type), because Outer itself is raw, hence so are all its inner classes including Inner, and so it is not possible to pass any type arguments to Inner
The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of generics into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.
I would suggest reading Generics (Updated)