File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCast exception

 
Abdul Mohsin
Ranch Hand
Posts: 111
Java Linux Netbeans IDE
  • 0
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • 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.
 
Abhishek Kanchan
Ranch Hand
Posts: 35
  • 0
  • 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.
 
Sahid Khan
Ranch Hand
Posts: 41
  • 0
  • 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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic