Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

ClassCast exception

 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is giving class cast exception Can anybody explain me why?

Source java inquisition

public static void main ( String [] args ) {
Map<Integer, String> map = new LinkedHashMap<Integer, String> ();
Map<Integer, String> sap = new HashMap<Integer, String> ();
populate( map );
populate( sap );
System.out.println( map.get(1) + sap.get(1) );
}

static void populate ( Map m ) {
for ( int i = 0 ; i < 10 ; i++ ) {
m.put(i,i);
}
}

Regards,

Abdul Mohsin
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the declared type of the values (not the keys) stored in the two maps and then look at the type of the values actually put into the map.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi BARRY,
Just want to confirm that shoudn't the Compiler stop us when we were putting int values in map where it was expecting String.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Just want to confirm that shoudn't the Compiler stop us when we were putting int values in map where it was expecting String.



How will compiler know what type of key-value pair you store in a raw Map? Generics gives you type-safety. But you can always widen a typed class to a raw type class. In that case you loose the type-safety. To work around this, compiler just adds a typecheck at runtime when you retrieve data from a typed map/collection. And that is giving the cast exception in this case.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Mixing generic code to non-generic code. populate method overlooks at
the type safe Map and adds Integer object to the map (Autoboxing is there).
Now come back to main method, there is cast
(String)map.get(1)+(String)sam.get(1) to get the concatenated String
to be printed. But what is got back from the Map are Integer objects,
casting Integer to String causes well known ClassCastException.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abheshek:
Just want to confirm that shoudn't the Compiler stop us when we were putting
int values in map where it was expecting String.


What is passed to the populate() method is just a plain Map, non-type safe
Map, that asks for <Object,Object>. There is no information regarding
type safety of the Map that is defined in the main method as
Map<Integer,String>.

So simply int is boxed to Integer wrapper and added to the map.
 
Abhishek Kanchan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,
THANKS a lot.Your all posts are really very helpful.My doubt is cleared.
You and Manfred really champions.
 
    Bookmark Topic Watch Topic
  • New Topic