I was trying to run the below code
import java.util.Arrays;
import java.util.List;
class HelloWorld {
public static void main(String[] args){
ttt();
}
public static void ttt(){
List<String> result;
result = kkk();
// String ll = result.get(0);
// ll.split(",");
if(result.get(0) instanceof String){
System.out.println("alsfjalsdfjaslkfj");
}else{
System.out.println("Long Long");
}
System.out.println(result);
}
public static <R> R kkk(){
List<Long> x = Arrays.asList(2L, 2L, 3L);
return (R) x;
}
}
I was expecting the code to fail at compile time. Surprisingly, the code runs printing “Long Long”. How is Java able to load List<Long>
into List<String>
. Is there any documentation about this behaviour?