• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mixing Generics and non-generics

 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic