File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equal or not ? 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 "equal or not ?" Watch "equal or not ?" New topic
Author

equal or not ?

Stefan Koeltze
Greenhorn

Joined: Feb 08, 2002
Posts: 14
Hi all!
The following:
Byte b1 = new Byte("127");
if(b1.toString() == b1.toString())
System.out.println("True");
else
System.out.println("False");
results to "False".
I thought the toString method would twice return a reference to the String "127" from the JVM Stringcontainer...and so this would result to "True".
So what happens there? Are there returned two new String instances created with ... what?
Thanks...!
Ciao Stefano
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
because toString of class Byte invokes toString of class Integer and the latter create the String using the keyword "new" which means that every time toString is invoked a new String instance is created. The content may be the same but the references are different and == compares the references not the content.
HIH
[ February 12, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: equal or not ?