Q. What is the output of the following code snippet?

Code:
String s1 = “Hello”;
String s2 = new String(“Hello”);
System.out.println(s1 == s2);
  • (A) True
  • (B) False
  • (C) Error
  • (D) None of these
πŸ’¬ Discuss
βœ… Correct Answer: (B) False
Explanation: In this case, s1 is a string literal and s2 is a string object created using the new keyworD) When a string object is created using the new keyword, a new object is created in memory even if the value is the same as an existing string. Therefore, s1 and s2 do not refer to the same object in memory, so s1 == s2 returns false.
Explanation by: Mayank
In this case, s1 is a string literal and s2 is a string object created using the new keyworD) When a string object is created using the new keyword, a new object is created in memory even if the value is the same as an existing string. Therefore, s1 and s2 do not refer to the same object in memory, so s1 == s2 returns false.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
261
Total Visits
πŸ“½οΈ
3 y ago
Published
πŸŽ–οΈ
Mayank
Publisher
πŸ“ˆ
99%
Success Rate