This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes maximum Integer value problem with HashMap(String, Integer)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "maximum Integer value problem with HashMap(String, Integer)?" Watch "maximum Integer value problem with HashMap(String, Integer)?" New topic
Author

maximum Integer value problem with HashMap(String, Integer)?

paul reberg
Greenhorn

Joined: Sep 01, 2010
Posts: 7
Hi. I have



I've noticed that if the value for myKey is over 127 (2^7-1), then I get the error message. Otherwise, if it's 127 or less, I'll get the success message. Why does this happen? And, what would you do in situations where the value is more than 128 (2^7)?

Thanks!

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4639
    
    5

I am surprised that any of your tests get success. You need to use the "equals()" function, not ==
paul reberg
Greenhorn

Joined: Sep 01, 2010
Posts: 7
ah, brain fart. yeah, you're right. i want to compare the values, not whether they're referring to the same object. however, the part that still confuses me is that they're clearly both different, distinct objects, which SHOULD mean != will always return true. But, if you test out the code, != will return either true or false depending on what you store as the value. Seems pretty odd, right?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Constant pooling for values < 128.

(Wouter's answer is better-I just hate typing on the iPad :)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It returns false because you use autoboxing to create your Integer objects.
Autoboxing uses the method Integer.valueOf(int) to create Integer objects.
That method use a cache and because of that, and the fact that Integer object are immutable, you get the same objects. So != returns false.

The range of that cache is -128 to 127 or the value set with java.lang.Integer.IntegerCache.high property with a minimum of 127.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
paul reberg
Greenhorn

Joined: Sep 01, 2010
Posts: 7
ahhh, i c. Ok, thanks alot for the insight!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

paul reberg wrote:ahhh, i c. Ok, thanks alot for the insight!

Sorry to be nitpicking, but it's "I see", not "i c". Please UseRealWords.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: maximum Integer value problem with HashMap(String, Integer)?
 
Similar Threads
can you please this doubt about hashcode()
Problem getting value from HashMap
Decoding Expresssions to values same as 'DECODE' function in Oracle
How to code program using API Hashmap to return two subsequently allocated strings
getting array values from HashMap!!!