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)?
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
posted
0
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?
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.