Which statement about Java String objects is true?
2025
Which statement about Java String objects is true?
Answer: C. Once a String object is created, its contents cannot be changed. — ConceptAn immutable object keeps the same observable state after construction. Java String represents a fixed character sequence; operations that appear to…
- A.
String is implemented as a primitive data type.
- B.
A String object must be explicitly destroyed when it is no longer in use.
- C.
Once a String object is created, its contents cannot be changed.
- D.
A String object can be copied by using the == operator.
Attempted by 219 students.
Show answer & explanation
Correct answer: C
Concept
An immutable object keeps the same observable state after construction. Java String represents a fixed character sequence; operations that appear to modify a String create and return a different String.
Application
Consider String s = "gate"; then String t = s.concat("way").
s refers to the String value "gate".
concat creates a new String value "gateway", and t refers to that new object.
s still refers to "gate"; the contents of the original String object were not changed.
Cross-check and contrast
String is a class, not a primitive type.
An unreachable String object becomes eligible for garbage collection; Java code does not explicitly destroy it, and collection timing is not guaranteed.
The == operator compares references for String operands; it does not copy a String.
Therefore, once a String object is created, its contents cannot be changed.