This question already has answers here:
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
What is the difference between an int and an Integer in Java and C#?
(26 answers)
Closed 7 years ago.
It holds true for (Integer) 1 == (Integer) 1, which seems legitimate.
So why it's having excursion for (Integer) 222's equality?
Integer is a class. So to compare objects you need to use equals instead of ==
What actually happens with shorter Integer is that if you get an Integer using the method valueOf you get always the same cached instance for values between -128 and 127. So in this case == works.
It doesn't work if you instead of using valueOf create a new instance explicitly with the operator new.
For To be more clear I write the current implementation of valueOf
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}
Related
This question already has answers here:
What is the difference between == and equals() in Java?
(26 answers)
Closed 2 years ago.
What is the difference between these two cases? Would the equality check be wrong? If not, which one is faster?
If you use wrapper class (Byte instead of byte), you should use equals method. The code below doesn't work (not likely to happen) as == compare reference not the value.
public static void main(String[] args) {
Byte b1 = new Byte((byte) 0);
Byte b2 = new Byte((byte) 0);
System.out.println(b1 == b2);
}
Some explanation here.
This question already has answers here:
How can I properly compare two Integers in Java?
(10 answers)
Closed 3 years ago.
I was coding an algorithm issue, below code can't pass case
public void pop() {
if (s1.pollFirst() == minStack.peekFirst())
minStack.pollFirst();
}
however below can,
public void pop() {
int tmp = s1.pollFirst() ;
if (tmp == minStack.peekFirst())
minStack.pollFirst();
}
the only difference is how I use s1,pollFirst() return result. I can't figure out real difference here.
Thanks
Comparing two Integer Objects with values less than -128 or bigger than 127 using == will always result in false. However, if you compare Integer with primitive int, it will give you true if the actual value is the same.
int n1=128;
Integer n2=127;
Integer n3=127;
Integer n4=128;
Integer n5=128;
System.out.println(n1==n2); //false
System.out.println(n2==n3); //true
System.out.println(n4==n5); //false
System.out.println(n1==n5); //true
In the second example you are assigning the value to the primitive int, therefore it is automatically unboxed.
This question already has answers here:
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
Closed 6 years ago.
I think that the output should be (true true). Sorry for my english
public class A{
public static void main(String[] args) {
Integer i1 = 128;
Integer i2 = 128;
System.out.println(i1 == i2);
Integer i3 = 127;
Integer i4 = 127;
System.out.println(i3 == i4);
}
}
There is a cache of Integer instances for a range of values (at least -128–127), that is used when implicitly converting an int to an Integer.
In this case, 128 is not in the cache, and so each Integer object representing that value is new and distinct.
The value 127, on the other hand, is guaranteed to be in the cache, and so the same instance of Integer is obtained repeatedly.
Integer, as opposed to the primitive int, is an object. Your comparison is comparing two objects of type Integer. I'm kind of surprised you get "false true". If you instead try:
System.out.println(i1.intValue() == i2.intValue());
System.out.println(i3.intValue() == i4.intValue());
you should get the expected result.
I got a very strange problem when I'm trying to compare 2 Long variables, they always show false and I can be sure they have the same number value by debugging in Eclipse:
if (user.getId() == admin.getId()) {
return true; // Always enter here
} else {
return false;
}
Both of above 2 return values are object-type Long, which confused me. And to verify that I wrote a main method like this:
Long id1 = 123L;
Long id2 = 123L;
System.out.println(id1 == id2);
It prints true.
So can somebody give me ideas?. I've been working in Java Development for 3 years but cannot explain this case.
== compares references, .equals() compares values. These two Longs are objects, therefore object references are compared when using == operator.
However, note that in Long id1 = 123L; literal value 123L will be auto-boxed into a Long object using Long.valueOf(String), and internally, this process will use a LongCache which has a [-128,127] range, and 123 is in this range, which means, that the long object is cached, and these two are actually the same objects.
because == compares reference value, and smaller long values are cached
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
so it works for smaller long values
Also See
Integer wrapper class and == operator - where is behavior specified?
Stuck on an issue for 4 hours because of the use of == ... The comparison was ok on Long < 128 but ko on greater values.
Generally it's not a good idea to use == to compare Objects, use .equals() as much as possible ! Keep ==, >, <, <= etc. for primitives.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java question about autoboxing and object equality / identity
Integer i1 = 10;
Integer i2 = 10;
Integer i3 = 210;
Integer i4 = 210;
if(i1 ==i2){
System.out.println("True");
}else{
System.out.println("False");
}
if(i3==i4){
System.out.println("True");
}else{
System.out.println("False");
}
if(Integer.valueOf(10) ==Integer.valueOf(10)){
System.out.println("True");
}else{
System.out.println("False");
}
if(Integer.valueOf(210) ==Integer.valueOf(210)){
System.out.println("True");
}else{
System.out.println("False");
}
The answer is
True
False
True
False
Why it is giving false for 2 and 4 condition ?
Use .equals() to compares Integer.== compares refrences equality
== compares instances not values. Use int instead of Integer and it will work
Note that Integer is an object, not a primitive. You're comparing different object instances.
For this particular example, it's worth reading about boxing.
In Java use the Object function Object.equals(Object) to compare objects. That comparison would only work correctly using the primitive int.