Forums Register Login

equals

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
 


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.
+Pie Number of slices to send: Send
Sorry guys

the O/p is

meaningfully equal
different objects
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks Keith for the link and explanation
+Pie Number of slices to send: Send
"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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Java 1.5 has knows Auto-Boxing and unBoxing.

Integer i= int literal -- >After Boxing becomes Object--> new Integer("literal");
+Pie Number of slices to send: Send
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


reply
reply
This thread has been viewed 794 times.
Similar Threads
== and .equals()
KB 235
how is this possible please explain
Wrapper ==
difference between != or ==
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:45:41.