aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == , autoboxing Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== , autoboxing Doubt" Watch "== , autoboxing Doubt" New topic
Author

== , autoboxing Doubt

Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
Check out this code:

Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println(i==j);
System.out.println(i.equals(j));

Output: false
true

-------------------------

Integer i = new Integer(1000);
Integer j = new Integer(1000);
System.out.println(i==j);
System.out.println(i.equals(j));

O/p : false
true

In the first case according to Kathy and Sierra
Two instances of wrapper objs will always be == when their primitive values same from -128 to 127.
But i dont find any difference in the output.

Also

Integer i = 50;

gives me and error "cannot assign int to java.lang.Integer"
but
Integer 1 = new Integer(50);
works fine.
Thanks in advance
Sirisha


If you worry you cannot work... If you work you need not worry
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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 ]
Sai Charan
Greenhorn

Joined: Apr 24, 2006
Posts: 22
please explain output for this

Integer i = new Integer(100);
int j = 100 ;

//1
System.out.println(i==j);
System.out.println(i.equals(j));

//2
System.out.println(j==i);
System.out.println(j.equals(i));

Thanks
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
The code does not compile, because j is not an object, hence, you cannot invoke j.equals().

Thus, there is not output.
Sai Charan
Greenhorn

Joined: Apr 24, 2006
Posts: 22
please explain the difference for this (sorry if i am not creating a seperate post for this)

//1
Integer i = new Integer(100);
int j = 100 ;
System.out.println(i==j);
System.out.println(i.equals(j));

//2
Integer i = new Integer(100);
Integer j = 100 ;

System.out.println(i==j);
System.out.println(i.equals(j));

Thanks
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is from the Java Language Specification.

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.
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
Thank You, i got it.
kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
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?




Regards
Kirba.
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
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


SCJP, SCWCD, SCBCD
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Sirishaaaaa Ghatty,

we have a policy according naming.
Sirishaaaaa does not look much like a propper name.
See this please:
http://www.javaranch.com/name.jsp


Change your user name before posting next time.
Your old postings (including this) will not be affected, only your user name will update.


Yours,
Bu.


all events occur in real time
kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
hi deepu,
Thanks for clearing my doubts.

Regards
Kirba.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == , autoboxing Doubt