aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassCast exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ClassCast exception " Watch "ClassCast exception " New topic
Author

ClassCast exception

Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

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


Regards, Abdul Mohsin
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Abhishek Kanchan
Ranch Hand

Joined: May 10, 2007
Posts: 32
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.


SCJP 1.5<br />SCWCD 5.0
Sahid Khan
Ranch Hand

Joined: Jun 27, 2007
Posts: 41
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

Joined: Feb 28, 2007
Posts: 1707


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.


cmbhatt
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: May 10, 2007
Posts: 32
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
 
subject: ClassCast exception
 
Similar Threads
Map mock question
Map....
Mock Exam Question
Map Question
Integer String