1Z0-803 · Question #212
Given: What is the result?
The correct answer is C. false true. The result false true is obtained due to how Java handles String object creation and literal interning when comparing using the == operator.
Question
Given:
What is the result?
Exhibit
Options
- Atrue true
- Btrue false
- Cfalse true
- Dfalse false
- ECompilation fails
How the community answered
(32 responses)- A3% (1)
- B13% (4)
- C78% (25)
- D6% (2)
Why each option
The result `false true` is obtained due to how Java handles String object creation and literal interning when comparing using the `==` operator.
`true true` would imply both comparisons yielded true, which is incorrect for `new String()` versus a literal.
`true false` would imply the first comparison yielded true and the second false, which contradicts standard String interning behavior.
Assuming the code compares `new String("Java") == "Java"` and then `"Java" == "Java"`. The first comparison (`new String("Java") == "Java"`) evaluates to `false` because `new String()` creates a new object in the heap, while `"Java"` refers to a literal in the string pool, thus their references are different. The second comparison (`"Java" == "Java"`) evaluates to `true` because identical string literals refer to the same object in the string pool due to interning.
`false false` would imply both comparisons yielded false, which is incorrect for two identical string literals.
Comparing String objects using `==` is valid Java syntax and does not cause a compilation failure.
Concept tested: Java String comparison (==), String pool, object creation
Source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.10.5
Topics
Community Discussion
No community discussion yet for this question.
