wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integer String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer String" Watch "Integer String" New topic
Author

Integer String

Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
source:http://enigma.vm.bytemark.co.uk/webstart.html
John Meyers's SCJP 5 mock exam:question 22


Answer:Runtime error

Runtime Exception. ClassCastException. It looks like 1 and 1 are added to obtain the result 2 but what happens is that, in main() map and sap are expected to return String values based on Integer keys. So when you pass a key, a String is supposed to be returned. At runtime an Integer is returned instead and when the JVM attempts to convert the Integer to String , BAM !


I can't understand that if we put "map.get(1);" there,we got no runtime exception.
The explanation says that " So when you pass a key, a String is supposed to be returned. ",but can't The JVM invoke implicitly the toString() method of Integer and also return a String?


SCJP 5.0 72%
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18103
    
  39

The explanation says that " So when you pass a key, a String is supposed to be returned. ",but can't The JVM invoke implicitly the toString() method of Integer and also return a String?


Basically, it is expecting the response to be a String. So, it will use the append() method of the StringBuffer/StringBuilder to do the concat. Yes, there is a version of append() that takes an object, and hence, uses the toString() method -- but the compiler is not calling that version.

The combination of using generics and not using generics is fooling the compiler.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
Originally posted by Henry Wong:


Basically, it is expecting the response to be a String. So, it will use the append() method of the StringBuffer/StringBuilder to do the concat. Yes, there is a version of append() that takes an object, and hence, uses the toString() method -- but the compiler is not calling that version.

The combination of using generics and not using generics is fooling the compiler.

Henry


Henry: I still can't understand,it is expecting the response to be a String,Why will it use append() method of the StringBuffer?It seems to be no
relation with StringBuffer here?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18103
    
  39

Henry: I still can't understand,


One piece at a time....

Why will it use append() method of the StringBuffer?It seems to be no relation with StringBuffer here?


The Java compiler uses the StringBuilder class to do concats.

"hello" + "world"

becomes

new StringBuilder().append("hello").append("world").toString();

it is expecting the response to be a String,


This is due to the generic. The compiler is expecting the result to be a String. So, when it calls the append() method, it calls the version that takes a String, whose implementation doesn't call the toString() method.

Henry
Dumitru Postoronca
Ranch Hand

Joined: May 06, 2008
Posts: 46
Another explanation might be that when you use generics, the compiler automatically inserts casts in your expressions, so
actually is compiled into
.

It might be that your map.get(1) + sap.get(1) be also transformed into (String) map.get(1) + (String) sap.get(1) which, of course makes it obvious why the ClassCastException.
Noam Wolf
Ranch Hand

Joined: Jan 12, 2008
Posts: 35
This is actually an interesting case, as pointed out before, where you're combining generics and pre-generics code. The problem is not with the compiler because you don't get a compile time error because you aren't doing anything syntactically incorrect.

The m.put(i,i) is basically what's screwing you... since both maps are defined to have an Integer key and a String value that's what you'd expect to get when you call "get()" but it looks like the non generic "put" is overriding the type of the value of the map to whatever you set it in the populate method.

If you change your populate method to:


The rest of your code will work correctly.


because .net guys can also write in java
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer String
 
Similar Threads
Map Question
Map mock question
Map....
Mock Exam Question
ClassCast exception