Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Adding String to an Integer ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding String to an Integer ArrayList " Watch "Adding String to an Integer ArrayList " New topic

Adding String to an Integer ArrayList

Vivek Kr Singh
Ranch Hand

Joined: Oct 12, 2007
Posts: 56

Why there is no error on line 20 at runtime?

SCJP 1.4
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

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.

[Jess in Action][AskingGoodQuestions]
Rob Spoor

Joined: Oct 27, 2005
Posts: 19849

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.

How To Ask Questions How To Answer Questions
subject: Adding String to an Integer ArrayList