Type, casting an object to any type of collection

We recently encountered a bug in our code that was mainly related to OOP concepts.

class ABC { String a; ABC(){ a = "abc"; } } public class Main { static Object listABC() { List<ABC> listOfABC = new ArrayList<>(); listOfABC.add(new ABC()); return listOfABC; } public static void main(String[] args) throws java.lang.Exception { List<Long> listLong = (List) Main.listABC(); System.out.println(listLong.get(0)); } } 

Output: ABC @ 642c39d2

Wouldn't that lead to a runtime exception? Can someone point me in the right direction as to why this code does not throw an exception?

+5
source share
2 answers

Generic types are erased, and in your example, information about them no longer exists at run time. This is just a compilation of time. The <T> to List listing also discards type information at compile time, although it is still valid.

So, as long as you make legal throws at every step that you are, this is compromised. At run time, type information is not listed here.

If you tried to apply get (0) to Long, then you will get a classcastexception, since the element itself is ABC.

+7
source

The answer is pretty simple. Generics are compile-time components to ensure compile-time compatibility. Generics disappeared after compilation and therefore do not exist in Runtime anymore.

How Object listABC returns a List , (List) Test.listABC(); does not throw an exception because it is valid, since it actually returns a List .

you can assign it to List<Long> listLong because at run time listLong no longer knows about this Long generic.

As a result, this, although it looks wrong, is a valid executable code.

also the only reason this works is because the PrintStream#println(Object) method exists, otherwise you will get an Exception because you are trying to call it with the wrong type, as you can see in the following example.

 static Object listABC() { List<Test> listOfABC = new ArrayList<>(); listOfABC.add(new Test()); return listOfABC; } public static void main(String[] args) throws java.lang.Exception { List<Long> listLong = (List) Test.listABC(); System.out.println(listLong.get(0)); test(listLong.get(0)); } private static void test(Long longvalue) { System.out.println("TEST"); } 

O / p

 ABC@19e0bfd Exception in thread "main" java.lang.ClassCastException: ABC cannot be cast to java.lang.Long at ABC.main(ABC.java:19) 
+3
source

All Articles