append() in Java

By: Jagan Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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();
System.out.println(s);
}
}

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.


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(12)


1. View Comment

Its really good.....

View Tutorial          By: Shivam at 2009-04-02 03:58:51
2. View Comment

good one

View Tutorial          By: vij at 2009-06-03 16:00:12
3. View Comment

One of the best

View Tutorial          By: Sudhir at 2009-12-22 01:45:28
4. View Comment

Good one. Clear basic concept

View Tutorial          By: Joshua at 2010-03-12 01:13:08
5. View Comment

good example

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

Excellent. I want you to publish about memory allocation for every sample programs side by side, as it plays a major role in live projects. For example :

public class stringmemory
{
public static void main(String[] args){
String str = "Sri seshaa";
String str1 = " Sri seshaa";
String str2 = str + str1 + " technologies";

StringBuffer s;
String d;
StringBuffer bufr = new StringBuffer(str);
s = bufr.append(str2);
d = String.valueOf(str);
System.out.println("......");
System.out.println(str2);
System.out.println("......");
System.out.println(d);
}
}

In this the memory allocated for concatenation in string class is more compared to the append operation in StringBuffer. Like this there are many advantages and disadvantages in it. Please publish it also, it will be helpful for the young programmers like me.


View Tutorial          By: Mohamed firnaz at 2011-12-07 04:54:27
7. View Comment

thank you for your excellent explaination..! :)

View Tutorial          By: marios at 2012-05-16 16:17:31
8. View Comment

Best Explanation & gud example by finnaz.........!
By: Umair Shuja at Iqra University on 2012-11-28


View Tutorial          By: Umair Shuja at 2012-11-28 10:53:35
9. View Comment

Good Example esp. way it was presented.

View Tutorial          By: Arunlal at 2013-06-20 11:56:43
10. View Comment

Sencillo y directo. No se podía explicar mejor.

View Tutorial          By: Eligio at 2013-09-03 05:26:35
11. View Comment

Cant undrstand clearly

View Tutorial          By: rathika at 2014-06-09 11:02:16
12. View Comment

it was nice and helpful for me.

View Tutorial          By: mayank at 2015-03-01 08:35:48

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Jagan
Importing a Certificate for the CA example using keytool in Java
The if-then-else Statement in Java
WHERE Clauses in SQL
The TryCatchFinally Interface in JSP
assert() Function Example program in C
Guidelines for Struts Application Development
Using printf function in C
File Inclusion in C
Using Multiple Message Resource Bundles in Struts
While and For Loops in C
Arrays sample program in C
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Using cout.fill() in C++
Types of Profiles in J2ME
DateField sample program in J2ME

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
Student marks calculation program in Java
Count number of vowels, consonants and digits in a String in Java
FileReader and FileWriter example program in Java
Execute system commands in a Java Program
Palindrome String in Java
How to use ArrayList in Java
Why java is important to the Internet
Calculate gross salary in Java
Vector example in Java
Read from a COM port using Java program
Tomcat and httpd configured in port 8080 and 80
The Unary Operators example in Java
right shift operator, >>, in Java
Integer: byte, short, int, and long data types in Java
XML and Java - Parsing XML using Java Tutorial
Most Emailed Articles (in Java)
Using Charts in JasperReports
Introducing Nested and Inner Classes
Tomcat and httpd configured in port 8080 and 80
List all the serial and parallel ports in your system using Java
What is Unicode?
FileReader and FileWriter example program in Java
FilenameFilter - sample program in Java
Basics of Inheritance in Java
Method Overloading (function overloading) in Java
while - Iteration in java
how to use boolean data type in Java
equals( ) Versus == in Java
J2EE Components
Increment and Decrement Operator
Read from a COM port using Java program