Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mixing Generics and non-generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mixing Generics and non-generics " Watch "Mixing Generics and non-generics " New topic
Author

Mixing Generics and non-generics

Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
HI,

In the K&B book, page 577 there is the below code:

import java.util.*;
public class TestBadLegacy {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<Integer>();
myList.add(4);
myList.add(6);
Inserter in = new Inserter();
in.insert(myList); // pass List<Integer> to legacy code
}
}
class Inserter {
// method with a non-generic List argument
void insert(List list) {
list.add(new Integer(42)); // adds to the incoming list
}
}
Sure, this code works. It compiles, and it runs. The insert() method puts an
Integer into the list that was originally typed as <Integer>, so no problem.
But�what if we modify the insert() method like this:

void insert(List list) {
list.add(new String("42")); // put a String in the list
// passed in
}
Will that work? The book states that Yes, sadly, it does! It both compiles and runs. No runtime exception. Yet, someone just stuffed a String into a supposedly type safe ArrayList of type <Integer>.

However, for me this has NOT worked I get a runtime exception java.lang.ClassCastException. Does anybody know whats going on? Is this a misprint in the book? Also if there is a link to corrections(if any) on the K&B SCJP5 book, I will be grateful. I have looked around but can't find it.


Many thanks.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Meghna Bhardwaj:
...for me this has NOT worked I get a runtime exception java.lang.ClassCastException. Does anybody know whats going on? Is this a misprint in the book? Also if there is a link to corrections(if any) on the K&B SCJP5 book, I will be grateful. I have looked around but can't find it...

It works for me -- tested on a Windows machine (at work) running Java 1.5.0.

There is a K&B errata page here. (A link to this is at the top of the SCJP forum page.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
V Gala
Ranch Hand

Joined: Aug 06, 2007
Posts: 113
I had compiled and run using list.add(new String("42");
It run without exception
may be after your insert call method
you are trying to retrive the element in which it may got a runtime exception
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
HI,

The code I am running is very simple and all it has is what I have posted. Does anyone know if its ok for some environments to catch the exception. If so I will jsut make a note that some environments may let it slip thru while others won't. If it is a incorrect for this to happen. I need to find out about my environment. Has anyone experienced this as well.
As far as I know i have the most upto date JRE 1.5

Thanks for the replies so far.
 
 
subject: Mixing Generics and non-generics
 
Similar Threads
Generics warnings... I don't understand?!
Generics Teaser
Need Help on Generecs and non-Generics
Queue
doubt in generics in java 1.5