String Interning and the == Operator in Java
Given: String s1 = "Hello"; String s2 = "Hello"; String s3 = new String("Hello"); Which of the following is true?
Quick Answer
The correct answer is that s1 == s2 is true while s1 == s3 is false. This is because Java uses string interning for literals: when you write "Hello" directly, the JVM stores it in the string pool, so both s1 and s2 reference the exact same pooled object, making reference equality with == true. However, the new keyword forces s3 to be a distinct object on the heap, so s1 == s3 compares two different memory addresses and returns false. On the Oracle Java Foundations 1Z0-811 exam, this question tests your understanding of string reference equality and the distinction between the string pool and heap allocation—a classic trap where beginners confuse == with .equals(). Remember: literals are interned, new objects are not. A handy memory tip: "Literals link, new is new"—if you see double quotes, think pool; if you see new, think separate object.
⚠ Common exam trap
Watch out — candidates often confuse == (reference equality) with .equals() (value equality) and assume that all String objects with the same content are the same reference, forgetting that new String() always creates a separate object.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
s1 == s2 is true, s1 == s3 is false
String literals in Java are interned, meaning s1 and s2 both reference the same object from the string pool, so s1 == s2 is true. However, s3 is created using the new keyword, which forces the creation of a new String object on the heap, so s1 == s3 is false because == compares object references, not content.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
s1 == s2 is false, s1 == s3 is true
Why it's wrong here
s1 == s2 is true, not false.
- ✗
s1 == s2 is true, s1 == s3 is true
Why it's wrong here
s1 == s3 is false because s3 is a new object.
- ✗
s1 == s2 is false, s1 == s3 is false
Why it's wrong here
s1 == s2 is true.
- ✓
s1 == s2 is true, s1 == s3 is false
Why this is correct
Correct due to string literal pooling and new String().
Quick reference
AWS S3 Storage Class Comparison
| Storage Class | Min Duration | Retrieval | Use Case |
|---|---|---|---|
| S3 Standard | None | Immediate | Frequently accessed data |
| S3 Standard-IA | 30 days | Immediate | Infrequent access, rapid retrieval |
| S3 One Zone-IA | 30 days | Immediate | Non-critical infrequent data |
| S3 Intelligent-Tiering | None | Immediate–hours | Unknown or changing access patterns |
| S3 Glacier Instant | 90 days | Milliseconds | Archive with instant retrieval |
| S3 Glacier Flexible | 90 days | Minutes–hours | Archive, flexible retrieval |
| S3 Glacier Deep Archive | 180 days | Hours | Long-term compliance archive |
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-811 question from scratch — 481 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
2 more ways this is tested on 1Z0-811
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. What is the output of the following code? String str1 = "Java"; String str2 = new String("Java"); System.out.println(str1 == str2);
easy- A.true
- ✓ B.false
- C.Compilation error
- D.Java
Why B: The == operator compares object references. One string is a literal (stored in the string pool) and the other is created using the 'new' keyword (heap object). They are different references, so the comparison returns false.
Variation 2. Refer to the exhibit. What is the output?
medium- A.Runtime exception
- ✓ B.true
- C.Compilation error
- D.false
Why B: The code uses `==` to compare two `String` objects created with string literals (e.g., `String s1 = "true"; String s2 = "true";`). In Java, string literals are interned, meaning they refer to the same object in the string pool. Therefore, the reference comparison returns `true`. The `==` operator checks reference equality, not content equality, but due to interning, the references are the same. If `new String("true")` were used, the comparison would be `false` because different objects are created.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.