SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
arvind kushwaha wrote:Dear team,
Please explain the below code why it showing different result being the same object.
Integer i1 = new Integer(2);
Integer i2 = new Integer(2);
System.out.println(i1 == i2); // FALSE
Integer j1 = 2;
Integer j2 = 2;
System.out.println(j1 == j2); // TRUE
Integer k1 = 150;
Integer k2 = 150;
System.out.println(k1 == k2); // FALSE
Integer jj1 = 127;
Integer jj2 = 127;
System.out.println(jj1 == jj2); // TRUE
int jjj1 = 127;
Integer jjj2 = 127;
System.out.println(jjj1 == jjj2); // TRUE
Integer kk1 = 128;
Integer kk2 = 128;
System.out.println(kk1 == kk2); // FALSE
Integer kkk1 = 128;
int kkk2 = 128;
System.out.println(kkk1 == kkk2); // TRUE
Integer w1 = -128;
Integer w2 = -128;
System.out.println(w1 == w2); // TRUE
Integer m1 = -129;
Integer m2 = -129;
System.out.println(m1 == m2); // FALSE
int mm1 = -129;
Integer mm2 = -129;
System.out.println(mm1 == mm2); // TRUE
Could any one explain it properly as i m not able to understand it.
gurpeet singh wrote:2. they are of type Short, Integer and is between -128 to 127.
Ideally, boxing a given primitive value p, would always yield an identical reference.
Campbell Ritchie wrote:By the way, which implementation are you using which caches Integer.valueOf(128)?
arvind kushwaha wrote:
Integer kk1 = 128;
Integer kk2 = 128;
System.out.println(kk1 == kk2); // FALSE
Integer kkk1 = 128;
int kkk2 = 128;
System.out.println(kkk1 == kkk2); // TRUE
Damn! I got that bit wrong.Adrian Burkett wrote: . . . His example wasn't caching 128. When both variables were Integers it returned false. When one variable was an int it returned true because the Integer variable would have been unboxed. . . .
Campbell Ritchie wrote:I was hoping somebody had found an implementation which caches more than -128…127!
Campbell Ritchie wrote:It says that option was introduced in Java6.
arvind kushwaha wrote:
Integer i1 = new Integer(2);
Integer i2 = new Integer(2);
System.out.println(i1 == i2); // FALSE
arvind kushwaha wrote:
Integer j1 = 2;
Integer j2 = 2;
System.out.println(j1 == j2); // TRUE
arvind kushwaha wrote:
Integer k1 = 150;
Integer k2 = 150;
System.out.println(k1 == k2); // FALSE
arvind kushwaha wrote:
Integer jj1 = 127;
Integer jj2 = 127;
System.out.println(jj1 == jj2); // TRUE
arvind kushwaha wrote:
Integer kk1 = 128;
Integer kk2 = 128;
System.out.println(kk1 == kk2); // FALSE
arvind kushwaha wrote:
Integer kkk1 = 128;
int kkk2 = 128;
System.out.println(kkk1 == kkk2); // TRUE
arvind kushwaha wrote:
Integer w1 = -128;
Integer w2 = -128;
System.out.println(w1 == w2); // TRUE
arvind kushwaha wrote:
Integer m1 = -129;
Integer m2 = -129;
System.out.println(m1 == m2); // FALSE
arvind kushwaha wrote:
int mm1 = -129;
Integer mm2 = -129;
System.out.println(mm1 == mm2); // TRUE
arvind kushwaha wrote:
int jjj1 = 127;
Integer jjj2 = 127;
System.out.println(jjj1 == jjj2); // TRUE
Are you sure about that? The details are in the Java Language Specification. It doesn’t say anything about boxing or unboxing when using == and != on reference types, so doesn’t the true output simply mean that caching causes boxing to reuse the existing object?Adam Zehavi wrote: . . .
arvind kushwaha wrote:
Integer j1 = 2;
Integer j2 = 2;
System.out.println(j1 == j2); // TRUE
boxed --> -128 <= value < 128 --> unboxed --> compare primitive... so true!
arvind kushwaha wrote:
Integer k1 = 150;
Integer k2 = 150;
System.out.println(k1 == k2); // FALSE
boxed --> value < -128 || value >= 128 --> compare two different instances... so false!
. . .
Campbell Ritchie wrote:
Are you sure about that? The details are in the Java Language Specification. It doesn’t say anything about boxing or unboxing when using == and != on reference types, so doesn’t the true output simply mean that caching causes boxing to reuse the existing object?
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|