This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS 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

Map mock question

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
below is from John Myer's

Why does it throw RunTime ClassCastException at line ##??
Object has toString() method, doesn't it?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This program runs well and prints:
11

If you want it Throw ClassCastException, you need to change

to:



Best,
Yue
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony, are you sure you typed it correctly in your editor? The code you gave compiles just fine, and gives output "11".
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by John Johnson:
Anthony, are you sure you typed it correctly in your editor? The code you gave compiles just fine, and gives output "11".



sorry all, it should be put(i, i) not i+"".

anyone can give me explanation? thanks

 
YUE ZHU
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics type is about the erasure and cast, so if you mix use the generic and non-generic will compile but will throw RuntimeException.

Rule: generic is compiler magic,it simply erase the type and cast it.

Look at the source code I decompiled from the TestMap:

You see? it Cast the Integer to String, that's why throw ClassCastException.

Best,
Yue
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built 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