This week's book giveaway is in the JavaScript forum.
We're giving away four copies of JavaScript Promises Essentials and have Rami Sarieddine on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashMap - ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashMap - ClassCastException" Watch "HashMap - ClassCastException" New topic
Author

HashMap - ClassCastException

Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Source: Inquisition



On execution, there is a ClassCastException saying "java.lang.Integer cannot be converted to java.lang.String". I understand it occurs when calling get(), which tries to put an Integer into a String.

My doubt is: We have declared the generic type of the Maps to be Map<Integer,String>. Then, why does not the compiler err while compiling Line 1 where we are putting an int in a place where String is expected? (Because we have defined the Generic types to state that the Key is of type Integer and Value is of type String)
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
Originally posted by Rekha:

My doubt is: We have declared the generic type of the Maps to be Map<Integer,String>. Then, why does not the compiler err while compiling Line 1 where we are putting an int in a place where String is expected? (Because we have defined the Generic types to state that the Key is of type Integer and Value is of type String)


Well Rekha, first of all you need to remember that generics are a compile time check.

so if you say,



The compiler will ensure that you put key/value pair in *map* and *sap* which is of type Integer/String. After it has ensured that, it will remove the type info and your above two declarations would look like:



i.e. normal Maps to the jvm at runtime.

Now, if you declare static void pop(Map m) then you are declaring a Map raw type as an argument to the method pop. The compiler knows that it(Map m) can hold all Objects,..... right.

Hence it allows you to put int's into the Map by autoboxing them into Integers.

Therefore, when you try to retrieve the value of the Map's using get() method, the jvm throws ClassCastException as it got an Integer instead of a String.

So, it's a classical case of mixing generic type code with non-generic type code which the developer should avoid or be careful not to blow it up!!!

Hope this makes sense!
[ November 30, 2008: Message edited by: Harvinder Thakur ]

thanks
Harvinder
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
So, if I have the method declaration as static void pop(Map<Integer,String> m), will there be a compiler error while trying to add an int in place of a String? (the "value" part of the map)
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Yes Rekha...You are right...Line 1 doesnt compile if the method signature is changed to
static void pop(Map<Integer,String> m)


Thanks,<br />Srilatha M
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Got it, Harvinder and Srilatha !! Thanks.
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi,
then, what will be the solution in this case to get the value?

Thanks
Preetha
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
Hi Preetha ,
it is the wrong aproach to put a type in another type of collection.But it will not through compiler error but warning just to avoid the confusion of those code existing before jdk1.5. We can only avoid this type of situation only by you just have to be little more cautious.


Thanks,<br />Subhasish
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
if you ever need your generic code to interact with a non generic method then I recommend you use the Collections.checkedXXX series of methods (see the javadocs for details).
They provide a wrapper around your collection that will throw an exception immediately when 'bad' data is added instead of at a later time when you try to retrieve the value of the collection. This makes it infinitely easier to debug when problems occur.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap - ClassCastException