• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

HashMap - ClassCastException

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Rekha...You are right...Line 1 doesnt compile if the method signature is changed to
static void pop(Map<Integer,String> m)
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, Harvinder and Srilatha !! Thanks.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
then, what will be the solution in this case to get the value?

Thanks
Preetha
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic