• 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

HashMap type safety

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

i create a map with



then i fill it with values


then i want to read a value like 3456


then i get


How can a value be long in a Map wich is with type safety created?
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
What is req? What is the return type of getValues()? Does it return a Map with a different generic type, or even a raw type?
It would appear you are getting a String-Long pairing. The valueOf method takes int or String parameters; if you pass an Integer, that would be unboxed to int, but you can’t unbox a Long to an int. So the JVM tries to cast the result to a String, and that doesn’t work.

I think the problem is filling the Map inappropriately from a (??) database request. Did you get any compiler warnings for that code? If you do, you should heed them. If there are any warnings, then type‑safety cannot be guaranteed, as you see here.

By the way: house numbers are not integers. They are not numbers of any sort. I have had friends living at no 11a, at no 32a, at no 2½, etc. House numbers are Strings.
 
Sacha Genoud
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ritchie

thank you for your quick answer.

Now it works, but is it a good idea?


 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sacha Genoud wrote:Hello Ritchie

thank you for your quick answer.

You’re welcome

Now it works, but is it a good idea?


No.

You need to find out why you are getting the cast exception. Also, tell us whether there are any compiler warnings.
Look at the chapter in Effective Java about generics. Bloch says Java™ is completely type-safe as long as there are no warnings from the compiler and you have not used the suppress warnings annotation.
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic