Yesterday this Question asked by Interviewer what is the output for that line.
public class StringTest {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = s1+s2;
String s4 = s1+s2;
//check s1 == s2
System.out.println(s1==s2);
//check s3 == s4
System.out.println(s3==s4);
}
}
when i looked at this question then thinking how the easy question asked by interviewer. I told him output s1==s2
and s3==s4
will return true
and true
and i was very confident.
Suddenly he said no thats the wrong output then i thinking he might be joking or trying to loosing my confidence, but till the end he said its wrong.
When i checked the output was true
and false
.
I also thinking how it is possible please solve my confusion by giving a appropriate answer.
thanks in advance.