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");

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
Зареєструйся Зараз
або Підпишись на майбутні тести