DEV Community

Discussion on: Java is Unsound: The Industry Perspective

Collapse
 
pgier profile image
Paul Gier

Similar to an earlier comment, I noticed the statement "whenever get is called on a List, the runtime checks if the returned value is actually a String" seems to be incorrect. The compiler and runtime will happily let you do something like this:

Integer myInt = (Integer)(Object)strs.get(0);
Enter fullscreen mode Exit fullscreen mode

So the runtime did not enforce that the object returned from "get" was actually a string. The ClassCastException only occurs if you try to assign the value to a String.

Collapse
 
rosstate profile image
Ross Tate

Right. I simplified things for pedagogical purposes. The compiler inserts casts in order to make the resulting bytecode well typed. Since typically the result of strs.get would be used as a String, typically the cast is inserted. But examples such as yours show that this is not always done.