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.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Java
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести