StringBuffer append() Method in Java: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-05 03:41:29   6119  Share
Subject Syllabus DetailsSubject Details 1 Questions
☰ TContent
☰Fullscreen

Table of Content:

StringBuffer, with its nature of mutability, comes with many methods that do not exist in String. One useful method is append() StringBuffer method which appends anything to the existing contents.

The append( ) method concatenates the string representation of any other type of data to the end of the invoking StringBuffer object. It has several overloaded versions. Here are a few of its forms in syntax:

Syntax

StringBuffer append(String str)
StringBuffer append(int num)
StringBuffer append(Object obj)

Program

class StringBufferExample{
public static void main(String args[]){

	StringBuffer strng=new StringBuffer("Hello ");
	strng.append("atnyla");//now original string is changed
	System.out.println(strng);//prints Hello Java

	}
}

Output

Hello atnyla
Press any key to continue . . .