• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry guys

the O/p is

meaningfully equal
different objects
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith for the link and explanation
 
Ranch Hand
Posts: 37
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra. It clears my confusion.
I was taking it with Java 1.4, so got confused.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic