0

I am beginner in Java and i have a question while trying to learn Strings . In the below program

String st1="Test";
String st2="TEST";
String st3= st1.toUpperCase();

if(st3 == st2)
{
    System.out.println("Equals");

}
else
    System.out.println("Not Equals");

O/P for the above program displays as "Not Equals" where as i was expecting "Equals" Since "TEST" already exist in String Pool, st3 should have pointed to already existing String rather than creating a new String. Kindly explain the behavior. Thanks in advance

Shyam
  • 181
  • 2
  • 2
  • toUpperCase return new String object, not the one you expected from String pool `return new String(result, 0, len + resultOffset);` – An Do Apr 02 '16 at 07:08
  • Use `.equals()` to compare strings – Debosmit Ray Apr 02 '16 at 07:42
  • “==” operator compares the objects’ location(s) in memory, where as equals() method is designed to compare the contents of 2 objects, and not their location in memory. In this case if you use obj.equals() you get expected result. – Oliver.Oakley Apr 04 '16 at 16:56

0 Answers0