Strings in Java are special because they are used to store text and have unique features that make working with text easier and more efficient.
Why strings are special in Java
String text = "Hello";
Strings are objects, not simple variables.
Java stores strings in a special place called the string pool to save memory.
String greeting = "Hello";
String name = new String("World");
String combined = greeting + " " + name;
This program shows how strings created with quotes share the same memory (string pool), so 'a == b' is true. But a new String object 'c' is different in memory, so 'a == c' is false. However, 'equals()' checks the text inside, so 'a.equals(c)' is true.
public class Main { public static void main(String[] args) { String a = "Java"; String b = "Java"; String c = new String("Java"); System.out.println(a == b); // true System.out.println(a == c); // false System.out.println(a.equals(c)); // true } }
Use equals() to compare string contents, not ==.
Strings are immutable, meaning once created, their text cannot change.
The string pool helps save memory by reusing common strings.
Strings store text and are objects in Java.
Java uses a string pool to reuse strings created with quotes.
Use equals() to compare string values, not ==.
Practice
Solution
Step 1: Understand string storage in Java
Java stores string literals in a special memory area called the string pool to reuse them and save memory.Step 2: Compare with other objects
Other objects do not have this special pool and are stored separately each time they are created.Final Answer:
Because Java stores string literals in a special pool to save memory -> Option AQuick Check:
String pool = special memory reuse [OK]
- Thinking strings are primitive types
- Believing strings only store numbers
- Assuming strings cannot be compared
Solution
Step 1: Identify correct string literal syntax
In Java, string literals must be enclosed in double quotes, like "hello".Step 2: Check other options for errors
String s = new String('hello'); uses single quotes which are for chars, String s = 'hello'; uses single quotes incorrectly, String s = hello; lacks quotes entirely.Final Answer:
String s = "hello"; -> Option BQuick Check:
Strings use double quotes "" [OK]
- Using single quotes for strings
- Omitting quotes around text
- Confusing char and string syntax
String a = "hello";
String b = "hello";
System.out.println(a == b);
Solution
Step 1: Understand string literal pooling
Both variables a and b refer to the same string literal "hello" in the string pool, so they point to the same object.Step 2: Evaluate '==' operator on strings
The '==' operator compares references, and since both refer to the same object, it returns true.Final Answer:
true -> Option DQuick Check:
Same literal references = true [OK]
- Assuming '==' compares string content
- Expecting false because of separate variables
- Confusing '==' with equals() method
String s1 = new String("test");
String s2 = new String("test");
if (s1 == s2) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}Solution
Step 1: Understand '==' vs equals() for strings
'==' compares references, not content. Since s1 and s2 are different objects, '==' returns false.Step 2: Correct way to compare string content
Use s1.equals(s2) to compare the actual text inside the strings.Final Answer:
Using '==' to compare string contents instead of equals() -> Option AQuick Check:
Use equals() for string content comparison [OK]
- Using '==' to compare string values
- Thinking new String() creates same object
- Ignoring string pool behavior
String s1 = "Java";
String s2 = new String("Java");
String s3 = s2.intern();
System.out.println(s1 == s2);
System.out.println(s1 == s3);
Why does this happen?
Solution
Step 1: Analyze s1 and s2 references
s1 is a string literal in the pool, s2 is a new String object outside the pool, so s1 == s2 is false.Step 2: Understand intern() method effect
s3 = s2.intern() returns the reference from the string pool for "Java", which is the same as s1, so s1 == s3 is true.Final Answer:
false true - intern() returns the pooled string reference -> Option CQuick Check:
intern() returns pool reference [OK]
- Assuming new String() returns pooled string
- Thinking '==' compares string content
- Ignoring intern() method purpose
Ready to prove it?
Pass the test and this pattern moves unknown
