Well, what you really need to understand is that the == operator checks if two objects are the same instance, not if they have the same state.
Hence if you say:
Integer a = new Integer(1000) Integer b = new Integer(1000)
The condition a == b will always yield false.
Now, in JSE 1.5 you can use autoboxing, which is a new feature that allows the compiler to treat primitive data types as if they were objects. At run time, the primitive data type is wrapped into its corresponding data type wrapper object (Byte, Short, Integer, Long, Float, Double, Character).
Hence, when using JDK 1.5 ,I could also say:
Integer a = 1000; Integer b = 1000;
Once again, two instances of the Integer class are created, thus a == b again yields false.
Nevertheless, all numeric values withing the range of byte are cached. That�s to say, there exist only one instance of them. Thus, this assigment
Integer a = 100; Integer b = 100;
...always returns the same instance of 100. Then the condition a == b will yield true in this case.
In order that a and b receive a different instance of the value "100" you need to use the new operator. Like this:
Integer a = new Integer(100); Integer b = new Integer(100);
In whose case, the condition a == b will yield false once again.
On the other hand equals(Object) will return true whenever the two Integer objects hold the same value. In this regard, all previous cases should return true, whether you compare the same instance of the object or not. [ May 24, 2006: Message edited by: Edwin Dalorzo ]
If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
Hi all, could someone clear my doubt.. 1.what Keith Lynn mean to say i am not getting? 2.see code below..
Integer i=new Integer(100); Integer j=new Integer(100); System.out.println(i==j); Output:false. Why it is false?
Equals:it will compare whether both ref variable(i nd j) both pointing to the same obj,is this statement correct Integer i=new Integer(100); Integer j=new Integer(50); System.out.println(i.equals(j)); Output:false. Why it is false?
Answer to second question: You did not get the concept, Lemmeie explain again a) == ref1 == ref2 will return true iff ref1 and ref2 both point to same object on heap otherwise the condition will return false. so now Integer a = new Integer(100); Integer b = new Integer(100);
Here JVM will construct two Integer objects because they are invoked using Integer constructor and now the two references a and b will point to two seperate objects in heap as a result a==b will result in false.
b) equals() : Integer class overides equals() of Object and will return true if two objects in comparision both have same value i.e., if( a.intValue() == b.intValue() ) return true. So now if you have Integer a = new Integer(100); Integer b = new Integer(100);
a.equals(b) or b.equals(a) will return true because the two Integer objects being referenced by a and b have same integer value of 100 and hence the result is true. So now you know why Integer a = new Integer(100); Integer b = new Integer(50); a.equals(b) or b.equals(a) will return false. Deepak