StringBuilder
1. This is a pre-defined-class in java.lang package (so, no need to import this package, ye package apne aap import ho jata hai)
2. This class is used to denote a mutable sequence of characters (means unlike string the content of StringBuilder can be changed)
note: In Java string is constant (bole toh immutable ya read-only)
3. This class was introduced in JDK 5
4. In order to implement immutability the StringBuilder uses a buffer (a type of memory) which can store 16 characters by default, this buffer will be automatically expanded when we store more characters than the buffer size
5. Each character inside buffer is indexed (first index starts from 0)
this is the formula how buffer resizes ==>
old-buffer-size * 2 + 2
so when we store 17th char the size of buffer will be automatically resized to 16 * 2 + 2 that is 34
Methods of StringBuilder class:
1. int capacity() <<== this method is used to fetch the size of buffer used by StringBuilder to store the data
2. int length() <<== this method is used to fetch no of characters inside the buffer
, by default no characters inside buffer are zero
3. StringBuilder append(any-type-of-data) <<== this method is used to store any type of data inside buffer at rear end (rear bole toh peeche ki taraf)
4. StringBuilder insert(int index,any-type-of-data) <<== this method is used to insert any type of data inside buffer at a given index
note: exception will be raised if index is invalid
5. StringBuilder delete(int startIndex, int endIndex) <<== this method is used to delete all the characters from startIndex to endIndex minus one
note: startIndex is inclusive but endIndex is exclusive
6. StringBuilder reverse() <<== this method is used to reverse the content of buffer
7. void trimToSize() <<== this method is used to make the size of buffer equal to the no of characters inside it
note: we can use this method for batter memory management
8. String toString() <<== this method is used to get a string from the object of StringBuilder
9. int indexOf(String str) <<== this method is used to get the first index of given string inside buffer, if string is not available it will return -1
10. int lastIndexOf(String str) <<== this method is used to get the last index of given string inside buffer, if string is not available it will return -1
# Below is the code to explain StringBuilder class #
Note: Please save this class inside TheStringBuilderDemo.java file
public class TheStringBuilderDemo
{
public static void main(String[] args)
{
// create an object of string-builder class
StringBuilder builder = new StringBuilder();
// fetch size of buffer
int bufferSize = builder.capacity();
System.out.println("initialy size of buffer is "+bufferSize);
// fetch no of elements inside buffer
int elements = builder.length();
System.out.println("initialy no of elements inside buffer are "+elements);
// store data inside buffer (at rear end)
builder.append("me");
builder.append(" some");
// show the data inside buffer
System.out.print("data in buffer after append: ");
System.out.println(builder);
// insert data at index 0
// all the data after index 0 will be shifted to right hand size
builder.insert(0, "give ");
// show the data inside buffer
System.out.print("data in buffer after insert: ");
System.out.println(builder);
// store data inside buffer (at rear end)
builder.append(" sunshine");
// show the data inside buffer
System.out.print("data in buffer after append: ");
System.out.println(builder);
// delete all the characters from from index 3 to index 6 minus 1
// here 'e vem' will be deleted from the buffer
builder.delete(3, 6);
// show the data inside buffer
System.out.print("data in buffer after delete: ");
System.out.println(builder);
// reverse the data of buffer
builder.reverse();
// show the data inside buffer
System.out.print("data in buffer after reverse: ");
System.out.println(builder);
// reverse the data of buffer again
builder.reverse();
// show the data inside buffer
System.out.print("data in buffer after re-reverse: ");
System.out.println(builder);
// fetch size of buffer again
bufferSize = builder.capacity();
System.out.println("size of buffer after some operations is "+bufferSize);
// fetch no of elements inside buffer
elements = builder.length();
System.out.println("no of elements in buffer after some operations "+elements);
}
}
output:
1. This is a pre-defined-class in java.lang package (so, no need to import this package, ye package apne aap import ho jata hai)
2. This class is used to denote a mutable sequence of characters (means unlike string the content of StringBuilder can be changed)
note: In Java string is constant (bole toh immutable ya read-only)
3. This class was introduced in JDK 5
4. In order to implement immutability the StringBuilder uses a buffer (a type of memory) which can store 16 characters by default, this buffer will be automatically expanded when we store more characters than the buffer size
5. Each character inside buffer is indexed (first index starts from 0)
this is the formula how buffer resizes ==>
old-buffer-size * 2 + 2
so when we store 17th char the size of buffer will be automatically resized to 16 * 2 + 2 that is 34
Methods of StringBuilder class:
1. int capacity() <<== this method is used to fetch the size of buffer used by StringBuilder to store the data
2. int length() <<== this method is used to fetch no of characters inside the buffer
, by default no characters inside buffer are zero
3. StringBuilder append(any-type-of-data) <<== this method is used to store any type of data inside buffer at rear end (rear bole toh peeche ki taraf)
4. StringBuilder insert(int index,any-type-of-data) <<== this method is used to insert any type of data inside buffer at a given index
note: exception will be raised if index is invalid
5. StringBuilder delete(int startIndex, int endIndex) <<== this method is used to delete all the characters from startIndex to endIndex minus one
note: startIndex is inclusive but endIndex is exclusive
6. StringBuilder reverse() <<== this method is used to reverse the content of buffer
7. void trimToSize() <<== this method is used to make the size of buffer equal to the no of characters inside it
note: we can use this method for batter memory management
8. String toString() <<== this method is used to get a string from the object of StringBuilder
9. int indexOf(String str) <<== this method is used to get the first index of given string inside buffer, if string is not available it will return -1
10. int lastIndexOf(String str) <<== this method is used to get the last index of given string inside buffer, if string is not available it will return -1
# Below is the code to explain StringBuilder class #
Note: Please save this class inside TheStringBuilderDemo.java file
public class TheStringBuilderDemo
{
public static void main(String[] args)
{
// create an object of string-builder class
StringBuilder builder = new StringBuilder();
// fetch size of buffer
int bufferSize = builder.capacity();
System.out.println("initialy size of buffer is "+bufferSize);
// fetch no of elements inside buffer
int elements = builder.length();
System.out.println("initialy no of elements inside buffer are "+elements);
// store data inside buffer (at rear end)
builder.append("me");
builder.append(" some");
// show the data inside buffer
System.out.print("data in buffer after append: ");
System.out.println(builder);
// insert data at index 0
// all the data after index 0 will be shifted to right hand size
builder.insert(0, "give ");
// show the data inside buffer
System.out.print("data in buffer after insert: ");
System.out.println(builder);
// store data inside buffer (at rear end)
builder.append(" sunshine");
// show the data inside buffer
System.out.print("data in buffer after append: ");
System.out.println(builder);
// delete all the characters from from index 3 to index 6 minus 1
// here 'e vem' will be deleted from the buffer
builder.delete(3, 6);
// show the data inside buffer
System.out.print("data in buffer after delete: ");
System.out.println(builder);
// reverse the data of buffer
builder.reverse();
// show the data inside buffer
System.out.print("data in buffer after reverse: ");
System.out.println(builder);
// reverse the data of buffer again
builder.reverse();
// show the data inside buffer
System.out.print("data in buffer after re-reverse: ");
System.out.println(builder);
// fetch size of buffer again
bufferSize = builder.capacity();
System.out.println("size of buffer after some operations is "+bufferSize);
// fetch no of elements inside buffer
elements = builder.length();
System.out.println("no of elements in buffer after some operations "+elements);
}
}
output:
ur concept is so gud,sir
ReplyDelete