append() in Java

By: Jagan Printer Friendly Format    

The append() method concatenates the string representation of any other type of data to the end of the invoking StringBuffer object. It has overloaded versions for all the built-in types and for Object. Here are a few of its forms: StringBuffer append(String str)
StringBuffer append(int num)
StringBuffer append(Object obj)

String.valueOf( ) is called for each parameter to obtain its string representation. The result is appended to the current StringBuffer object. The buffer itself is returned by each version of append(). This allows subsequent calls to be chained together, as shown in the following example:

// Demonstrate append().
class appendDemo {
public static void main(String args[]) {
String s;
int a = 42;
StringBuffer sb = new StringBuffer(40);
s = sb.append("a = ").append(a).append("!").toString();

The output of this example is shown here:

a = 42!

The append( ) method is most often called when the + operator is used on String objects. Java automatically changes modifications to a String instance into similar operations on a StringBuffer instance. Thus, a concatenation invokes append( ) on a StringBuffer object. After the concatenation has been performed, the compiler inserts a call to toString( ) to turn the modifiable StringBufferback into a constant String. All of this may seem unreasonably complicated. Why not just have one string class and have it behave more or less like StringBuffer? The answer is performance. There are many optimizations that the Java run time can make knowing that Stringobjects are immutable. Thankfully, Java hides most of the complexity of conversion between Strings
and StringBuffers. Actually, many programmers will never feel the need to use StringBuffer directly and will be able to express most operations in terms of the + operator on String variables.

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Its really good.....
View Tutorial          By: Shivam at 2009-04-02 03:58:51

2. good one
View Tutorial          By: vij at 2009-06-03 16:00:12

3. One of the best
View Tutorial          By: Sudhir at 2009-12-22 01:45:28

4. Good one. Clear basic concept
View Tutorial          By: Joshua at 2010-03-12 01:13:08

5. I want study how I translate any sentences in Java
View Tutorial          By: deeeegiiiiiiiiiiii at 2010-06-18 02:07:13

6. Your explanation and examples are short and sweet.
View Tutorial          By: prince at 2011-01-19 00:18:45

7. good example
View Tutorial          By: WANT TO KNOW WHY WE USE APPEND at 2011-06-14 00:39:48

8. Excellent. I want you to publish about memory allo
View Tutorial          By: Mohamed firnaz at 2011-12-07 04:54:27

9. Great work. Simple and easily understandable
View Tutorial          By: Janshair Khan at 2012-02-21 12:36:51

10. very nice....,
View Tutorial          By: karthik,meenakshi amman kovil,mdu at 2012-04-20 10:57:14

11. thank you for your excellent explaination..! :)
View Tutorial          By: marios at 2012-05-16 16:17:31

12. Best Explanation & gud example by finnaz......
View Tutorial          By: Umair Shuja at 2012-11-28 10:53:35

13. Good Example esp. way it was presented.
View Tutorial          By: Arunlal at 2013-06-20 11:56:43

14. Sencillo y directo. No se podía explicar me
View Tutorial          By: Eligio at 2013-09-03 05:26:35

15. Cant undrstand clearly
View Tutorial          By: rathika at 2014-06-09 11:02:16

16. it was nice and helpful for me.
View Tutorial          By: mayank at 2015-03-01 08:35:48

17. hai
View Tutorial          By: Priya.d at 2016-10-06 10:03:00

18. I must say you have hi quality posts here. Your po
View Tutorial          By: LavinaX at 2017-07-07 04:57:32

19. GymGuygase
View Tutorial          By: GymGuygase at 2017-07-25 04:50:55