The following prints out "true";
String s = "231";
if(s == "231")
{
System.out.println("true");
}
else
{
System.out.println("false");
}
This is because Strings are not mutable and java will try and save as much space as possible, so it points both to the same memory reference.
However, the following prints out "false":
String s = new String("231");
if(s == "231")
{
System.out.println("true");
}
else
{
System.out.println("false");
}
new
will force it to store the string in a new memory location.
By the way, you should ALWAYS use .equals()
to compare strings (for cases just like this one)