Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn StringBuilder | String
Java Basics
course content

Course Content

Java Basics

Java Basics

1. Getting Started
2. Basic Types, Operations
3. Loops
4. Arrays
5. String

book
StringBuilder

What is StringBuilder?

StringBuilder is a utility class for manipulating and editing string objects.

StringBuilder provides its own methods, and creating a StringBuilder object is straightforward and intuitive.

java

Main

copy
1
StringBuilder builder = new StringBuilder();

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

java

Main

copy
1
StringBuilder builder = new StringBuilder("c<>definity");

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

copy
123456789
package com.example; public class Main { public static void main(String[] args) { StringBuilder builder = new StringBuilder("c<>definity"); String string = builder.toString(); System.out.println(string); } }

StringBuilder in action

StringBuilder provides a rich set of methods, with one of the most valuable and frequently used being append(String str). This method allows us to add a specified string to the existing content of a StringBuilder object.

Let's examine a code example:

java

Main

copy
12345678910111213141516
package com.example; public class Main { public static void main(String[] args) { // Initializing the new `String` with value "Hello" String hello = "Hello"; System.out.println("Our string before using an append() method: " + hello); // Initializing a new `StringBuilder` with value of our already initialized string StringBuilder builder = new StringBuilder(hello); // Using `append()` method, adding a string world to our `StringBuilder` builder = builder.append(" world!"); // Creating a new `String` variable that has a `StringBuilder` value String result = builder.toString(); System.out.println("Our string after using an append() method: " + result); } }

Why not use the + operator?

The main reason we prefer StringBuilder's append method is that it minimizes the creation of temporary objects, improving performance when modifying content multiple times. Every time you use the + operator, a new object is created, which can be costly in terms of memory and time. StringBuilder directly modifies the content, making it much more efficient for repeated modifications.

Task
test

Swipe to begin your solution

Create a string that lists products with their prices for display on a webpage. Use StringBuilder to construct the string.

  1. Add the header "Product List:\n" to the StringBuilder (where \n represents a line break).
  2. Loop through the array using a for-each loop.
  3. Get the product name from each array element.
  4. Get the product price from each array element.
  5. Append the product name and price to the StringBuilder in the correct format (Laptop - $999.99).
  6. Output the content of the StringBuilder to the console.

Solution

java

solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 6
toggle bottom row

book
StringBuilder

What is StringBuilder?

StringBuilder is a utility class for manipulating and editing string objects.

StringBuilder provides its own methods, and creating a StringBuilder object is straightforward and intuitive.

java

Main

copy
1
StringBuilder builder = new StringBuilder();

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

java

Main

copy
1
StringBuilder builder = new StringBuilder("c<>definity");

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

copy
123456789
package com.example; public class Main { public static void main(String[] args) { StringBuilder builder = new StringBuilder("c<>definity"); String string = builder.toString(); System.out.println(string); } }

StringBuilder in action

StringBuilder provides a rich set of methods, with one of the most valuable and frequently used being append(String str). This method allows us to add a specified string to the existing content of a StringBuilder object.

Let's examine a code example:

java

Main

copy
12345678910111213141516
package com.example; public class Main { public static void main(String[] args) { // Initializing the new `String` with value "Hello" String hello = "Hello"; System.out.println("Our string before using an append() method: " + hello); // Initializing a new `StringBuilder` with value of our already initialized string StringBuilder builder = new StringBuilder(hello); // Using `append()` method, adding a string world to our `StringBuilder` builder = builder.append(" world!"); // Creating a new `String` variable that has a `StringBuilder` value String result = builder.toString(); System.out.println("Our string after using an append() method: " + result); } }

Why not use the + operator?

The main reason we prefer StringBuilder's append method is that it minimizes the creation of temporary objects, improving performance when modifying content multiple times. Every time you use the + operator, a new object is created, which can be costly in terms of memory and time. StringBuilder directly modifies the content, making it much more efficient for repeated modifications.

Task
test

Swipe to begin your solution

Create a string that lists products with their prices for display on a webpage. Use StringBuilder to construct the string.

  1. Add the header "Product List:\n" to the StringBuilder (where \n represents a line break).
  2. Loop through the array using a for-each loop.
  3. Get the product name from each array element.
  4. Get the product price from each array element.
  5. Append the product name and price to the StringBuilder in the correct format (Laptop - $999.99).
  6. Output the content of the StringBuilder to the console.

Solution

java

solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 6
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt