1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Test<E> {
private final E value;
public Test(E value) {
this.value = value;
}
public E get() {
return this.value;
}
public static void main(String[] args) {
Test<String>[] tests = new Test[10]; // warning
Object[] objects = tests;
objects[0] = new Test<Integer>(0); // En théorie cela devrait planter, mais ce n'est pas le cas
System.out.println("Ce code fonctionne sans erreur");
System.out.println("On met pourtant un Test<Integer> dans un tableau de Test<String>");
System.out.println();
System.out.println("En fait cela plantera lorsqu'on lira le tableau :");
String s = tests[0].get(); // ClassCastException, alors que ce code ne devrait pas générer d'exception
}
} |
Partager