File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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().
I agree. Here's the link:
subject: toString()
It's not a secret anymore!