• 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

Help me with this Wrapper class code

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CASE 1:
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");
OOUTPUT:
different objects
meaningfully equal

CASE2:
Integer i1 = 10;
Integer i2 = 10;
if(i1 == i2) System.out.println("same object");
if(i1.equals(i2)) System.out.println("meaningfully equal");
OOUTPUT:
same object
meaningfully equal
I ran this code in jdk1.5_06. How is the 1st if statement true in both the cases.
Appretiate your time...
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to JLS 5.1.7 - Boxing Converson...

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.


In case 1, the int values are outside the range of -128 and 127, so there is no guarantee that they will box to the same wrapper instance. The != comparison is true.

In case 2, the int value are within the range of -128 and 127, so they box to the same wrapper instance. Here, the == comparison is true.
[ October 08, 2007: Message edited by: marc weber ]
 
nirmal Rchavan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Marc..
but i stilll have doubts with this.
Character c1 = new Character('\u0070');
Character c2 = new Character('\u0071');
if(c1 == c2) System.out.println("different objects");
OUTPUT: EMPTY

Character c1 = new Character('\u0070');
Character c2 = new Character('\u0071');

if(c1 != c2) System.out.println("different objects");
OUTPUT: different objects
Here we have character with in the range '\u0000' to '\u007f', so it must always return true for if statement right?...
Thanks for your help.
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Character c1 = new Character('\u0070');
Character c2 = new Character('\u0071');
if(c1 == c2) System.out.println("different objects");
OUTPUT: EMPTY

Here c1 and c2 are different objects which point to different characters so the == comparison will result in false.So c1 != c2 the if statement below will be executed.

if(c1 != c2) System.out.println("different objects");

But if you do like this
Character c1 = new Character('\u0070');
Character c2 = new Character('\u0070');
Then c1 and c2 will point to the same character and so the == comparison will result in true. So c1==c2
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if(variable'value in range -127- 127){
put into the constant pool, and the other referece call share the value;
} else {
variable's value will malloc the memory;
}

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nirmal Rchavan:
...Here we have character with in the range '\u0000' to '\u007f', so it must always return true for if statement right?...


No, because this only applies to autoboxing. If you create an instance with "new," you will get a new object.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vanlalhmangaiha khiangte:
...if you do like this
Character c1 = new Character('\u0070');
Character c2 = new Character('\u0070');
Then c1 and c2 will point to the same character and so the == comparison will result in true. So c1==c2


No. You've created separate objects using "new," so the == comparison will return false.
 
nirmal Rchavan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic