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 Wrapper-Method-Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper-Method-Q" Watch "Wrapper-Method-Q" New topic


Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

why b1.toString() == b2.toString() is evaluating to false.

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
why b1.toString() == b2.toString() is evaluating to false.

When a Java expression creates a new String at run time, a new object is created. The = operator checks whether these are the same object. The equals() method would compare the characters in these objects and yield true.

"127" == "127" would be true because the String object "127" is created once at compile time in the String Literal Pool, so you are comparing an object to itself. When you use toString() to create a String, you get a unique object every time.

Mike Gershman
SCJP 1.4, SCWCD in process
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Wrapper-Method-Q
jQuery in Action, 3rd edition