📖 Scenario: Imagine you are writing a program that builds a message by adding words one by one. You want to see how using String and StringBuilder affects the way your program works.
🎯 Goal: You will create a simple Java program that first builds a message using String concatenation and then builds the same message using StringBuilder. You will see the difference in how the code looks and runs.
📋 What You'll Learn
Create a String variable with an initial value.
Create a StringBuilder variable with the same initial value.
Add words to both the String and the StringBuilder.
Print the final messages from both variables.
💡 Why This Matters
🌍 Real World
Building messages or text dynamically is common in apps like chat programs, reports, or logs.
💼 Career
Understanding when to use <code>String</code> or <code>StringBuilder</code> helps write efficient Java code, important for software developers.
Progress0 / 4 steps
1
Create a String variable
Create a String variable called message and set it to the value "Hello".
Java
Hint
Use String message = "Hello"; to create the variable.
2
Create a StringBuilder variable
Add a StringBuilder variable called builder and set it to a new StringBuilder initialized with the value of message.
Java
Hint
Use StringBuilder builder = new StringBuilder(message); to create the variable.
3
Add words to both variables
Add the word " World" to the message variable using + concatenation, and add the same word to the builder variable using the append() method.
Java
Hint
Use message = message + " World"; and builder.append(" World");.
4
Print both final messages
Print the message variable and then print the builder variable converted to a String using toString().
Java
Hint
Use System.out.println(message); and System.out.println(builder.toString());.
Practice
(1/5)
1. Which statement best describes when to use StringBuilder instead of String in Java?
easy
A. Use StringBuilder when you need to change text many times efficiently.
B. Use StringBuilder for fixed text that never changes.
C. Use String when you want faster text modifications.
D. Use String to save memory when changing text repeatedly.
Solution
Step 1: Understand the nature of String and StringBuilder
String objects are immutable, meaning they cannot be changed once created. Changing text creates new objects, which is slow and uses more memory.
Step 2: Identify when StringBuilder is better
StringBuilder allows changing text without creating new objects, making it efficient for many modifications.
Final Answer:
Use StringBuilder when you need to change text many times efficiently. -> Option A
Quick Check:
Many changes = StringBuilder [OK]
Hint: Use StringBuilder for many text changes, String for fixed text [OK]
Common Mistakes:
Thinking String is better for many changes
Confusing immutability of String
Assuming StringBuilder uses more memory always
2. Which of the following is the correct way to create a StringBuilder object with initial text "Hello"?
easy
A. StringBuilder sb = StringBuilder("Hello");
B. StringBuilder sb = "Hello";
C. StringBuilder sb = new StringBuilder[] {"Hello"};
D. StringBuilder sb = new StringBuilder("Hello");
Solution
Step 1: Recall the correct constructor syntax for StringBuilder
The StringBuilder class requires the new keyword and parentheses with the initial string inside.
Step 2: Check each option for syntax correctness
StringBuilder sb = new StringBuilder("Hello"); uses new StringBuilder("Hello"), which is correct. Options A, B, and D have syntax errors or wrong usage.
Final Answer:
StringBuilder sb = new StringBuilder("Hello"); -> Option D
Quick Check:
Use new + constructor for StringBuilder [OK]
Hint: Always use 'new StringBuilder("text")' to create StringBuilder [OK]
Common Mistakes:
Omitting 'new' keyword
Using assignment without constructor
Using array syntax mistakenly
3. What is the output of this Java code?
StringBuilder sb = new StringBuilder("Hi");
sb.append("!");
sb.append(" How are you?");
System.out.println(sb.toString());
medium
A. Hi!How are you?
B. Hi How are you?
C. Hi! How are you?
D. Hi
Solution
Step 1: Understand what append() does on StringBuilder
Each append() adds the given text to the end of the current StringBuilder content without spaces unless explicitly added.
Step 2: Trace the code execution
Initial text is "Hi". First append adds "!" making "Hi!". Second append adds " How are you?" (with leading space), resulting in "Hi! How are you?".
Final Answer:
Hi! How are you? -> Option C
Quick Check:
Appends add text exactly as given [OK]
Hint: Append adds text exactly; watch spaces carefully [OK]
Common Mistakes:
Ignoring spaces in appended strings
Assuming append replaces text
Printing StringBuilder object directly without toString()