Quantcast
Channel: Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java? - Stack Overflow
Browsing latest articles
Browse All 9 View Live

Answer by helloworld for Why is 128==128 false but 127==127 is true when...

if the value is between -128 and 127, it will use the cached pool and this is true only when auto-boxing.So you will have below: public static void main(String[] args) { Integer a = new Integer(100);...

View Article


Answer by user13463803 for Why is 128==128 false but 127==127 is true when...

Other answers describe why the observed effects can be observed, but that's really beside the point for programmers (interesting, certainly, but something you should forget all about when writing...

View Article

Answer by Developer Marius Žilėnas for Why is 128==128 false but 127==127 is...

It is memory optimization in Java related.To save on memory, Java 'reuses' all the wrapper objects whose values fall in the following ranges:All Boolean values (true and false)All Byte valuesAll...

View Article

Answer by yanghaogn for Why is 128==128 false but 127==127 is true when...

Have a look at the Integer.java, if the value is between -128 and 127, it will use the cached pool, so (Integer) 1 == (Integer) 1 while (Integer) 222 != (Integer) 222 /** * Returns an {@code Integer}...

View Article

Answer by thejartender for Why is 128==128 false but 127==127 is true when...

I wrote the following as this problem isn't just specific to Integer. My conclusion is that more often than not if you use the API incorrectly, you sill see incorrect behavior. Use it correctly and you...

View Article


Answer by chrisbunney for Why is 128==128 false but 127==127 is true when...

Using primitive data types, ints, would produce true in both cases, the expected output.However, since you're using Integer objects the == operator has a different meaning.In the context of objects, ==...

View Article

Answer by Andreas Petersson for Why is 128==128 false but 127==127 is true...

When you compile a number literal in Java and assign it to a Integer (capital I) the compiler emits:Integer b2 =Integer.valueOf(127)This line of code is also generated when you use autoboxing.valueOf...

View Article

Answer by Michael Lloyd Lee mlk for Why is 128==128 false but 127==127 is...

Autoboxing caches -128 to 127. This is specified in the JLS (5.1.7). If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and...

View Article


Why is 128==128 false but 127==127 is true when comparing Integer wrappers in...

class D { public static void main(String args[]) { Integer b2=128; Integer b3=128; System.out.println(b2==b3); }}Output:falseclass D { public static void main(String args[]) { Integer b2=127; Integer...

View Article

Browsing latest articles
Browse All 9 View Live




<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>