This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equal or not ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: equal or not ?
 
Similar Threads
toString( ) method
* Byte == comparision
why the outputis false....
Interesting Question
comparison