Query related to == in case of Wrapper classes [duplicate] - java

This question already has answers here:
Integer wrapper class and == operator - where is behavior specified? [duplicate]
(2 answers)
Closed 8 years ago.
I have been playing with == in wrapper classes. There's some weird thing that I encountered.
Integer i1 = 3;
Integer i2 = 3;
if(i1 != i2)
{
System.out.println("i1 and i2 are not equal"); // if condition returns false in this case
}
which is working perfectly fine as far as my knowledge is concerned.
whereas, If I instantiate wrapper with
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2)
{
System.out.println("i1 and i2 are not equal"); // if condition returns true in here
}
Then I am getting confused with the dual behavior of JVM with respect to the different values assigned to wrapper.
Why is this happening so? Please share your thought on this.
Thanks

You need to use !i1.equals(i2)

Related

Tricky but not hard endless loop exercise [duplicate]

This question already has answers here:
Why does (i<=j && j<=i && i!=j) evaluate to TRUE?
(10 answers)
Closed 2 years ago.
I saw this exercise on a Java test, which finally led me to ask for ideas because there are some solutions I tried, but obviously won't solve the problem, due to Math conditions.
So the exercise:
Define S and T variables as it results an endless while loop below!
while(s <= t && s >= t && s != t)
{
}
think about OOP, in fact 0!=0 is false, but new Integer(0) != new Integer(0) is true because it's not the same reference.
so Integer S= new Integer(0); Integer T = new Integer(0);
should resolve your problem
here is answer :
This is not possible with primitive types. You can achieve it with boxed Integers:
Integer a = new Integer(1);
Integer b = new Integer(1);
The <= and >= comparisons will use the unboxed value 1, while the != will compare the references and will succeed since they are different objects.
you can check farther details in this link How can "a <= b && b <= a && a != b" be true?

Why do these two number comparisions not work the same way? [duplicate]

This question already has answers here:
How can I properly compare two Integers in Java?
(10 answers)
Why Comparison Of two Integer using == sometimes works and sometimes not? [duplicate]
(3 answers)
Weird Integer boxing in Java
(12 answers)
Closed 4 years ago.
Integer i3 = 10;
Integer i4 = 10;
System.out.println(i3 == i4);
Integer i5 = 1000;
Integer i6 = 1000;
System.out.println(i5 == i6);
I got output for the above code as,
true
false
I can understand how i3==i4, but what is the reason to i5==i6 become false.

i am getting true and false output why? [duplicate]

This question already has answers here:
Weird Integer boxing in Java
(12 answers)
Closed 7 years ago.
I know int range is -2147483648 to +2147483647 but here I'm getting output as true and false. Why? Actually i1 and i2 point to the same object, so output is true. I can understood but i3 and i4 also pointing to same object but I got output as false. Why?
public class MainClass {
public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2);
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4);
}
}
the output is
true
false
why output should be like this?
Because you are using Integer object. For Integer object, values in between -128 to 127 are pooled
The problem is the difference between == and equals. == just tests to see if two Integer variables point to the same object, which could be either true or false depending on the implementation of your JVM. equals actually tests to see if they hold the same value. So in this case, you'll want to use equals:
// ...
System.out.println(i1.equals(i2));
// ...
System.out.println(i3.equals(i4));

Puzzling behaviour of == after postincrementation [duplicate]

This question already has answers here:
Integer wrapper objects share the same instances only within the value 127? [duplicate]
(5 answers)
Closed 10 years ago.
Someone postulated in some forum thread that many people and even experienced Java Developers wouldn't understand the following peace of Java Code.
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1++ == i2++);
System.out.println(i1 == i2);
As a person with some interest in Java I gave it my thoughts and came to the following result.
System.out.println(i1++ == i2++);
// True, since we first check for equality and increment both variables afterwards.
System.out.println(i1 == i2);
// True again, since both variables are already incremented and have the value 128
Eclipse tells me otherwise. The first line is true and the second is false.
I really would appreciate an explanation.
Second question. Is this Java specific or does this example hold for example for the C based languages, too?
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1++ == i2++);
// here i1 and i2 are still 127 as you expected thus true
System.out.println(i1 == i2);
// here i1 and i2 are 128 which are equal but not cached
(caching range is -128 to 127),
In case 2 if you use equals() it'd return true as == operator for integers only works for cached values. as 128 is out of cache range the values above 128 will not be cached, thus youhave to use equals() method to check if two integer instances above 127 are true
TEST:
Integer i1 = 126;
Integer i2 = 126;
System.out.println(i1++ == i2++);// true
System.out.println(i1 == i2); //true
Integer i1 = 126;
Integer i2 = 126;
System.out.println(i1++ == i2++);// true
System.out.println(i1.equals(i2)); //true
Integer i1 = 128;
Integer i2 = 128;
System.out.println(i1++ == i2++);// false
System.out.println(i1==i2); //false
Integer i1 = 128;
Integer i2 = 128;
System.out.println(i1++.equals(i2++));// true
System.out.println(i1.equals(i2)); //true
As explained this is due to Integer caching. For fun, you can run the program with the following JVM option:
-XX:AutoBoxCacheMax=128
and it will print true twice (option available on hostpot 7 - not necessarily on other JVMs).
Note that:
this is JVM specific
the modified behaviour is compliant with the JLS which says that all values between -128 and +127 must be cached but also says that other values may be cached.
Bottom line: the second print statement is undefined in Java and can print true or false depending on the JVM implementation and/or JVM options used.

How Integer object is created? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How != and == operators work on Integers in Java?
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 == i2) is return false. Exactly what it happen how it is checking this condition here?
if i assign lesser than 128 in both i1 and i2 if condition is true. How object is created here, it is comman for all the values or different?
Could someone clarify this scenario.
if i assign lesser than 128 in both i1 and i2 if condition is true
Yes this happens because for that range Java uses flyweight pattern and caches Integer objects so you get backed the cached version and == works
This is possible as Integer objects are immutable and the caching is only for the range [-128,127]

Categories