What will be the result of the following code execution:

String s1 = "abc";
String s2 = new String("abc");
String s3 = "abc";
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1 == "abc");
Explanation

String literals refer to the same instance of the String class. Therefore s1 == s3 and s1 == "abc" comparisons will result in true.

New instance of the class is created with the new operator, therefore s1 == s2 will result in false.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes