Demonstrating string comparison with Java

The compiler does some optimizations in your case so that s1 and s2 are really the same object. You can work around that by using

String s1 = new String( "Hello" );
String s2 = new String( "Hello" );

Then you have two distinct objects with the same text content.

Leave a Comment