📖 Scenario: Imagine you are building a simple program to manage names of people in a list. You want to understand how Java handles text data, especially strings, because strings are used everywhere in programs.
🎯 Goal: You will create a small Java program that shows how strings are created, compared, and why they behave differently from other objects.
📋 What You'll Learn
Create two string variables with the same text using string literals
Create a third string variable using the new keyword with the same text
Compare the strings using == and equals() methods
Print the results of the comparisons
💡 Why This Matters
🌍 Real World
Understanding string behavior helps avoid bugs when comparing text in Java programs, such as user input validation or searching.
💼 Career
Java developers frequently work with strings, so knowing how string objects behave is essential for writing correct and efficient code.
Progress0 / 4 steps
1
Create two string variables using string literals
Create two string variables called str1 and str2 and assign both the exact value "hello" using string literals.
Java
Hint
Use double quotes to create string literals in Java, like "hello".
2
Create a third string variable using the new keyword
Add a third string variable called str3 and assign it the exact value "hello" using the new String("hello") constructor.
Java
Hint
Use new String("hello") to create a new String object explicitly.
3
Compare the strings using == and equals()
Write code to compare str1 and str2 using == and store the result in a boolean variable called compareLiteral. Then compare str1 and str3 using == and store the result in compareNew. Finally, compare str1 and str3 using equals() and store the result in compareEquals.
Java
Hint
Use == to compare references and equals() to compare string content.
4
Print the results of the comparisons
Add System.out.println statements to print the values of compareLiteral, compareNew, and compareEquals each on a new line.
Java
Hint
Use System.out.println(variableName); to print each boolean value.
Practice
(1/5)
1. Why are strings considered special objects in Java compared to other objects?
easy
A. Because Java stores string literals in a special pool to save memory
B. Because strings can only store numbers
C. Because strings are primitive data types
D. Because strings cannot be compared using methods
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 A
Quick Check:
String pool = special memory reuse [OK]
Hint: Remember: string literals share memory in a pool [OK]
Common Mistakes:
Thinking strings are primitive types
Believing strings only store numbers
Assuming strings cannot be compared
2. Which of the following is the correct way to create a string literal in Java?
easy
A. String s = new String('hello');
B. String s = "hello";
C. String s = 'hello';
D. String s = hello;
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 B
Quick Check:
Strings use double quotes "" [OK]
Hint: Use double quotes for strings, single for chars [OK]
Common Mistakes:
Using single quotes for strings
Omitting quotes around text
Confusing char and string syntax
3. What will be the output of the following Java code?
String a = "hello"; String b = "hello"; System.out.println(a == b);
medium
A. false
B. Runtime exception
C. Compilation error
D. true
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 D
Quick Check:
Same literal references = true [OK]
Hint: == true if both strings share the same pool object [OK]
Common Mistakes:
Assuming '==' compares string content
Expecting false because of separate variables
Confusing '==' with equals() method
4. Identify the error in the following code snippet:
String s1 = new String("test"); String s2 = new String("test"); if (s1 == s2) { System.out.println("Equal"); } else { System.out.println("Not Equal"); }
medium
A. Using '==' to compare string contents instead of equals()
B. Missing semicolon after string declarations
C. Incorrect string initialization syntax
D. Cannot create strings with new keyword
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 A
Quick Check:
Use equals() for string content comparison [OK]
Hint: Use equals() to compare string text, not '==' [OK]