File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in equals and == . Please help.

 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya it is from the book K&B..

Thanks a lot Nicola. You again helped me.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when you post source code.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic