This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Getting MEAN with Mongo, Express, Angular, and Node and have Simon Holmes on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString()" Watch "toString()" New topic


Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
The answer says false. I thought since toString() is called on the sameObject, it will not create a new String. Will it again create a new String for each toString() invocation?
1: Byte b1 = new Byte("127");
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");

Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

right... each b1.toString() method creates a new String object. but what happens if you call

if( b1.toString().equals( b1.toString() ) )
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
output true for equals(), Obviously since Boolean wrapper class overrides equals().
Consider Paul's rocket mass heater.
subject: toString()