• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding String to an Integer ArrayList

 
Vivek Kr Singh
Ranch Hand
Posts: 56
  • 0
  • 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
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • 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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • 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