🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the StringBuilder append() method in Java programming and how to use it with an example.
1. StringBuilder append() Method Overview
Definition:
The append() method of the StringBuilder class is used to append data of various data types (like int, char, String, etc.) to the current sequence, thus making string concatenation efficient and fast.
Syntax:
stringBuilder.append(data)
Parameters:
- data: The data to be appended to the current sequence. This can be of various data types.
Key Points:
- StringBuilder is mutable, which means strings can be modified without creating new objects.
- The append() method is overloaded to accept all data types, including objects.
- It provides a much more performance-efficient way to concatenate strings, especially in loops or repeated operations, compared to using the + operator.
2. StringBuilder append() Method Example
public class StringBuilderAppendExample {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder("Hello, ");
// Appending a String
builder.append("World");
System.out.println(builder);
// Appending an integer
builder.append(2023);
System.out.println(builder);
// Appending a character
builder.append('!');
System.out.println(builder);
// Appending a boolean
builder.append(true);
System.out.println(builder);
// Appending another StringBuilder
StringBuilder anotherBuilder = new StringBuilder(" Have a nice day.");
builder.append(anotherBuilder);
System.out.println(builder);
}
}
Output:
Hello, World Hello, World2023 Hello, World2023! Hello, World2023!true Hello, World2023!true Have a nice day.
Explanation:
In the example:
1. We start by creating a StringBuilder object with the text "Hello, ".
2. We then use the append() method to concatenate various types of data:
- First, we append the string "World".
- Next, we append the integer 2023.
- We follow that with the character '!'.
- We also append a boolean value 'true'.
- Lastly, we append another StringBuilder object.
The append() method makes it easy to concatenate different data types in a more performance-efficient manner than using the + operator, especially in scenarios where strings are being concatenated multiple times.
Related Java StringBuilder class method examples
- Java StringBuilder append() example
- Java StringBuilder delete() example
- Java StringBuilder insert() example
- Java StringBuilder reverse() example
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment