There will be so many instances in a software development job where you will join strings in an existing string. Although it seems easier than done, strings are immutable in Java. For making them mutable and implementing changes, two classes, StringBuffer and StringBuilder, are used. The StringBuilder and StringBuffer classes have methods such as insert, replace, delete and Append in Java.
The append method is mainly used to append or add data in a file. You can add characters, Booleans, string, integer, float, etc., to the data in a program. We will see each of these with the way append in Java is coded in a program.
Check out our free courses to get an edge over the competition.
Append in Java
Append in Java is a StringBuilder and StringBuffer class method used to append a value to the current sequence. String concatenation in Java is done using the StringBuilder or StringBuffer class and append() method.Â
String Classes in Java
String class is final and has no child classes, and its instances cannot be modified after creation. The StringBuilder in Java represents a mutable sequence. The StringBuilder class provides an alternative to String Class.
Check out upGrad’s Java Bootcamp
Due to immutability, new string instances are created after each operation, and old ones are discarded, creating lots of garbage. Thus, StringBuffer or StringBuilder classes deal with the temporary garbage generation due to modifications to the String.
Explore Our Software Development Free Courses
The function of StringBuilder is quite similar to the StringBuffer class. However, both the StringBuilder and StringBuffer class differ in synchronisation. The StringBuffer class is synchronised, whereas the StringBuilder class provides no synchronisation. Hence, StringBuffer will be modified frequently in a multi-threaded environment and StringBuilder in a single-threaded environment.
Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
Append Method Implementation in StringBuilder and StringBuffer Classes
Append in Java is StringBuilder and StringBuffer classes’ method that adds a new value to the current sequence. The appending can be done in 13 forms.Â
Syntax
public StringBuffer append()
Let’s see the actual implementations of the append method in all the forms:
1. StringBuffer append(boolean bo): Appending string to the boolean argument.
import java.lang.*;Â Â
public class AB { Â Â Â Â public static void main(String[] args) Â Â Â Â { Â Â Â Â Â Â Â Â StringBuffer sbd = new StringBuffer (“This is appending boolean “); Â Â Â Â Â Â Â Â System.out.println(“Input: ” + sbd); Â Â Â Â Â Â Â Â sbd.append(in Java); Â Â Â Â Â Â Â System.out.println(“Output: “+ sbd); Â Â Â Â } } |
Output
Input: This is appending boolean
Output: This is appending boolean in Java
Our learners also read: Java free online courses!
Explore our Popular Software Engineering Courses
2. StringBuffer append(char ch): Appending string to the character argument.
import java.lang.*;Â
public class CD{ Â Â Â Â public static void main(String[] args) Â Â Â Â { Â Â Â Â Â Â Â Â StringBuffer sbd = new StringBuffer(“This is appending character”); Â Â Â Â sbf.append(‘A’); Â Â Â Â Â Â Â Â System.out.println(“After appending = ” + sbd); Â Â } } |
Output:
This is appending a character
After appending = This is appending character A
upGrad’s Exclusive Software and Tech Webinar for you –
SAAS Business – What is So Different?
3. StringBuffer append(char[] string):Â Appending string to the character array.
import java.lang.*;
public class EF{Â Â
    public static void main(String[] args)
{
        StringBuffer sbd = new StringBuffer(” This is appending a character string “);
        char[] astr = new char[] { ‘I’, ‘N’, ‘D’, ‘I’, ‘A’ };
        sbd.append(astr);
        System.out.println(“After appending = ” + sbd);
    }
}Â
Output:
This is appending a character string
After appending = This is appending a character string INDIA
In-Demand Software Development Skills
4. StringBuffer append(char[] string, int offset, int len): Appending string to a character array.
import java.lang.*;Â Â
public class GH {Â Â Â Â Â Â public static void main(String[] args) Â Â Â Â {Â Â Â Â Â Â Â Â Â Â Â StringBuffer sbd = new StringBuffer (“Java”); Â Â Â Â Â Â Â Â System.out.println(” Before Appending= ” + sbd);Â Â Â Â Â Â Â Â Â Â char[] cstr = new char[] { ‘i’, ‘s’, ‘a’, ‘w’, ‘e’, ‘s’, ‘o’, ‘m’, ‘e’ }; Â Â Â Â Â Â Â Â sb.append(cstr, 0, 3);Â Â Â Â Â Â Â Â Â Â Â System.out.println(“After appending string = ” + sbd); Â Â Â Â } } |
Output:
Before Appending= Java
After appending string = Javaisawesome
5. StringBuffer append(CharSequence cse): Appending string to a character Sequence.
import java.lang.*;Â Â
public class IJ { Â Â Â Â public static void main(String[] args) Â Â Â Â { Â Â Â Â Â Â Â Â Â StringBuffer sbd = new StringBuffer(“Javais”); Â Â Â Â Â Â Â Â System.out.println(” string = ” + sbd); Â Â Â Â Â Â Â Â CharSequence chSeq = “Awesome”;Â Â Â Â Â Â Â Â Â Â Â sbf.append(chSeq); Â Â Â Â Â Â Â Â System.out.println(“After appending = ” + sbd); Â Â Â Â } } |
Output:
string = Javais
After appending = JavaisAwesome
6. StringBuffer append(CharSequence cse, int start, int end): Appending Character Sequence to a String.
import java.lang.*;Â Â
public class KL { public static void main(String[] args) {      StringBuffer sbd = new StringBuffer(“Java is “);         System.out.println(” string = ” + sbd);           CharSequence chSeq = “awesome”;         sbf.append(chSeq, 1, 4);          System.out.println(“After appending string = ” + sbf);     } } |
Output:
string = Java is
After appending string = Java is weso
Read our Popular Articles related to Software Development
7. StringBuffer append(double do): Appending a double to the string.
Program:
import java.lang.*;
public class MN {Â Â
    public static void main(String[] args)
    { Â
        StringBuffer sbd = new StringBuffer(“The main number is: “);
        Double astr = new Double(522.39);
        sbd.append(astr);
        System.out.println(“After appending = ” + sbd);
    }
}
Output:
The main number is:
After appending = 522.39
8. StringBuffer append(float fl): Appending the string to float.
import java.lang.*;Â Â
public class OP {
    public static void main(String[] args)
    { Â
        StringBuffer sbd = new StringBuffer(“Java is awesome “);
        Float astr = new Float(1.23);
        sbd.append(astr);
        System.out.println(“After appending = ” + sbd);
    }
}
Output:
Java is awesome
After appending = Java is awesome 1.23
9. StringBuffer append(int i): Append integer to the string.
import java.lang.*;Â Â
public class QR {
    public static void main(String[] args)
    {
        StringBuffer sbd = new StringBuffer(“Java is awesome “);
        Integer astr = new Integer(478);
        sbd.append(astr);
        System.out.println(“After appending = ” + sbd);
    }
}Â
Output:
Java is
After appending = Java is awesome 478
10. StringBuffer append(long lng): Appending the string to a long argument.
import java.lang.*;Â Â
public class ST {Â Â Â Â Â Â public static void main(String[] args) Â Â Â Â { Â Â Â Â Â Â Â Â StringBuffer sbd = new StringBuffer(“Java is Awesome “);Â Â Â Â Â Â Â Â Â Â Long astr = new Long(1993); Â Â Â Â Â Â Â Â sbd.append(astr); Â Â Â Â Â Â Â Â System.out.println(“After appending = ” + sbd); Â Â Â Â } } |
Output:
Java is Awesome
After appending = Java is Awesome 1993
11. StringBuffer append(Object obj): Appending an Object to a string.
import java.lang.*;Â Â
public class UV{Â Â Â Â Â Â public static void main(String[] args) Â Â Â Â {Â Â Â Â Â Â Â Â Â Â StringBuffer sbd = new StringBuffer(“Javais”); Â Â Â Â Â Â Â Â System.out.println(“string = ” + sbd);Â Â Â Â Â Â Â Â Â Â Object objectvalue = “awesome”;Â Â Â Â Â Â Â Â Â Â sbd.append(objectvalue);Â Â Â Â Â Â Â Â Â Â System.out.println(“After appending = ” + sbd); Â Â Â Â } } |
Output:
string = Javais
After appending = Javaisawesome
12. StringBuffer append(String str): Append a string to another string.
import java.lang.*;Â Â
public class WX{Â Â Â Â Â Â public static void main(String[] args) Â Â Â Â { Â Â Â Â Â Â Â Â StringBuffer sbd = new StringBuffer(“Javais”); Â Â Â Â Â Â Â Â System.out.println(“string = ” + sbd);Â Â Â Â Â Â Â Â Â Â Â String strvalue = “awesome”; Â Â Â Â Â Â Â Â sbd.append(strvalue);Â Â Â Â Â Â Â Â Â Â Â System.out.println(“After appending = ” + sbd); Â Â Â Â } } |
Output:
string = Javais
After appending = Javaisawesome
13. StringBuffer append(StringBuilder sbf): Appending the StringBuilder to StringBuffer
import java.lang.*;
public class YZ{Â Â Â Â Â Â public static void main(String[] args) Â Â Â Â { Â Â Â Â Â Â Â Â StringBuffer sbd = new StringBuffer(“Javais”); Â Â Â Â Â Â Â Â System.out.println(“String 1 = ” + sbd1); Â Â Â Â Â Â Â Â StringBuffer sbf2 = new StringBuilder(“awesome “); Â Â Â Â Â Â Â Â System.out.println(“String 2 = ” + sbd2); Â Â Â Â Â Â Â Â Sbd1.append(sbd2);Â Â Â Â Â Â Â Â Â Â System.out.println(“After appending = ” + sbd1); Â Â Â Â } } |
Output:
String 1 = Javais
String 2 = awesome
After appending = Javaisawesome
Conclusion
In this article, we have covered one of the most important concepts, that is, append in Java. It makes an immutable string mutable and can concatenate anything to a string, whether an integer, character, or object. From the programming point of view, it is very useful and widely practised too. We hope we could make you understand every aspect of append along with its practical implementation.
upGrad offers a Unique Master of Science in Computer Science Course for honing your skills and fostering growth in your software development career journey.
What is StringBuffer and StringBuilder class in Java?
Java StringBuffer class is used to create modifiable or mutable objects. This class is similar to the string class except for a couple of differences. StringBuffer(), StringBuffer(String str), and StringBuffer(int capacity) are the important constructors of this class. StringBuffer() constructor allocates a capacity of 16 to an empty string buffer. StringBuffer(String str) assigns a specified string to a string buffer. StringBuffer(int capacity) specifies the length of the buffer and creates an empty string buffer. StringBuilder class focuses on creating modifiable or mutable strings. A StringBuffer is not synchronized. StringBuilder(), StringBuilder(String str), and StringBuilder(int length) are important constructors of this class. The StringBuilder() constructor allocates a capacity of 16 to an empty string builder. StringBuilder(String str) assigns a specified string to a string. StringBuilder(int length) specifies the length of the buffer and creates an empty string buffer.
How are String and StringBuffer different from each other?
The String class is immutable whereas the StringBuffer class is mutable. The next difference between them is that String class works with a string constant pool and StringBuffer uses heap memory. Performing concatenation operations using a string class is a slow process compared to the StringBuffer class. Due to the slowness of the string class, it consumes more memory. However, StringBuffer works with less memory due to its speed.
What is the difference between StringBuffer and StringBuilder?
Java uses String, StringBuffer, and StringBuilder classes for its characters. The notable differences between the two starts with their introduction. StringBuffer was used during Java 1.0 whereas StringBuilder’s introduction happened in Java 1.5. The efficiency of StringBuffer is less than StringBuilder. Moreover, StringBuffer is thread-safe which means it is synchronized. This means two threads can’t call StringBuffer methods at the same time. StringBuilder isn’t synchronized and isn’t thread-safe. Therefore, two threads can refer to the StringBuilder methods at the same time. Due to these differences, depending on the scenarios, one class object is used over the other.