What is an append Method in Java?

Published on Oct 23,2019 5.8K Views

What is an append Method in Java?

edureka.co

Java offers a lot of methods to ease down your work. In this article, let’s discuss one such method, append(). The append method in java appends the specified string to the character sequence. Let me elaborate on the method append in Java.

The points to be discussed are as follows:

Let’s begin!

What is the method append in Java?

The method append in Java helps in appending the specified string to the character sequence. The characters of the string argument are then appended.

The declaration of the append method is as follows:

public StringBuilder append(String str)

str: It’s a string

Returns a reference to the object

Now that you are aware of the general syntax, let’s check out different ways/forms in which the method append in Java can be used.

Append in Java: Syntax

Different ways to represent the append method are:

Now that you are aware of the concept, let’s try to understand the concept with the help of an example.

Example

Below given code shows you the usage of StringBuilder class. Have a look!

Code:

import java.util.*; 
import java.util.concurrent.LinkedBlockingQueue; 
  
public class A { 
    public static void main(String[] argv) 
        throws Exception 
    { 
   
        StringBuilder str 
            = new StringBuilder(); 
  
        str.append("ABC"); 
  
         
        System.out.println("String = "
                           + str.toString()); 
  
         
        StringBuilder str1 
            = new StringBuilder("XYZ"); 

        System.out.println("String1 = "
                           + str1.toString()); 
  
         
        StringBuilder str2 
            = new StringBuilder(10); 
  
        // print string 
        System.out.println("String2 capacity = "
                           + str2.capacity()); 
  
         
         
        StringBuilder str3 
            = new StringBuilder(str1); 
  
        // print string 
        System.out.println("String3 = "
                           + str3.toString()); 
    } 
} 

Output:

String = ABC

String1 = XYZ

String2 capacity = 10

String3 = XYZ

Another way out is by using StringBuffer class.

Code:

import java.io.*; 
class GFG { 
    public static void main(String[] args) 
    { 
        StringBuffer s = new StringBuffer("GeeksforGeeks"); 
        int p = s.length(); 
        int q = s.capacity(); 
        System.out.println("Length of the string Edureka =" + p); 
        System.out.println("Capacity of the string Edureka =" + q); 
    } 
} 

Output:
Length of the string Edureka = 7
Capacity of the string Edureka = 10

In the above code, I have mentioned the two most used methods belonging to the StringBuffer class. Let me pour a little bit more information about this method that Java offers!

When to use the append method?

Well, the situation where + operator is used on string objects. Java by itself changes any modifications done to a string instance in two similar operations on a StringBuffer instance. Therefore concatenation invokes the append method on a StringBuffer object. As soon as the concatenation is performed the compiler calls the toString method to change the modified StringBuffer back into a constant string. It sounds really complex, right?

Well instead of all this why not just have one string class that acts like StringBuffer?

The solution here is performance. There are a lot of optimizations that the hour and time can make knowing that string objects are immutable. Java hides the complex part of conversion between String and StringBuffer, more precisely the programmers never really feel the need to use StringBuffer and will be able to solve most of the problems in terms of the + operator on string variables!

This brings us to the end of this article on method Append in Java. I hope you found it informative. Keep reading, keep exploring!

Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. 

Got a question for us? Please mention it in the comments section of this “Append in Java” article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework