Bird
Raised Fist0
Javaprogramming~10 mins

String creation in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - String creation
Start
↓
Declare String variable
↓
Assign value using literal or new
↓
String object created in memory
↓
Variable holds reference to String
↓
Use String in program
↓
End
This flow shows how a String variable is declared, assigned a value, and holds a reference to a String object in memory.
Execution Sample
Java
String s1 = "Hello";
String s2 = new String("World");
System.out.println(s1 + " " + s2);
Creates two strings in different ways and prints them with a space.
Execution Table
StepActionVariableValue/ReferenceOutput
1Declare s1 and assign literal "Hello"s1Reference to "Hello" in string pool
2Declare s2 and assign new String("World")s2Reference to new String object "World"
3Print s1 + " " + s2Hello World
4End of program
💡 Program ends after printing the combined string.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
s1undefinedReference to "Hello"Reference to "Hello"Reference to "Hello"Reference to "Hello"
s2undefinedundefinedReference to new String "World"Reference to new String "World"Reference to new String "World"
Key Moments - 2 Insights
Why does s1 point to the string pool but s2 points to a new object?
Because s1 is assigned using a string literal which Java stores in the string pool for reuse, while s2 uses new String() which creates a new object in memory even if the same text exists.
Does s1 == s2 return true?
No, because s1 and s2 reference different objects in memory. The == operator checks reference equality, not content.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does variable s1 reference after step 1?
AA string literal in the string pool
BA new String object in heap
Cnull
DAn integer value
💡 Hint
Check the 'Value/Reference' column for s1 at step 1 in the execution_table.
At which step is the output "Hello World" produced?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table to find when the print happens.
If we changed s2 to use a string literal instead of new String("World"), what would change in variable_tracker?
AOutput would change to "HelloWorld" without space
Bs1 would become null
Cs2 would reference the string pool instead of a new object
DNo change at all
💡 Hint
Consider how string literals and new String() differ in memory referencing.
Concept Snapshot
String creation in Java:
- Use String s = "text"; to create from literal (stored in string pool).
- Use String s = new String("text"); to create a new object.
- Variables hold references to String objects.
- == checks references, .equals() checks content.
- String literals reuse objects for memory efficiency.
Full Transcript
This example shows how Java creates strings. First, s1 is declared and assigned a string literal "Hello", which Java stores in a special area called the string pool. Then s2 is declared and assigned a new String object with the text "World". This creates a new object in memory, different from the string pool. When printing s1 and s2 combined with a space, the output is "Hello World". The variable tracker shows s1 and s2 holding references to different objects. Beginners often confuse string literals with new String objects and the difference in memory. Also, the == operator compares references, so s1 == s2 is false here. Changing s2 to a literal would make it reference the string pool like s1. This helps save memory and allows string reuse.

Practice

(1/5)
1. Which of the following is the correct way to create a string variable in Java?
easy
A. String greeting = "Hello";
B. string greeting = 'Hello';
C. String greeting = Hello;
D. String greeting = "Hello"

Solution

  1. Step 1: Check the data type and quotes

    Java strings use the class String with a capital S and text must be in double quotes.
  2. Step 2: Validate syntax correctness

    String greeting = "Hello"; uses correct capitalization, double quotes, and ends with a semicolon.
  3. Final Answer:

    String greeting = "Hello"; -> Option A
  4. Quick Check:

    String creation uses String and double quotes [OK]
Hint: Use capital S in String and double quotes for text [OK]
Common Mistakes:
  • Using single quotes instead of double quotes
  • Using lowercase 'string' instead of 'String'
  • Missing semicolon at the end
2. Which of the following lines will cause a syntax error in Java?
easy
A. String name = "Alice";
B. String name = 'Alice';
C. String name = "Bob";
D. String name = "Charlie";

Solution

  1. Step 1: Identify correct string literal syntax

    Java strings must be enclosed in double quotes, not single quotes.
  2. Step 2: Check each option

    String name = 'Alice'; uses single quotes which is invalid for strings in Java, causing a syntax error.
  3. Final Answer:

    String name = 'Alice'; -> Option B
  4. Quick Check:

    Strings need double quotes, single quotes cause syntax error [OK]
Hint: Remember: Java strings use double quotes only [OK]
Common Mistakes:
  • Using single quotes for strings
  • Confusing char literals with strings
  • Missing semicolon
3. What will be the output of the following code?
String first = "Good";
String second = "Morning";
System.out.println(first + " " + second);
medium
A. GoodMorning
B. Good+Morning
C. Good Morning
D. gninroM dooG

Solution

  1. Step 1: Understand string concatenation

    The + operator joins strings. Here, first + " " + second adds a space between words.
  2. Step 2: Combine the strings

    "Good" + " " + "Morning" results in "Good Morning" with a space.
  3. Final Answer:

    Good Morning -> Option C
  4. Quick Check:

    + joins strings including spaces [OK]
Hint: Use + with " " to add spaces between words [OK]
Common Mistakes:
  • Forgetting to add space between words
  • Using + without quotes for space
  • Expecting + to print plus sign
4. Find the error in the following code snippet:
String text = "Hello"
System.out.println(text);
medium
A. System.out.println syntax is wrong
B. Incorrect string quotes
C. Variable name is invalid
D. Missing semicolon after string assignment

Solution

  1. Step 1: Check string assignment line

    The line ends without a semicolon, which is required in Java statements.
  2. Step 2: Verify other lines

    Quotes are correct, variable name is valid, and print statement syntax is correct.
  3. Final Answer:

    Missing semicolon after string assignment -> Option D
  4. Quick Check:

    Every statement must end with a semicolon [OK]
Hint: Always end Java statements with a semicolon [OK]
Common Mistakes:
  • Omitting semicolon at end of statement
  • Confusing quotes with apostrophes
  • Misnaming variables
5. You want to create a string variable that holds the full name by combining first and last names with a space. Which code correctly does this?
hard
A. String fullName = firstName + " " + lastName;
B. String fullName = firstName + lastName;
C. String fullName = firstName.concat(lastName);
D. String fullName = firstName - lastName;

Solution

  1. Step 1: Understand string concatenation with space

    To add a space between first and last names, include " " between them.
  2. Step 2: Check each option

    String fullName = firstName + " " + lastName; correctly uses + operator with a space string. String fullName = firstName + lastName; misses space, C concatenates without space, D uses invalid operator.
  3. Final Answer:

    String fullName = firstName + " " + lastName; -> Option A
  4. Quick Check:

    Use + " " + to add space between strings [OK]
Hint: Add " " between strings to insert space [OK]
Common Mistakes:
  • Forgetting space between names
  • Using invalid operators like -
  • Using concat() without space
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard