String intern puzzles

Posted by Yob on Stack Overflow See other posts from Stack Overflow or by Yob
Published on 2012-09-05T09:27:39Z Indexed on 2012/09/05 9:38 UTC
Read the original article Hit count: 190

Filed under:
|

On this blog I found interesting String puzzles:

--- Quote ---

String te = "te", st = "st";
//"test".length();
String username = te + st;
username.intern();
System.out.println("String object the same is: " 
   + (username == "test"));

prints

String object the same is: true

but uncomment the "test".length(); line and it prints

String object the same is: false

--- EoQ ---

Being honest I don't understand why the outputs are different. Could you please explain me what's the cause of such behaviour?

© Stack Overflow or respective owner

Related posts about java

Related posts about string