String creation in Java - Time & Space Complexity
We want to see how the time needed to create strings changes as we make bigger or more strings.
How does the work grow when we build strings in different ways?
Analyze the time complexity of the following code snippet.
public class StringCreation {
public static void main(String[] args) {
String s = "";
for (int i = 0; i < 1000; i++) {
s += i;
}
}
}
This code builds a string by adding numbers one by one in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding numbers to the string inside the loop.
- How many times: The loop runs 1000 times, and each time it creates a new string by copying the old one plus the new number.
Each time we add to the string, the program copies the whole string so far, which takes longer as the string grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 55 copies (1+2+...+10) |
| 100 | About 5050 copies |
| 1000 | About 500,500 copies |
Pattern observation: The work grows much faster than the input size; it grows roughly like the square of the input.
Time Complexity: O(n²)
This means the time needed grows roughly with the square of how many times we add to the string.
[X] Wrong: "Adding to a string in a loop always takes the same time as the number of loop runs."
[OK] Correct: Each addition copies the whole string so far, so the work grows more than just the loop count.
Understanding how string creation time grows helps you write faster code and explain your choices clearly in interviews.
"What if we used a StringBuilder instead of adding strings directly? How would the time complexity change?"
Practice
Solution
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.Step 2: Validate syntax correctness
String greeting = "Hello"; uses correct capitalization, double quotes, and ends with a semicolon.Final Answer:
String greeting = "Hello"; -> Option AQuick Check:
String creation uses String and double quotes [OK]
- Using single quotes instead of double quotes
- Using lowercase 'string' instead of 'String'
- Missing semicolon at the end
Solution
Step 1: Identify correct string literal syntax
Java strings must be enclosed in double quotes, not single quotes.Step 2: Check each option
String name = 'Alice'; uses single quotes which is invalid for strings in Java, causing a syntax error.Final Answer:
String name = 'Alice'; -> Option BQuick Check:
Strings need double quotes, single quotes cause syntax error [OK]
- Using single quotes for strings
- Confusing char literals with strings
- Missing semicolon
String first = "Good";
String second = "Morning";
System.out.println(first + " " + second);
Solution
Step 1: Understand string concatenation
The + operator joins strings. Here, first + " " + second adds a space between words.Step 2: Combine the strings
"Good" + " " + "Morning" results in "Good Morning" with a space.Final Answer:
Good Morning -> Option CQuick Check:
+ joins strings including spaces [OK]
- Forgetting to add space between words
- Using + without quotes for space
- Expecting + to print plus sign
String text = "Hello"
System.out.println(text);
Solution
Step 1: Check string assignment line
The line ends without a semicolon, which is required in Java statements.Step 2: Verify other lines
Quotes are correct, variable name is valid, and print statement syntax is correct.Final Answer:
Missing semicolon after string assignment -> Option DQuick Check:
Every statement must end with a semicolon [OK]
- Omitting semicolon at end of statement
- Confusing quotes with apostrophes
- Misnaming variables
Solution
Step 1: Understand string concatenation with space
To add a space between first and last names, include " " between them.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.Final Answer:
String fullName = firstName + " " + lastName; -> Option AQuick Check:
Use + " " + to add space between strings [OK]
- Forgetting space between names
- Using invalid operators like -
- Using concat() without space
Ready to prove it?
Pass the test and this pattern moves unknown
