nerdexam
Oracle

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.

Working with Java Data Types

Question

Given:

What is the result?

Exhibit

1Z0-803 question #212 exhibit

Options

  • Atrue true
  • Btrue false
  • Cfalse true
  • Dfalse false
  • ECompilation fails

How the community answered

(32 responses)
  • A
    3% (1)
  • B
    13% (4)
  • C
    78% (25)
  • D
    6% (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.

Atrue true

`true true` would imply both comparisons yielded true, which is incorrect for `new String()` versus a literal.

Btrue false

`true false` would imply the first comparison yielded true and the second false, which contradicts standard String interning behavior.

Cfalse trueCorrect

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.

Dfalse false

`false false` would imply both comparisons yielded false, which is incorrect for two identical string literals.

ECompilation fails

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

#String comparison#object equality#reference equality

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice