How Integer object is created? [duplicate] - java

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]

Related

Different behavior of int and Integer [duplicate]

This question already has answers here:
How do I compare two Integers? [duplicate]
(9 answers)
Closed 7 years ago.
I noticed that the following code returns false
Integer a = 600;
Integer b = 600;
System.err.println(a == b);
but this one
int a = 600;
int b = 600;
System.err.println(a == b);
returns true
can anybody explain?
The most important thing to know is the values up to 128 are cached, and the JVM gives you the same objects,for that the reference comparison works. Above 128 it creates a new instance.
For more info go to javadoc of Integer.valueOf(int) (which is what happens behind the scene)
This behavior is correct, in java == compares object references, it checks to see if the two operands point to the same object (not equivalent objects, the same object).
so your 1st example:
Integer a = 600;
Integer b = 600;
System.err.println(a == b);
you are asking java to tell you if the have the same reference, which is false
in the example 2:
a and b are primitives, and you are asking java to tell you if the have the same value, which is True
Jeff had already an answer for this

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));

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

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)

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.

java == for Integer [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Inconsistent behavior on java's ==
Integer wrapper objects share the same instances only within the value 127?
I have found the following == behaviour for Integer objects and I fail to understand it. (I am well aware that one should use equals for such comparisons, but I am studying for OCPJP...)
On short, == works as expected for 1000, but not for 10.
The former fragment of code is:
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");
and it behaves as one would expect:
different objects
meaningfully equal
The latter though:
Integer i3 = 10;
Integer i4 = 10;
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");
has the following output:
same object
meaningfully equal
Can someone please explain why this is happening?
Since Java 5, wrapper class caching was introduced. The following is an examination of the cache created by an inner class, IntegerCache, located in the Integer cache. For example, the following code will create a cache:
Integer myNumber = 10
or
Integer myNumber = Integer.valueOf(10);
256 Integer objects are created in the range of -128 to 127 which are all stored in an Integer array. This caching functionality can be seen by looking at the inner class, IntegerCache, which is found in Integer:
So when creating an object using Integer.valueOf or directly assigning a value to an Integer within the range of -128 to 127 the same object will be returned. Therefore, consider the following example:
Integer i = 100;
Integer p = 100;
if (i == p)
System.out.println("i and p are the same.");
if (i != p)
System.out.println("i and p are different.");
if(i.equals(p))
System.out.println("i and p contain the same value.");
The output is:
i and p are the same.
i and p contain the same value.
It is important to note that object i and p only equate to true because they are the same object, the comparison is not based on the value, it is based on object equality. If Integer i and p are outside the range of -128 or 127 the cache is not used, therefore new objects are created. When doing a comparison for value always use the “.equals” method. It is also important to note that instantiating an Integer does not create this caching.
Remember that “==” is always used for object equality, it has not been overloaded for comparing unboxed values
See Integer wrapper objects share the same instances only within the value 127?. The Integer class keeps shared static instances around for common values.

Categories