In my experience as a software developer, handling strings in Java is a frequent requirement. However, Java strings are immutable, meaning they cannot be altered once created. To work around this limitation, I often turn to two classes: StringBuffer and StringBuilder. These classes provide various methods, including insert, replace, delete, and Append in Java.
The append method is particularly useful for adding data to a file. It allows me to seamlessly incorporate characters, Boolean values, strings, integers, floats, and more into my programs. Mastering the append method in Java has been crucial for efficiently managing string manipulation tasks ensuring smooth integration of new data into existing files or structures.
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
Learn Java Tutorials
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
As I conclude our exploration of append in Java, you will better understand their significance in programming. Learning how to append data using StringBuilder and StringBuffer classes has been enlightening.
This skill empowers you to enhance coding, making it more readable, maintainable, and efficient. With the ability to append, you can seamlessly handle tasks such as string concatenation and file manipulation in Java applications.
This newfound proficiency in append equips me to tackle more intricate programming challenges and develop robust software solutions. Given Java’s widespread use, refining my append skills is essential for advancing in my career and staying competitive in the industry.
Therefore, I believe you will be committed to further practicing and refining the knowledge of append in Java, leveraging it to create even more effective programs and propel in professional growth.
upGrad offers a Unique Master of Science in Computer Science Course for honing your skills and fostering growth in your software development career journey.
Check out all the Trending Java Tutorial Topics in 2024