• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

various Short.equals( short )

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something interesting to look at if you have a minute. This is the output of an actual java program with conditionals.


C:\TEMP>java Dump
Integer I1 = 127
Integer I2 = 128

Short S1 = 127
Short S2 = 128

int i2 = 128

short s1 = 127
short s2 = 128

S1.equals(127) is false
S1.equals((short) 127 ) is true
S1.equals( s1 ) is true

S2.equals(128) is false
S2.equals((short) 128 ) is true
S2.equals( s2 ) is true

S2.equals( I2 ) is false
I2.equals( S2 ) is false

I1.equals((byte) 127 ) is false
I1.equals( 127 ) is true
I2.equals( i2 ) is true

C:\TEMP>
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot see anything strange...
 
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
The following may make what I've done a little clearer.

C:\TEMP>java Dump
Byte B1 = new Byte((byte)127);
Byte B2 = 127;

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

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

int i2 = 128;

short s1 = 127;
short s2 = 128;

B1.equals(127) is false
B2.equals(127) is false
B2.equals((byte)127) is true

S1.equals(127) is false
S1.equals((short) 127 ) is true
S1.equals( s1 ) is true

S2.equals(128) is false
S2.equals((short) 128 ) is true
S2.equals( s2 ) is true

S2.equals( I2 ) is false
I2.equals( S2 ) is false

I1.equals((byte) 127 ) is false
I1.equals( 127 ) is true
I2.equals( i2 ) is true

C:\TEMP>
 
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
It isn't that the results are "strange", Lucas. It's that the results are interesting. I don't think that I could have predicted these results before hand. That's why I'm still studying for the SCJP. -Harry
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so a few clues for you.
Remember the concept of equals method.
public boolean equals(Object o);
It should be checked if 2 classes are in the same inheritance tree.

From Integer class:

So new Integer(10).equals((short)10) will be false, because short is boxed to Short and Short IS-NOT-A(n) Integer.

Moreover between -128 and 127 Wrapper classes are buffered (but it is not requested for Long), when you instantiate the objects without "new" operator, so "==" will return true, because they are in the same place in memory.
 
I knew that guy would be trouble! Thanks tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic