• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

== and equals()

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Source: enthuware
I know this topic has been discussed severl times code below is my confusion.

1. Integer i1=1;
2. Integer i2=new Integer(1);
3. int i3=1;
4. System.out.println(i1==i2);//prints false
5. System.out.println(i1==i3);//prints true;

I am not able to understand the result. I know when == used then wrapper will be unwrapped and primitive values will be used in comparision. so line 5 is ok(please clarify if i m wrong) but line 4 is printing false as i know that to save memory Integer(-127 to 128) will retrun true if there primitive values are save. if this is true then why line 4 is printing false.

REgards,
Nitin
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 4 is testing whether two objects refered to by i1 and i2 refer to the same object which is not true so it prints false.
And two objects are considered equal only they are created using the short hand new to java 5, as in Integer i = 5;
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are confusing with what had been sayed on this forum a few days ago:

The specification states that "Character from \u0000 to \u007f, Boolean, Short and Integer from -128 to 127" are cached, and hence, will be "==", via boxing when their primitives values are the same.


You obtain i1 via boxing and i2 by calling the constructor. So you cannot apply that rule. The references of the two objects are different. And then
System.out.println(i1==i2); will printed false.
[ August 15, 2007: Message edited by: Collins Mbianda ]
 
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
The exact wording is...

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.


Ref: JLS - 5.1.7

Note the wording, "two boxing conversions."
 
Collins Mbianda
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc for your precision.
I hope we will not confuse again on the stuff.

 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to all.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic