Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
StringBuilder | String
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:

java

Main.java

Inside the parentheses, we can also pass a string value, and our StringBuilder variable will hold that value, for example:

java

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:

java

Main.java

Note

Keep in mind that if your StringBuilder has a null value, it will result in a NullPointerException (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:

java

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():

Note

Be extremely cautious when using StringBuilder, and remember that a String 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?

Section 5. Chapter 6
toggle bottom row
course content

Course Content

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:

java

Main.java

Inside the parentheses, we can also pass a string value, and our StringBuilder variable will hold that value, for example:

java

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:

java

Main.java

Note

Keep in mind that if your StringBuilder has a null value, it will result in a NullPointerException (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:

java

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():

Note

Be extremely cautious when using StringBuilder, and remember that a String 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?

Section 5. Chapter 6
toggle bottom row
some-alt