Course Content
Java Basics
1. Getting Started
Java Basics
StringBuilder














What is StringBuilder?
StringBuilder
is a utility class for manipulating and editing string objects.
Note
You will learn more about classes, how to create them, and how to work with them correctly in a separate course.
StringBuilder
has its own methods, and creating a StringBuilder
object is as straightforward as creating a String
object:
Main.java
Inside the parentheses, we can also pass a string value, and our StringBuilder
variable will hold that value, for example:
Main.java
To print our value to the console, we need to use one of the StringBuilder
methods. It's the toString()
method, which converts the value of the StringBuilder to a String type value.
Let's look at an example:
Main.java
Note
Keep in mind that if your
StringBuilder
has a null value, it will result in aNullPointerException
(NPE) being thrown.
StringBuilder in Action
Both StringBuilder
and String
have their own set of methods, and while they share many similarities, there's one method that's especially valuable and frequently used in StringBuilder
: append(String str)
. This method allows us to add a specified string to the existing content of our StringBuilder
object.
Let's examine a code example:
Main.java
Why Not Use the + Operator?
You might be wondering why we don't simply use the + operator for string manipulation. Well, performing mathematical operations with string variables is discouraged in Java. Even using == for comparison is discouraged in most cases, except when comparing with null. (Remember, we can't use methods with null values, right?)
Instead, it's better to use the equals()
method for string comparison, which we will delve into in the next chapter.
Task
You have been provided with an array of string values. Your task is to append "!" to each of these values to ensure clarity when our speaker reads the answers. Keep in mind that the array may also contain null values (I did not create it). Replace these null values with a space symbol (" "
). After adding the exclamation mark to each array element, display the modified array on the screen using a for-each loop and System.out.print()
:
Main.java
- 1. Use a for-each loop where you will create a
StringBuilder
object that will use theappend()
method to add "!" to each array element; - 2. Then, within the same loop, print each element to the console.
package com.example;
public class Main {
public static void main(String[] args) {
String[] speech = {"Today", " we", " are", null, "making", null, "Java", " great", " again", null};
for (String element : speech) {
if (element != null) { // null check
// creating a StringBuilder object and init it with element value
StringBuilder builder = new StringBuilder(element);
// adding an "!" to each element
builder = builder.append("!");
//printing each element to the console
System.out.print(builder.toString());
} else {
// changing element to space symbol
element = " ";
// printing this element to the console
System.out.print(element);
}
}
}
}
Note
Be extremely cautious when using
StringBuilder
, and remember that aString
cannot be modified after initialization. If you encounter a situation in your program where you think your methods are not working, double-check whether you have considered this.














Everything was clear?