• 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

wrapper objects. equals and ==

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any links to material on how wrapper objects behave with equals and == ?
Pulling my hair out on when these reteurn true or false - any input help appreciated
 
Ranch Hand
Posts: 128
MS IE Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals()
Wrappers return true if their primitive values are equal.

==
Integer, Long and Character wrappers return true if their primitive values are <128. (Reason: For saving memory wrapper references with values less than 128 are made to refer to the same Object on the heap)
i.e If

now (i1==i2) will be true.

[ November 06, 2006: Message edited by: Allen Sylvester ]
[ November 06, 2006: Message edited by: Allen Sylvester ]
 
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
See JLS - 5.1.7 Boxing Conversion...

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.


Also note the "Discussion" that follows this quote.
 
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 Allen Sylvester:
...if their primitive values are <128...


Careful with this. It's the inclusive range -128 through 127 (not just <128) for which this behavior is guaranteed. (Outside of this range, I interpret the JLS to say that there are no guarantees. See "Discussion" under above link.)
[ November 06, 2006: Message edited by: marc weber ]
 
Peter Gade Christensen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So its just for


what happens when

I am assuming equals returns true (meaningfully equivalent) and == is false

and when

guessing
equals = true?
== false?


Will the results be the same if I use Number i1 and Number i2?

And if I extract the primitive using
(say Integer i1 = 10; )
a) Integer i2 = i1.intvalue() or
b) wrap it using Integer i2 = Integer.valueof("10")
would these create new objects or point to the existing objects?

I can verify writing the code but wanted to see if there is a way I can figure out using reasoning and just not commit everything to memory !

Many thanks !
[ November 06, 2006: Message edited by: Peter Gade Christensen ]
 
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
Reference equality (==) of objects wrapping values in the byte range (-128 through 127) is guaranteed only for boxing conversions.

When you use "new" to create a wrapper object, you are not using boxing. Similarly, Integer.valueOf(String s) creates a new object without boxing. On the other hand, intValue() returns an int, and assigning this value to an Integer variable would invoke boxing.
 
Peter Gade Christensen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Marc!!!

You say

Reference equality (==) of objects wrapping values in the byte range (-128 through 127) is guaranteed only for boxing conversions.



I am assuming both the objects being compared need to be boxed? Correct?

some scenarios below ...

#1.So if you are comparing an object created using New with one created using intvalue (which is boxed per your note) one is boxed and one is not so I guess i1==i2 is a false?
i1 equals i2 is true of course (meaningfully equal)



#2. whereas below both are autoboxed so true and true




#3. and then in this scenario below 2 objects are created
and so == is false
and equals is true ?



#4. one is boxed and one is created new using valueOf() so
== is false
and equals is true



Am I on the right track?? Many thanks for responding again Marc!
[ November 06, 2006: Message edited by: Peter Gade Christensen ]
 
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 Peter Gade Christensen:
... Am I on the right track?? ...


Absolutely! But it's still a good idea to verify your understanding and reinforce the ideas by writing test code.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ranchers,


Originally posted by Allen
Integer, Long and Character wrappers return true if their primitive values are <128. (Reason: For saving memory wrapper references with values less than 128 are made to refer to the same Object on the heap)



It is Byte ,Character and Integer.Long wrapper returns false for eatch of their each primitive values.
[ November 06, 2006: Message edited by: Sanjeev Kumar Singh ]
 
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 Sanjeev Kumar Singh:
...Long wrapper returns false for eatch of their each primitive values...


Please refer to the JLS quote above, and note in the Discussion that follows, "For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references."

Indeed, I get "true" when I run this.

[ November 06, 2006: Message edited by: marc weber ]
 
I need a new interior decorator. This tiny ad just painted every room in my house purple.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic