Загрузка...

Day-139: Java String Puzzle: Can You Guess the Output? #daily #java #quiz #shorts

Welcome to daily coding challenge!!!

The correct answer to the code shown in video is:
A. one
C. three
D. four

Explanation:
A, C, D. The variable s is assigned the string "Hello", which is a string literal and thus stored in the String Pool.
The variable t is created using new String(s), which explicitly creates a new String object on the heap, separate from the string pool.
When "Hello".equals(s) is evaluated, it returns true because equals() checks the string content, so "one" is printed.
The comparison t == s checks for reference equality, and since t refers to a new object while s refers to the interned literal, the result is false and nothing is printed.
The expression t.intern() == s compares references and returns true because intern() returns the reference from the string pool, which matches s, so "three" is printed.
The comparison "Hello" == s is also true because both point to the interned string, so "four" is printed.
However, "Hello".intern() == t is false because although "Hello".intern() points to the pool, t is still a different object, so nothing is printed.
Hence, the output includes one, three, and four, making the correct options A, C, and D.

The code, options and answer is verified in Eclipse IDE with Java 21. Incase you find it wrong then feel free to DM me over LinkedIn/Instagram. Just ensure you share screenshot of question and answer which you are getting in your IDE.

Subscribe the channel for regular Java updates.
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
Social Media Handles:
• Website: https://abhishekvermaa10.github.io
• YouTube: https://www.youtube.com/@abhishekvermaa10
• Instagram: https://www.instagram.com/abhishekvermaa10
• LinkedIn: https://www.linkedin.com/in/abhishekvermaa10
• GitHub: https://github.com/abhishekvermaa10
• TopMate: https://topmate.io/abhishekvermaa10
• X: https://x.com/ytabhishekverma
• Telegram: https://t.me/ScaleUpIndiayt
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
Copyright © 2025 Abhishek Verma - All Rights Reserved

Видео Day-139: Java String Puzzle: Can You Guess the Output? #daily #java #quiz #shorts канала Abhishek Verma
Страницу в закладки Мои закладки
Все заметки Новая заметка Страницу в заметки