0% found this document useful (0 votes)
16 views5 pages

String Buffer and String Builder Lyst5543

string buffer is an java mutable class

Uploaded by

Ramya Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

String Buffer and String Builder Lyst5543

string buffer is an java mutable class

Uploaded by

Ramya Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Mutable String

Therefore mutable strings are those strings whose content can be


changed without creating a new object. StringBuffer and StringBuilder
are mutable versions of String in java.

StringBuffer Constructors:
● StringBuffer(): This creates an empty StringBuffer with a default capacity of 16

characters.

● StringBuffer(int capacity): This creates an empty StringBuffer with a specified

capacity.

● StringBuffer(CharSequence charseq): This creates a StringBuffer containing

the same characters as in the specified character sequence.

● StringBuffer(String str): Creates a StringBuffer corresponding to specific

string.

public class Demo{

public static void main(String[] args) {


StringBuffer st = new StringBuffer(); // Creating
object of StringBuffer
System.out.println(st.capacity());// Initial default
capacity is 16
}
}

Output:
16
In the above code StringBuffer object is created by default 16 memory is
allocated.

Let’s see how to append strings to StringBuffer.

public class Demo{

public static void main(String[] args) {


StringBuffer st = new StringBuffer();
System.out.println(st.capacity());
st.append("Java"); // Java is added to string Buffer
st.append("JavaScript"); // JavaScript is added to
string Buffer
System.out.println(st);
}
}

Output:

Let’s see if you try to append the string more than the default capacity
public class Demo{

public static void main(String[] args) {


StringBuffer st = new StringBuffer();
System.out.println(st.capacity());
st.append("Java");
st.append("JavaScript");
System.out.println(st);
st.append("James Gosling");
System.out.println(st.capacity()); //once the capacity
is full size will be increase by (capacity*2)+2
}
}

Output:
If the size is larger than the size of characters then you can reduce the storage
using trimToSize().

public class Demo{

public static void main(String[] args) {


StringBuffer st = new StringBuffer();
System.out.println(st.capacity());
st.append("Java");
st.append("JavaScript");
System.out.println(st);
st.append("James Gosling");
System.out.println(st.capacity());
st.trimToSize();
System.out.println(st.capacity());

}
}

Output:
String Tokenizer:

StringTokenizer class in Java is used to break a string into tokens.

start

true
false
hasMoreToken()

nextToken()

stop

import java.util.StringTokenizer;

public class Demo{


public static void main(String[] args) {
String s = "JAVA PYTHON SQL AI";
StringTokenizer st = new StringTokenizer(s);

while(st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}

Output:

You might also like