This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

(Solved!) Boxing tricks

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey.

Well, checking all chapters before the friday's exam made me encounter this tricky thing about boxing.

Byte b = 127;
Byte b1 = 127;
b == b1; // this is true

Integer i = 128;
Integer i1 = 128;
i == i1; // this is false, because it's larger than 127. Ok, that's burnt in.

Now. Ha! Look:
Short s = 10;
Short s2 = new Short((short)10);
s == s2; // this is false. Eeek.
s.equals(s2); // this is true >_<

Short s3 = new Short("10");
s2 == s3; // false too. Surprised.

Well, is there any logic explanation to this, or it's just to be accepted as-is and remember it until the exam is done?

thanks.

[ October 24, 2007: Message edited by: Jem Hobstad ]
[ October 24, 2007: Message edited by: Jem Hobstad ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jem Hobstad:
Hey.

Well, checking all chapters before the friday's exam made me encounter this tricky thing about boxing.

Byte b = 127;
Byte b1 = 127;
b == b1; // this is true

Integer i = 128;
Integer i1 = 128;
i == i1; // this is false, because it's larger than 127. Ok, that's burnt in.

Now. Ha! Look:
Short s = 10;
Short s2 = new Short((short)10);
s == s2; // this is false. Eeek.
s.equals(s2); // this is true >_<

Short s3 = new Short("10");
s2 == s3; // false too. Surprised.

Well, is there any logic explanation to this, or it's just to be accepted as-is and remember it until the exam is done?

thanks.

[ October 24, 2007: Message edited by: Jem Hobstad ]



Yes, there is a logical explanation.

When used between object references, == tests whether or not those references refer to the same object.

The equals method tests the contents of the object.
 
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 Jem Hobstad:
...this is false, because it's larger than 127. Ok, that's burnt in...


Remember, this behavior of referencing the same wrapper instance for values withing a byte range applies only to boxed instances. Whenever you use "new" to construct a wrapper instance, you are creating a brand new separate instance, regardless of the value.
 
Jem Hobstad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, but my point is


They hold the same value. They all are instances of Byte, but strangely, only two references point to the same object.
In the book, it states (p236) taht two wrapper objects will always be == when their primitive values are the same:
- Boolean
- Byte
- (..)
- Short and Integer from -128 to 127.
According to that, the == betweeb any of these four Byte instances should return true ...
 
Jem Hobstad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah sorry didn't see your post Marc

ok that solves my issue then!
cheers.
 
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 Jem Hobstad:
... In the book, it states (p236) that two wrapper objects will always be == when...


I don't have the book with me, but I think this is under the topic of autoboxing, and only applies to that context. That is, "two boxed wrapper objects..."

(I know you got it now, but I just want to stress this for anyone else reading this, because there's a good chance it will be on the exam. )
 
And then the flying monkeys attacked. My only defense was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic