• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

"==" on Wrapper Class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The concept is clear that, if two Integer objects has the same value, in the range of -128 to 127, they both refer to the same object.
For Example:
class Wrapper
{
public static void main(String[] args)
{
Integer i1=10;
Integer i2=10;
System.out.println(i1==i2);// prints true
}
}


But please explain why the following code behaviour changes like this.

class Wrapper
{
public static void main(String[] args)
{
Integer i1=new Integer(10);
Integer i2=10;
System.out.println(i1==i2);//Why does this prints false ?

//i1=10; System.out.println(i1==i2); // when this line uncommented, why it prints true?

}
}
Please explain, I think i am missing some logic here.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Case #1

1- i1 is assigned a reference to the Object created using
"new" operator.
2- primitive 10 is autoboxed and Integer object is referenced to the
ref variable i2

So you are comparing two different references and hence result false.

Case #2


//i1=10; System.out.println(i1==i2); // when this line uncommented, why it prints true?



You did the same what you talk about very first, (for some range of values
the autoboxed ref will yield the value true)

i1=10;
i2=20;
(i1==i2); //true


Thanks,
 
Sowjanya Chowdary
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changed name according to naming policy.
Thank you.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To make it clear:


if two Integer objects has the same value, in the range of -128 to 127, they both refer to the same object.


is not always correct. It is correct for Integer-Objects that were created using auto-boxing, but not for Integers created with the new-Operator
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic