🎓 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
StringBuffer.append() method in Java is used to append various types of data to the existing sequence of characters in a StringBuffer object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
appendMethod Syntax- Examples
- Appending Strings
- Appending Different Data Types
- Chaining
appendMethods
- Conclusion
Introduction
The StringBuffer.append() method is a member of the StringBuffer class in Java. It allows you to efficiently append different types of data to a StringBuffer object without creating a new object for each modification. Unlike StringBuilder, StringBuffer is synchronized, making it thread-safe and a preferred choice for string manipulation in multi-threaded environments.
append Method Syntax
The syntax for the append method is as follows:
public synchronized StringBuffer append(String str)
The append method is overloaded to accept different data types:
StringBuffer append(boolean b)StringBuffer append(char c)StringBuffer append(char[] str)StringBuffer append(char[] str, int offset, int len)StringBuffer append(double d)StringBuffer append(float f)StringBuffer append(int i)StringBuffer append(long lng)StringBuffer append(CharSequence s)StringBuffer append(CharSequence s, int start, int end)StringBuffer append(Object obj)StringBuffer append(String str)StringBuffer append(StringBuffer sb)
Examples
Appending Strings
The append method can be used to concatenate strings to the existing sequence in a StringBuffer object.
Example
public class StringBufferAppendExample {
public static void main(String[] args) {
// Create a StringBuffer object with initial content "Hello"
StringBuffer sb = new StringBuffer("Hello");
// Append ", World!" to the existing sequence
sb.append(", World!");
// Print the final content of the StringBuffer
System.out.println(sb.toString());
}
}
Output:
Hello, World!
Appending Different Data Types
The append method is overloaded to handle different data types, allowing you to append integers, characters, booleans, and other types directly.
Example
public class StringBufferAppendExample {
public static void main(String[] args) {
// Create an empty StringBuffer object
StringBuffer sb = new StringBuffer();
// Append different types of data to the StringBuffer
sb.append("Age: "); // Append a string
sb.append(30); // Append an integer
sb.append(", Height: ");
sb.append(5.9); // Append a double
sb.append(", Active: ");
sb.append(true); // Append a boolean
// Print the final content of the StringBuffer
System.out.println(sb.toString());
}
}
Output:
Age: 30, Height: 5.9, Active: true
Chaining append Methods
The append method returns the StringBuffer object itself, allowing for method chaining to perform multiple append operations in a single statement.
Example
public class StringBufferAppendExample {
public static void main(String[] args) {
// Create an empty StringBuffer object
StringBuffer sb = new StringBuffer();
// Chain multiple append operations
sb.append("Name: ").append("Ramesh").append(", Occupation: ").append("Developer");
// Print the final content of the StringBuffer
System.out.println(sb.toString());
}
}
Output:
Name: Ramesh, Occupation: Developer
Conclusion
The StringBuffer.append() method in Java is a versatile and efficient way to build and modify strings dynamically. By understanding how to use this method, you can optimize your string manipulation operations, especially in scenarios where performance and thread safety are crucial. Whether you need to append strings, handle different data types, or chain multiple append operations, the append method provides a powerful solution for these tasks.
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