• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Map mock question

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic