String builder in Java
Since string variables are immutable, once we create a string variable we cannot delete or perform any manipulation task on that string variable. It is a time consuming process in Java to modify or delete any string.
String builder is a class in Java
language which is an optimized class to deal with problems in slow memory
operations in case of deletion or manipulation in string variables.
String builder can easily perform
deletion or manipulation task and does not take too much time. So whenever
there is an operation which involves deletion or manipulation of string
variable, we do not use only string rather we us string builder.
Java String builder
vs. string:
Let’s us try to understand why strings
are laid back compared to string. When we create any string variable it
occupies a space in the memory. The pointer points to the memory location where
the string value is stored.
Every time we make any changes in the
string variable, there is a new memory location getting occupied in the memory
with the updated string value and the variable will be pointing to a new memory
location every time.
Since a new memory location is being
assigned with every modification in the variable, the old memories get deleted.
This entire process is time consuming.
If the user is dealing with huge
amount of data, it will certainly lag the executive of the program and user
experience will be compromised.
In order to solve this issue string
builder class has been introduced. In string builder class, same deletion or
manipulation operation is performed very fast and every time we make any
changes in the variable, the same memory address gets updated every time so it
saves lot of time.
How to use
string builder:
String builder class is quite similar
to string variable. A lot of functions or methods which is used with string
variable can also be used with string builder class.
Moreover, for modification operations
there are new and advanced methods available in string builder.
How to
declare string builder class in Java:
String builder class can be declared
in Java with ‘new’ keyword. Please
check below the declaration of a string builder in Java language.
public class JavaOperators { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Akshay"); System.out.println(sb); } } |
The functions which were used with
string variable can also be used with string builder.
The charAt() function can be
used with string builder to find the character at any given index position in a
string builder.
So far we have seen functions and
methods which could only access and print values of string and string builder.
Unlike string variable, in string
builder class there are some important methods which can also modify or delete
values of a string builder.
Some Methods
used in String Builder:
Functions /
Methods |
Description |
Arguments
Passed |
setCharAt(); |
To set character at a given index. The
existing character will be replaced by new character. |
Index position & Character to be set. |
insert(); |
To insert a character at a given index. The
existing & next characters will be shifted to right by one index
position. |
Index position & Character to be inserted. |
delete(); |
To delete a character or a substring from
the string builder class. |
Starting index position & ending index
position. |
append(); |
To add a character or a string at the end of
the string builder. |
The character or string to be appended. |
We can also print the length of the
string builder with the help of .length()
method.
Conclusion:
We hope the concept for string builder
has been clear to you. If you have any question you can write to us.
No comments: