Java Text Blocks and String Equality
Given the code fragment: What is the result? - 
Community Votes
100% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
The common trap is assuming text blocks are trimmed like standard strings; they actually include a newline at the end if content follows the closing quotes on a new line.
This question tests the behavior of Java text blocks regarding implicit newlines and string equality. The correct answer is D because text blocks preserve trailing newlines unless explicitly removed, causing mismatches with standard strings.
Many learners choose C, incorrectly assuming that text blocks are automatically trimmed or that interning affects the structural comparison of the block's content.
Community Discussion (7 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
The code compares a standard stringmyStr with two text blocks. myTextBlk1 has its closing delimiter on the same line as the content, so it does not add an implicit newline, making it equal to myStr. myTextBlk2 has the closing delimiter on a new line, which adds an implicit newline character to the content, making it unequal to myStr. The final comparison involves String.intern, but since the objects are already distinct in value, interning does not make them equal if their values differ. Thus, the output is true:false:false:false.Why the Other Options Are Wrong
Option A assumes all comparisons are true, ignoring the newline difference. Option B assumes the second comparison is false but the third is true, failing to recognize thatmyTextBlk2's value differs from myTextBlk1. Option C incorrectly assumes that intern forces equality or that text blocks are always trimmed.Community Comment Notes
Most users correctly identified D by testing the code. As one user noted, "The first comparison is true because myStr and myTextBlk1 are equal... The second and third comparisons are false because myTextBlk2 includes a newline character." Another user confirmed, "D tested correct" after running the snippet.Exam Strategy
When working with text blocks, always check the position of the closing triple quotes. If they are on a new line, a newline character is included in the string value.
Frequently Asked Questions
Why does myTextBlk1 equal myStr but myTextBlk2 does not?
myTextBlk1's closing quotes are on the same line as the text, so no newline is added. myTextBlk2's closing quotes are on a new line, adding an implicit newline.
Does String.intern() make different text blocks equal?
No. intern() ensures that strings with identical values share the same reference, but it does not change the value of the string itself.