Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Integer String

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Gary Kevin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic