Skip to main content

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.

 

Comments

Popular posts from this blog

What is Java

Java is a general purpose programming language. Which means this programming language is used in many purposes apart from being used only in android application development. There are a few similarities between Java programming language and C++ programming language. But Java programming language is having some advanced features which you are going to learn in future. In programming languages you write programs in the form of statements in many lines and those statements are executed one by one. In Java, the execution happens really fast and concurrently it can execute many statements. Java is a platform independent (Java compiled byte code can be run across different operating systems) and object oriented programming language.   Why should we learn Java programming language?   Easy to learn: Java is a easy to learn programming language. Once you start learning Java programming, you will actually be learning the real programming and you will observe everyth...

Java User Input Exercise

Exercise 1: Write a simple java program to take user input of a number and store it inside an integer variable. Later print the number on the screen.   Solution 1:   import java.util.Scanner;   public class UserInpur {        public static void main(String[] args ) {               Scanner sc = new Scanner(System. in );           System. out .println( "Enter a number" );           int x = sc .nextInt();           System. out .println( "You have entered:" );                   System. out .println( x );        }   }   Exercise 2: Write a simple java program to tak...

Java Operators

In this lesson you are goin g to learn about operators in Java and also what are the different types of operators used in Java programming.   In our previous lesson we have learnt how to take user input in Java. We were performing different types of arithmetic operations in order to create a simple ration shop billing application which took different values as the user input and calculated total billing amount.   During the programming the billing application we used different signs like addition (+) , multiplication (*) etc. These signs are called operators in Java programming.   Types of Operators in Java There are five types of operators in Java. Arithmetic Operators Bitwise Operators Assignment Operators Comparison Operators Logical Operators 1.Java Arithmetic Operators Arithmetic operators are used to perform mathematical operations such addition, division, multiplication, subtraction and modulation. Let’s see the example of arithmetic operators in below:   ...