This question already has answers here:
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
Comparing boxed Long values 127 and 128
(4 answers)
Closed 5 years ago.
I'm trying to compare 4 values in a program.
Long val1 = 127l;
Long val2 = 127l;
Long val3 = 128l;
Long val4 = 128l;
Log.e("XXXX",(val1==val2)+" "+(val3==val4));
This gives me val1l == val2l is true and val3' == val4l is false. What is the reason behind this output. I think it's coming because of the wrapper class. Please help me to clear this.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
How come the first condition is false and the second is true? I was sure they were both true.
System.out.println(Integer.toString(3) == "3");
System.out.println(Integer.parseInt("3") == 3);
Integer.parseInt converts a String to a primitive int and primitives can be compared with ==. However, Integer.toString produces a String object and == for objects checks that they are the exact same reference; use String#equals instead to compare the values of the Strings.
System.out.println(Integer.toString(3).equals("3"));
System.out.println(Integer.parseInt("3") == 3);
The above code outputs:
true
true
This question already has answers here:
How can I properly compare two Integers in Java?
(10 answers)
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
Closed 3 years ago.
Integer a = 100, b = 100;
Integer c = 1000, d = 1000;
System.out.println(a == b);
System.out.println(c == d);
The output of the above code is:
true
false
Can someone explain why so?
This question already has answers here:
Java: Integer equals vs. ==
(7 answers)
Why do we use autoboxing and unboxing in Java?
(10 answers)
How can I properly compare two Integers in Java?
(10 answers)
Integer == int allowed in java
(6 answers)
Closed 4 years ago.
System.out.println(5 == new Integer(5)) output = true
Integer i31 = 2;
Integer i32 = new Integer(2);
System.out.println(i31 == i32); output = false
It rather seems since we are in function scope. hence different output.
Unable to grasp what can be different.
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);
}
This question already has answers here:
Equals operator for zeros (BigDecimal / Double) in Java
(7 answers)
Closed 8 years ago.
I want to compare a BigInteger with a BigDecimal. For example, the integer 10 should be equal to the decimal 10.0.
Why does the following return false? I am creating a new decimal from the integer, and then comparing the two decimals.
System.out.println(new BigDecimal(BigInteger.valueOf(10)).equals(BigDecimal.valueOf(10.0)));
This returns true:
System.out.println(new BigDecimal(BigInteger.valueOf(10)).equals(BigDecimal.valueOf(10)));
How can I correctly compare a BigInteger with a BigDecimal in the mathematical/human definition (10 == 10.0)?
For BigInteger and BigDecimal you should use the compareTo method to check if their value is the same
System.out.println(new BigDecimal(BigInteger.valueOf(10)).compareTo(BigDecimal.valueOf(10.0)));