• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Boxing ==,and equals()

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"i1 & i2 refer to same object in order to save memory"

What makes you think that?
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,

can any one please clarify this.

for integers 1000 & 50 both are in acceptable ranges.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

  •  
    "How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic