I was in an interview and got a question as below:
String s = "abcde5";
String s1 ="abcde"+"5";
System.out.println(s==s1);
System.out.println(s.equals(s1));
String s4 = "abcd5";
String s3 = "abcd"+s4.length();
System.out.println(s3);
System.out.println(s3.equals(s4));
System.out.println(s3==s4);
I thought that the output will always yield false for "==" as using the + operator will create a new string same as doing the s4.length. but the expected answer is different. Can anyone please let me know why s==s1 gives true, and s3==s4 yields false.
expected answer is: true true abcd5 true false