📖 Scenario: You are learning how strings work in Java. Strings are sequences of characters used to store words or sentences. Understanding how to create and compare strings is important for many programs.
🎯 Goal: You will create strings, compare them using == and equals(), and print the results to see how Java handles string memory and equality.
📋 What You'll Learn
Create string variables with exact values
Use == operator to compare string references
Use equals() method to compare string contents
Print the comparison results exactly as specified
💡 Why This Matters
🌍 Real World
Understanding string memory and comparison helps avoid bugs in programs that check user input, passwords, or text data.
💼 Career
Many software jobs require knowledge of how strings work in memory to write efficient and correct code, especially in Java backend development.
Progress0 / 4 steps
1
Create two string variables
Create two string variables called str1 and str2. Set str1 to "hello" and str2 to "hello".
DSA Java
Hint
Use double quotes to create string literals in Java.
2
Create a new string object
Add a new string variable called str3 and set it to a new String object with the value "hello" using new String("hello").
DSA Java
Hint
Use new String("hello") to create a new string object.
3
Compare strings using == operator
Add three boolean variables called compare1, compare2, and compare3. Set compare1 to the result of str1 == str2, compare2 to str1 == str3, and compare3 to str2 == str3.
DSA Java
Hint
Use == to compare if two string variables point to the same object.
4
Print comparison results using equals() and ==
Print the values of compare1, compare2, and compare3 each on a new line. Then print the results of str1.equals(str3) and str2.equals(str3) each on a new line.
DSA Java
Hint
Use System.out.println() to print each value on its own line.
Practice
(1/5)
1. What is the correct way to create a string literal in Java?
easy
A. String s = 'Hello';
B. String s = new String();
C. String s = "Hello";
D. String s = String();
Solution
Step 1: Understand string literal syntax
In Java, string literals are enclosed in double quotes, so "Hello" is correct.
Step 2: Check each option
String s = "Hello"; uses double quotes correctly. String s = new String(); creates an empty string object but not a literal. String s = 'Hello'; uses single quotes which are for characters, not strings. String s = String(); is invalid syntax.
Final Answer:
String s = "Hello"; -> Option C
Quick Check:
String literal uses double quotes = A [OK]
Hint: Use double quotes for string literals in Java [OK]
Common Mistakes:
Using single quotes for strings
Trying to call String() like a function
Confusing new String() with literal
2. Which of the following is the correct syntax to compare two strings s1 and s2 for content equality in Java?
easy
A. if (s1.equals(s2))
B. if (s1 == s2)
C. if (s1 = s2)
D. if (s1.compare(s2))
Solution
Step 1: Understand string comparison in Java
Using == compares references (memory addresses), not content. To compare content, use the equals() method.
Step 2: Evaluate each option
if (s1 == s2) compares references, not content. if (s1.equals(s2)) correctly uses equals() method. if (s1 = s2) uses assignment (=) which is invalid in condition. if (s1.compare(s2)) uses a non-existent method compare().
Final Answer:
if (s1.equals(s2)) -> Option A
Quick Check:
Use equals() for string content comparison = B [OK]
Hint: Use equals() method to compare string contents [OK]
Common Mistakes:
Using == to compare strings
Using assignment operator in if condition
Calling non-existent methods
3. What will be the output of the following Java code?
s1 and s2 are string literals and point to the same object in the string pool, so s1 == s2 is true. s3 is a new String object, so s1 == s3 is false because they reference different objects.
Step 2: Check equals() method
s1.equals(s3) compares content and both have "hello", so it returns true.
Final Answer:
true
false
true -> Option A
Quick Check:
== compares references, equals() compares content = C [OK]
4. Identify the error in the following code snippet:
String s = "test";
if (s == "test") {
System.out.println("Match");
} else {
System.out.println("No Match");
}
medium
A. System.out.println syntax is incorrect
B. Missing semicolon after string declaration
C. String literals cannot be compared
D. Using == to compare strings can cause unexpected results
Solution
Step 1: Check string comparison method
The code uses == to compare strings, which compares references, not content. This can cause unexpected results if strings are not from the pool.
Step 2: Verify other syntax
There is no missing semicolon, string literals can be compared with equals(), and System.out.println syntax is correct.
Final Answer:
Using == to compare strings can cause unexpected results -> Option D
Quick Check:
Use equals() for string comparison, not == = A [OK]
Hint: Avoid == for strings; use equals() instead [OK]
Common Mistakes:
Thinking == compares string content
Ignoring string pool behavior
Assuming syntax errors where none exist
5. Given the following code, what will be the output?
String a = "Java";
String b = "Ja" + "va";
String c = new String("Java");
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));
Explain why the outputs are as they are.
hard
A. false
true
false
B. true
false
true
C. true
true
true
D. false
false
true
Solution
Step 1: Analyze string concatenation with literals
Strings "Ja" and "va" are literals concatenated at compile time, so b points to the same string pool object as a. Therefore, a == b is true.
Step 2: Understand new String() behavior
c is a new String object created at runtime, so a == c is false because they reference different objects. However, a.equals(c) compares content and returns true.
Final Answer:
true
false
true -> Option B
Quick Check:
Compile-time concat shares pool, new String() creates new object = D [OK]
Hint: Compile-time concat shares pool; new String() creates new object [OK]
Common Mistakes:
Assuming runtime concat shares pool
Confusing == with equals()
Ignoring compile-time optimization
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.