This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Byte and String 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 "Byte and String" Watch "Byte and String" New topic

Byte and String

Mylene Morato

Joined: Feb 10, 2009
Posts: 10

Hello friends,

I have this code:

The output is false

Could anybod help me ?
Why the output is false?


Henry Wong

Joined: Sep 28, 2004
Posts: 20019

Could anybod help me ?
Why the output is false?

Well, simply, the toString() method generates a new string every time. The value in the strings are the same, but they are different strings. And since you are only checking the references, it is false.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
b1.toString().equals(b1.toString()) would be true, in other words

SCJP 88% | SCWCD 84%
I agree. Here's the link:
subject: Byte and String
It's not a secret anymore!