# data[].attributes.body
Difference :

# data[].attributes.body
Comparison between string and stringbuffer (13DDT17F2013

# data[].attributes.body
coding stringbuffer

# data[].attributes.body
Coding String

# data[].attributes.body
import java.io.*;

class GFG {

public static void main(String[] args)

{

StringBuffer s = new StringBuffer("Geeksfor");

s.append("Geeks");

System.out.println(s); // returns GeeksforGeeks

s.append(1);

System.out.println(s); // returns GeeksforGeeks1

}

}

# data[].attributes.body
Muhammad Hisyam bin Ismail
13DDT17F1032

# data[].attributes.body
StringBuffer

# data[].attributes.body
CHE ALIF SHUHAIMAN BIN CHE KAMRUL
13DDT17F1036

# data[].attributes.body
String declaration and output

# data[].attributes.body
MUHAMMAD ADRYZIKRY BIN MOHD YUSOFF (13DDT17F1048)

# data[].attributes.body
MUZAKKIR (13DDT17F1047)

# data[].attributes.body
(13ddt17f1056)
String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be modified.

# data[].attributes.headline
Mohamad Haridz Hakimi Bin Ghazali 13DDT17F2045

# data[].attributes.subject
Mohamad Haridz Hakimi Bin Ghazali 13DDT17F2045

# data[].attributes.body
The Basics:

String is an immutable class, it can't be changed. StringBuilder is a mutable class that can be appended to, characters replaced or removed and ultimately converted to a String StringBuffer is the original synchronized version of StringBuilder

You should prefer StringBuilder in all cases where you have only a single thread accessing your object.

The Details:

Also note that StringBuilder/Buffersaren't magic, they just use an Array as a backing object and that Array has to be re-allocated when ever it gets full. Be sure and create your StringBuilder/Bufferobjects large enough originally where they don't have to be constantly re-sized every time .append() gets called.

The re-sizing can get very degenerate. It basically re-sizes the backing Array to 2 times its current size every time it needs to be expanded. This can result in large amounts of RAM getting allocated and not used when StringBuilder/Bufferclasses start to grow large.

In Java String x = "A" + "B"; uses a StringBuilder behind the scenes. So for simple cases there is no benefit of declaring your own. But if you are building String objects that are large, say less than 4k, then declaring StringBuilder sb = StringBuilder(4096); is much more efficient than concatenation or using the default constructor which is only 16 characters. If your String is going to be less than 10k then initialize it with the constructor to 10k to be safe. But if it is initialize to 10k then you write 1 character more than 10k, it will get re-allocated and copied to a 20k array. So initializing high is better than to low.

In the auto re-size case, at the 17th character the backing Array gets re-allocated and copied to 32 characters, at the 33th character this happens again and you get to re-allocated and copy the Array into 64 characters. You can see how this degenerates to lots of re-allocations and copies which is what you really are trying to avoid using StringBuilder/Buffer in the first place.

This is from the JDK 6 Source code for AbstractStringBuilder

   void expandCapacity(int minimumCapacity) {
    int newCapacity = (value.length + 1) * 2;
        if (newCapacity < 0) {
            newCapacity = Integer.MAX_VALUE;
        } else if (minimumCapacity > newCapacity) {
        newCapacity = minimumCapacity;
    }
        value = Arrays.copyOf(value, newCapacity);
    }

A best practice is to initialize the StringBuilder/Buffer a little bit larger than you think you are going to need if you don't know right off hand how big the String will be but you can guess. One allocation of slightly more memory than you need is going to be better than lots of re-allocations and copies.

Also beware of initializing a StringBuilder/Buffer with a String as that will only allocated the size of the String + 16 characters, which in most cases will just start the degenerate re-allocation and copy cycle that you are trying to avoid. The following is straight from the Java 6 source code.

public StringBuilder(String str) {
    super(str.length() + 16);
    append(str);
    }

If you by chance do end up with an instance of StringBuilder/Buffer that you didn't create and can't control the constructor that is called, there is a way to avoid the degenerate re-allocate and copy behavior. Call .ensureCapacity()with the size you want to ensure your resulting String will fit into.

The Alternatives:

Just as a note, if you are doing really heavy String building and manipulation, there is a much more performance oriented alternative called Ropes.

Another alternative, is to create a StringList implemenation by sub-classing ArrayList<String>, and adding counters to track the number of characters on every .append() and other mutation operations of the list, then override .toString() to create a StringBuilder of the exact size you need and loop through the list and build the output, you can even make that StringBuilder an instance variable and 'cache' the results of .toString() and only have to re-generate it when something changes.

Also don't forget about String.format()when building fixed formatted output, which can be optimized by the compiler as they make it better.

# data[].attributes.body
Muzakkir

# data[].attributes.body
If your string can change, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous so you have thread-safety.

# data[].attributes.body
String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values.

# data[].attributes.body
String is immutable and fixed length.If we are creating string which cannot be changed. So once we have created a string object we cannot change the value of string.

StringBuffer is a  peerclass of string that provides almost all functionality of strings.StringBuffer represents mutable, grow able and writable.

String code :

String str="hello";
 String strCon=str.concat("world");//return's new concatenated string,str not changed

Stringbuffer code :

StringBuffer strBuff=new StringBuffer("hello");
strBuff.append("world");//strBuff changed

# data[].attributes.body
Students..discuss about the topic given