• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals

 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys

the O/p is

meaningfully equal
different objects
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith for the link and explanation
 
Sanjay Singh
Ranch Hand
Posts: 37
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 1.5 has knows Auto-Boxing and unBoxing.

Integer i= int literal -- >After Boxing becomes Object--> new Integer("literal");
 
Sanjay Singh
Ranch Hand
Posts: 37
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chandra. It clears my confusion.
I was taking it with Java 1.4, so got confused.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic