This question already has answers here:
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
How can I properly compare two Integers in Java?
(10 answers)
Closed 3 years ago.
My Question about Wrapper classes. Case 1: I'm Declaring two Integer variables and assigning same values 127, and i'm printing both hashCode. It's Generated same hashCode and result will be printed true. But In Case 2: Generated the Same hashCode but result will be printing false. Please Could you relate. Why Printing a false result.
package com.oca.test.exam;
public class TestCasesIntegerAndDouble {
public static void main(String[] args) {
//Case 1:
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1.hashCode()); //127
System.out.println(i2.hashCode()); //127
System.out.println(i1 == i2); //true
//Case 2:
Double d1 = 127.0;
Double d2 = 127.0;
System.out.println(d1.hashCode());//1080016896
System.out.println(d2.hashCode());//1080016896
System.out.println(d1 == d2); //false
}
}
Related
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 1 year ago.
class MyClass {
public static void main(String[] args) {
Integer a = 5;
Integer b = 5;
Integer c = 129;
Integer d = 129;
System.out.println(a == b);
System.out.println(c == d);
}
}
The output is
true
false
Why is this happening?
When using Integer wrapper objects instead of the int primitive, java globally caches these objects in this range because they are used often. This can speed up execution. Because these objects are cached, they are equal, while above the threshold a new objects is created of every integer.
When you want to compare Integers instead of ints use the .equals() method instead of ==.
This question already has answers here:
Why does the behavior of the Integer constant pool change at 127?
(4 answers)
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
How does auto boxing/unboxing work in Java?
(4 answers)
Closed 4 years ago.
Integer x1 = Integer.parseInt("4");
Integer y1 = Integer.parseInt("4");
Integer x2 = Integer.parseInt("444");
Integer y2 = Integer.parseInt("444");
System.out.println(x1==y1); // true
System.out.println(x2==y2); // false ???
Integer a1 = Integer.valueOf("4");
Integer b1 = Integer.valueOf("4");
Integer a2 = Integer.valueOf("444");
Integer b2 = Integer.valueOf("444");
System.out.println(a1==b1); // true
System.out.println(a2==b2); // false
I understand why the third and fourth output print true and false. This is because valueOf returns an object, and wrapper classes cache objects that have values in the range of -128 to 127. If valueOf is passed any value in that range, it should reuse the object in the cache. Otherwise, it will create a new object.
Now, why does the second output print out false? I thought parseInt returns a primitive, not an object like valueOf does.
I thought parseInt returns a primitive, not an object like valueOf does.
parseInt returns an int but you assign it to an Integer variable, which causes auto-boxing. Since an int whose value is 444 is being auto-boxed two times, each time a new Integer instance is created (since the Integer cache cannot be used for that value), so comparing them with == returns false.
If you'll assign the output of parseInt to an int, the comparisons will return true in both cases:
int x1 = Integer.parseInt("4");
int y1 = Integer.parseInt("4");
int x2 = Integer.parseInt("444");
int y2 = Integer.parseInt("444");
System.out.println(x1==y1); // true
System.out.println(x2==y2); // true
This question already has answers here:
How to concatenate int values in java?
(22 answers)
Closed 7 years ago.
I want to print the two integer variables divided.
int a = 1, b = 2;
System.out.println(a + b);
Obviously println() function processes them as integers and calculates the sum.
Instead I would like the output becomes like this "12". Any ideas?
Insert some blank Strings to induce this:
System.out.println(a +""+ b);
Store the integers as strings:
String a = "1";
String b = "2";
String c = a + b;
System.out.println(c);
This question already has answers here:
BigDecimal equals() versus compareTo()
(4 answers)
Closed 7 years ago.
The following code:
BigDecimal a = new BigDecimal("8900");
BigDecimal b = new BigDecimal("8.9E+3");
System.out.println(a.equals(b));
prints false. Why is so, if mathematically those numbers are equal?
You have to use compareTo():
BigDecimal a = new BigDecimal("8900");
BigDecimal b = new BigDecimal("8.9E+3");
System.out.println(a.compareTo(b) == 0);
This question already has answers here:
Integer wrapper objects share the same instances only within the value 127? [duplicate]
(5 answers)
Closed 8 years ago.
I was writing some test code and found one strange thing, and still confused how this is happening?
Integer i1 = 220;
Integer i2 = 220;
System.out.println(i1 == i2);
prints false as expected. But
Integer i1 = 20;
Integer i2 = 20;
System.out.println(i1 == i2);
prints true, but both are different references referring to different objects (I assume that).
How come second snippet prints true?
The == operator only works for Integer values between -128 and 127. That is why it doesn't work for 220 but does for 20. In general it is best to always use .equals() when comparing Integers and you should never rely on the == operator.
More information can be found here: https://www.owasp.org/index.php/Java_gotchas#Immutable_Objects_.2F_Wrapper_Class_Caching