Sathya Shanmugam
,
Ranch Hand
Apr 02, 2007 19:24:00
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
Apr 02, 2007 19:37:00
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
Apr 02, 2007 22:50:00
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...
Scott Tiger
,
Greenhorn
Apr 03, 2007 08:04:00
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
Apr 03, 2007 08:30:00
Sorry guys the O/p is meaningfully equal different objects
Srinivasan thoyyeti
,
Ranch Hand
Apr 03, 2007 08:42:00
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 ]
Sathya Shanmugam
,
Ranch Hand
Apr 04, 2007 10:04:00
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
Apr 04, 2007 10:07:00
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
Apr 04, 2007 10:18:00
Thanks Keith for the link and explanation
Sanjay Singh
,
Ranch Hand
Apr 05, 2007 00:25:00
"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?
Chandra Bhatt
,
Ranch Hand
Apr 05, 2007 00:57:00
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
Srinivasan thoyyeti
,
Ranch Hand
Apr 05, 2007 01:09:00
Java 1.5 has knows Auto-Boxing and unBoxing. Integer i= int literal -- >After Boxing becomes Object--> new Integer("literal");
Sanjay Singh
,
Ranch Hand
Apr 05, 2007 02:26:00
Thanks Chandra. It clears my confusion. I was taking it with Java 1.4, so got confused.
permaculture is giving a gift to your future self. After reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com