aspose file tools*
The moose likes Beginning Java and the fly likes Doubt in equals and == . Please help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in equals and == . Please help." Watch "Doubt in equals and == . Please help." New topic
Author

Doubt in equals and == . Please help.

Soumya Ranjan Mohanty
Ranch Hand

Joined: Mar 07, 2010
Posts: 44
class Et {
public static void main(String args[]) {

Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("equal value");
}
}

produces the Output

different objects
equal value


but

class Et {
public static void main(String args[]) {

Integer i1 = 10;
Integer i2 = 10;
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("equal value");
}
}

produces

equal value


----------------

I am not getting the Reason Please Help ME..


Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
That's an example taken from the book K&B.

I report the explanation:

In order to save memory, two instances of the
following wrapper objects (created through boxing), will always be == when their
primitive values are the same:
■ Boolean
■ Byte
■ Character from \u0000 to \u007f (7f is 127 in decimal)
■ Short and Integer from -128 to 127



Bye,
Nicola
Soumya Ranjan Mohanty
Ranch Hand

Joined: Mar 07, 2010
Posts: 44
Ya it is from the book K&B..


But Still i have doubt in my Question why the two output are different. what is the difference between Integer i=10 and Integer i=1000. That means above 127 will not be same?? What is the reason?? also i didn't get that Sentence--" In order to save memory".
Soumya Ranjan Mohanty
Ranch Hand

Joined: Mar 07, 2010
Posts: 44
Ya it is from the book K&B..

Thanks a lot Nicola. You again helped me.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

Please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Yes you save memory on the heap. you have one object instead of two.

the values are automatically unboxed so you are comparing two primitive constants and not two objects.

When you write:



You have one Integer object with value 10 referenced by i1 and i2.


Different is the case of



where you force i3 and i4 to reference two different objects on the heap memory.

It's quite the same thing it happens with String literals.

If you type



s1==s2 returns true because there is 1 object on the heap "hallo" referenced by s1 and s2 and by the String Literal Pool
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in equals and == . Please help.
 
Similar Threads
KB 235
wrappers
wrapper classes
Wrapper comparison.
Boxing, == and equals