File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring 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: 24195

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: 20183

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
I agree. Here's the link:
subject: Adding String to an Integer ArrayList
It's not a secret anymore!