| Author |
equals
|
Sathya Shanmugam
Ranch Hand
Joined: Mar 02, 2007
Posts: 45
|
|
hi Java folks, Could anyone give some explanation for the example below Integer i3 = 1000; Integer i4 = 1000; if (i3 == i4) System.out.println("same object"); if (i3.equals(i4)) System.out.println("meaningfully equal"); if (i3 != i4) System.out.println("different objects"); o/p same object meaningfully equal different objects how come its displaying both same and different objects. pls give me some explanation.. Thanks
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
Originally posted by Sathya Shanmugam: hi Java folks, Could anyone give some explanation for the example below Integer i3 = 1000; Integer i4 = 1000; if (i3 == i4) System.out.println("same object"); if (i3.equals(i4)) System.out.println("meaningfully equal"); if (i3 != i4) System.out.println("different objects"); o/p same object meaningfully equal different objects how come its displaying both same and different objects. pls give me some explanation.. Thanks
I don't see how both same and different can be printing, because the conditions in those statements are opposite. The issue here has to do with boxing. The language specification guarantees that ints in the range of -128 to 127 will be boxed into the same Integer if they are autoboxed. However, individual JVMs might box other ints to the same Integer. So the output you get will depend on the JVM. But in this particular code, the language specification does not guarantee that i3 and i4 refer to the same object.
|
 |
Raghav Aggarwala
Greenhorn
Joined: Jul 21, 2005
Posts: 26
|
|
Sathya, I tried compiling public class MyClass{ public static void main(String[] args) { Integer i3 = 1000; Integer i4 = 1000; if (i3 == i4) System.out.println("same object"); if (i3.equals(i4)) System.out.println("meaningfully equal"); if (i3 != i4) System.out.println("different objects"); } } ------------------------ Gives output meaningfully equal different objects which is what it is suppose to do...
|
-----------------<br />SCJP 1.4<br />Target SCWCD by December 2007
|
 |
Scott Tiger
Greenhorn
Joined: Mar 23, 2007
Posts: 7
|
|
Integer i3 = 1000; Integer i4 = 1000; if (i3 == i4) System.out.println("same object"); if (i3.equals(i4)) System.out.println("meaningfully equal"); if (i3 != i4) System.out.println("different objects"); o/p same object meaningfully equal different objects
i think you should make a mistake the output as follows: meaningfully equal different objects when 2 compare objects are reference the sign of the "==" is used to compare their's reference is or isn't same.
|
 |
Sathya Shanmugam
Ranch Hand
Joined: Mar 02, 2007
Posts: 45
|
|
Sorry guys the O/p is meaningfully equal different objects
|
 |
Srinivasan thoyyeti
Ranch Hand
Joined: Feb 15, 2007
Posts: 557
|
|
Hi Satya, There was a point to make!!! You missed it. Try this and find out why ? public class MyClass9{ public static void main(String[] args) { Integer i3 = 100; Integer i4 = 100; if (i3 == i4) System.out.println("same object"); if (i3.equals(i4)) System.out.println("meaningfully equal"); if (i3 != i4) System.out.println("different objects"); } } output: same object meaningfully equal Have a nice time. I did saw earlier Keith has already pointed it out. [ April 03, 2007: Message edited by: Srinivasan thoyyeti ]
|
Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
|
 |
Sathya Shanmugam
Ranch Hand
Joined: Mar 02, 2007
Posts: 45
|
|
hi Srini, At last I found one point, but i don't know whats the reason when Int i3 and i4 is < and equals to 127 the o/p is Same Object Meaningfully equal When it is greater or equal to 128 the o/p is Meaningfully equal Different object could anyone pls explain how its happened
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
This is from the Java Language Specification 5.1.7. 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.
|
 |
Sathya Shanmugam
Ranch Hand
Joined: Mar 02, 2007
Posts: 45
|
|
|
Thanks Keith for the link and explanation
|
 |
Sanjay Singh
Greenhorn
Joined: Mar 02, 2006
Posts: 27
|
|
"Integer i = 100;" Here is my concern about this question. How can we assign primitive type int to Integer object, as written in the above code?
|
Regards,
Sanjay Singh
SCJP-1.6, OCEWCD6
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Hi Sanjay,
"Integer i = 100;" Here is my concern about this question. How can we assign primitive type int to Integer object, as written in the above code?
Java 5.0 offers Autoboxing facility. Integer i = 100; is automatically converted to Integer i = new Integer(100); //it is called boxing, //boxing primitive to the wrapper object Also see this: int x = new Integer(500); //it is Integer object is automatically unboxed to //primitive For more detail go through Java 5.0 Autoboxing features! Thanks and Regards, cmbhatt
|
cmbhatt
|
 |
Srinivasan thoyyeti
Ranch Hand
Joined: Feb 15, 2007
Posts: 557
|
|
Java 1.5 has knows Auto-Boxing and unBoxing. Integer i= int literal -- >After Boxing becomes Object--> new Integer("literal");
|
 |
Sanjay Singh
Greenhorn
Joined: Mar 02, 2006
Posts: 27
|
|
Thanks Chandra. It clears my confusion. I was taking it with Java 1.4, so got confused.
|
 |
 |
|
|
subject: equals
|
|
|