• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equal or not ?

 
Stefan Koeltze
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic