Posted By:
Tim_Frith
Posted On:
Tuesday, March 31, 2009 01:47 PM
The second way you declared the strings uses the string pool. As an optimization, before instantiating a new string Java checks if that string is already in use and if so, just refers to it.
Using new String() forces Java to always instantiate a new object.
Of course, you shouldn't be comparing strings with == anyways. You should be doing st1.equals(st2).