Now I'm new to Java, but I was under the impression that when using == to compare two Strings, java checked to see whether the objects where exactly the same rather than doing the character by character check that equals() performs.
However, when I was just fiddling about, I wrote this:
This however, prints the value True which surprised me as they are not the same String object as far as I'm concerned.
I must be doing something very silly here, but I just can't understand what. Please advise a newbie :)
However, when I was just fiddling about, I wrote this:
Code:
public class Test
{
private String testString;
private String compareString;
public Test()
{
testString = "Hello World!";
compareString = "Hello World!";
}
public void compareString()
{
if(testString == compareString)
{
System.out.println("True");
}
else
{
System.out.println("False");
}
}
}
I must be doing something very silly here, but I just can't understand what. Please advise a newbie :)
Comment