• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

S2.equals(7) is false, but (S2 == 7) is true. Short S2 = new Short((short)7); Why?

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(original thread here)

Hi,

I did a little more investigation using Java 6. It appears that a Wrapper Object is == to a primitive, if the value of the Wrapper Object is exactly equal to the value of the primitive. Please see the output of a test program that I wrote:

The fact that there is a pool of bytes in the range of 127 to -128 in the JVM doesn't seem to come into play in these comparisons. The type of the Wrapper Object doesn't seem to make any difference either; it is strictly a value based comparison.

Regards,

Harry

The output of the program is listed below:

C:\TEMP>java Dumper
Byte B1 = new Byte((byte)127);
Byte B2 = 127;
Byte B3 = 129; \\ compiler error
Byte B3 = new Byte((byte) 129); \\ == -127

byte b2 = 127;
byte b3 = -127; \\ == (byte)129

Integer I1 = new Integer( 127 );
Integer I2 = new Integer( 128 );
Integer I3 = new Integer( 32769 );

Short S1 = new Short((short)127);
Short S2 = new Short((short)128);
Short S3 = new Short((short)32769);

int i2 = 128;
int i3 = 32769;

short s1 = 127;
short s2 = 128;

long l2 = 128;

B1 == 127 is true
B2 == 127 is true
B3 == -127 is true
B3 == b3 is true

S1 == 127 is true
S2 == 128 is true
S1 == s1 is true

Short S3 = new Short((short)32769);
S3.shortValue() = -32767

S3 == i3 is false.
S3 == -32767 is true
S3 == (long)-32767 is true
S2 == l2 is true

I1 == 127 is true
I2 == s2 is true
I3 == 32769 is true

S2 == I2 \\ compiler error
\\ incomparable types

C:\TEMP>
 
Ranch Hand
Posts: 37
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S2.equals(7) is false, but (S2 == 7) is true. Short S2 = new Short((short)7); Why?


In S2.equals(7) first argument is type of Short and second becomes Integer after auto boxing, so equals fails as they belongs to different Object hierarchy.
(S2 == 7) is true because == comparison looks into bit pattern and both points to same value 7 hence tru.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i am confused about the question. What exactly is it that you want to ask here? Also please use code tags.

following from your code are examples for unboxing

Integer wrapper can never be compared with Short wrapper as they are 2 different branches of Number class.

 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

This article has some explanation for your question:
http://today.java.net/pub/a/today/2005/03/24/autoboxing.html
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice article for autoboxing.
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitish and Krishna,

This discussion thread is a continuation of a previous thread from day before yesterday. I guess my question is this, "Are the various Wrapper Objects unboxed to primitives of the same type, and then compared to the corresponding literal or primitive on the other side of the == operator without any further conversions of either operand?"

Nice article, by the way.

Regards,
Harry
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This discussion thread is a continuation of a previous thread from day before yesterday


Next time, to avoid confusion, that would be cool if you could avoid splitting the discussion in different threads
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic