File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boxing ==,and equals()

 
aslika bahini
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How this works?

1 Integer i1=1000;
2 Integer i2=1000;
3 if(i1!=i2)
4 System.out.println("different objects");
5 if(i1.equals(i2))
6 System.out.println("meaningfully equal");

output
different objects
meaningfully equal

i1 & i2 refer to same object in order to save memory. when it refers to same object how come line 3 is true and prints o/p different object

regards
samura
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"i1 & i2 refer to same object in order to save memory"

What makes you think that?
 
Vishal K Patel
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here, if We will assign the value between -128 to 127 then and only then,
Both reference variable will refer to the same object as you are saying.

Here you are assigning 1000, so it refers to the different object.

Try it with,
 
Ram Reddy
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,

can any one please clarify this.

for integers 1000 & 50 both are in acceptable ranges.
 
Musou Dee
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess there is a confusion between int and the wrapperclass Integer.

If you do:

int i=100;
int j=100;
System.out.println(i==j);

will return a true since the value are compared and they are the same.
However,

Integer i= New Integer(100);
Integer j= New Integer(100);

will define two different Integer class in which case they are different, so the comparsion will returen false.

BTW, since Integer is a wrapperclass, you cannot directly write Integer i=500.

/D
 
manmit roy
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1 Integer i1=1000;
2 Integer i2=1000;

3 if(i1!=i2)
4 System.out.println("different objects");



result : true, because its reference comparison. both are different.



5 if(i1.equals(i2))
6 System.out.println("meaningfully equal");



result : true, because its value comparison. both having same value 1000



hope you got ...
[ March 08, 2007: Message edited by: meet roy ]
 
Milan Jagatiya
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah meet is correct....


1 Integer i1=1000;
2 Integer i2=1000;

both having differet reference but same value...
try by adding line
i1=i2

now both have same reference and value. and the result of your code is

meaningfully equal
 
Jesse Custer
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code generates output: 12

Explanation:
  • x==y => true, because it's the value of the primitive that is compared
  • a==b => true, the reference to the Integer object is compared, but because the Integer value is in byte range, they refer to the same object
  • a==c => false, the object references are compared, and c refers to different object because of declaration despite being in byte range
  • d==e => false, the object references are compared and are different because out of byte range

  •  
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic