For example,
Integer a = 4;
if (a < 5)
I know that this is a comparative operation by learning auto boxing. The Integer and int types are comparatively well calculated, The problem is when calculating Integer and Integer.
Integer a = 4;
Integer b = 5;
if (a == b)
In the code above, if a and b are the same objects, the result will be true, but maybe it's because of auto unboxing Or maybe it's because the objects are the same.
java integer autoboxing
In classes such as Integer and Long, the == operation compares the two objects with the same reference.
Integer x = ...;
Integer y = ...;
System.out.println(x == y);
In the code above, x==y compares the references that x and y refer to to to be the same It does not compare the values of numbers. So,
Integer x = new Integer(10);
Integer y = new Integer(10);
System.out.println(x == y);
When you do this, the result is false. If you want to compare the values in the Integer, You can compare it using the equals() method or intValue() method.
if (x.intValue() == y.intValue())
if (x.equals(y))
You can do it like this.
© 2024 OneMinuteCode. All rights reserved.