• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Mixing Generics and non-generics

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
His brain is the size of a cherry pit! About the size of this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic