• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding String to an Integer ArrayList  RSS feed

 
Vivek Kr Singh
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why there is no error on line 20 at runtime?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With generics, no type checking is done at runtime; they're just a compile-time thing. By passing your List<Integer> to a method accepting just List as an argument, you've completely defeated the type system.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the compiler should have given you a warning that you are using a so-called raw type. Whenever you ignore or suppress these warnings, the compiler simply puts all responsibilities on the programmer.

Your example is just waiting for a ClassCastException to happen once you really start using the List, and it all stems from ignoring the warning.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!