Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is an append Method in Java?

Published on Oct 23,2019 5.8K Views

17 / 22 Blog from Java Collections

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.

  • Declaration

The declaration of the append method is as follows:

public StringBuilder append(String str)
  • Parameter

str: It’s a string

  • Return value

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:

  • public StringBuilder append(boolean b)
  • public StringBuilder append(int i)
  • public StringBuilder append(float f)
  • public StringBuilder append(long l)
  • public StringBuilder append(double d)
  • public StringBuilder append(char c)
  • public StringBuilder append(char[] str)
  • public StringBuilder append(char[] str, int offset, int len)
  • public StringBuilder append(CharSequence cs)
  • public StringBuilder append(CharSequence cs, int start, int end)
  • public StringBuilder append(Object obj)
  • public StringBuilder append(String str)
  • public StringBuilder append(StringBuffer sb)
  • public StringBuilder appendCodePoint(int codePoint)

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 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is an append Method in Java?

edureka.co