Basic Java – 23 || String, StringBuffer & StringBuilder in Java.

All these classes String, StringBuffer & StringBuilder used to declare, initialize, and manipulation a String objects.

String

A String is a class we use to declare and initialize the string literals. These string literals are basically the group of characters.

In general, strings are immutable. In other words, each time we perform any modification in the existing String object or variable it creates a new object and allocates new memory.

To get better understanding on String visit below article:

StringBuilder

We can’t create string literals using the StringBuilder class. We have to use a new keyword to create a String object.

Unlike String, objects of StringBuilder are mutable. All the modifications or updates would be reflected in the original variable.

StringBuilder Class would be used where we are using single threads in the program.

StringBuffer

We can’t create string literals using the StringBuffer class. We have to use a new keyword to create a String object.

As StringBuilder, these are also mutable. The key difference is that StringBuffer objects are used in multi-threading programs. Due to this feature, it is called the thread-safe mutable sequence of characters.

Mutable vs Immutable

To understand this see the below code. We have created a String literal or object try to modify the text using the updateString(String str) method. And we will observe that there is no change in the original String variable.

public class String_Immutable {
	public static void main(String[] args) {
		
		// The same program can be try with below code, it will give same output:
		// String temp=new String("Java");
		
		String temp="Java";
		String_Immutable obj=new String_Immutable();
		
		System.out.println("Before modification value of temp="+temp);
		obj.updateString(temp);
		
		System.out.println("After modification value of temp="+temp);
		
	}
	
	public void updateString(String test)
	{
		test=test.concat(" Program.");
	}
}

Output:
Before modification value of temp=Java
After modification value of temp=Java

Now will try the same program using StringBuffer and StringBuilder objects. Here, you can see that modification or changes are reflecting in the original String object.

public class StringBuilder_Mutable {

	public static void main(String[] args) {
		
		// The same program can be try with below code, it will give same output:
		// String StringBuffer=new StringBuffer("Java");
		
		StringBuilder temp=new StringBuilder("Java");
		StringBuilder_Mutable obj=new StringBuilder_Mutable();
		
		System.out.println("Before modification value of temp="+temp);
		obj.updateString(temp);
		
		System.out.println("After modification value of temp="+temp);
		
	}
	
	public void updateString(StringBuilder test)
	{
		test.append(" Program");
	}
}

Output:
Before modification value of temp=Java
After modification value of temp=Java Program

Hope now you understand the difference between mutable and immutable strings. Please let us know in case of any further queries or questions.

Other Topics

If you want to learn more in Java and Selenium visit the below links. These blogs created in such a way, so that readers can understand the concepts very easily.


TestNG:

Computer Basics:

Java Basics:
Java Collection:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


Leave a Comment

Your email address will not be published. Required fields are marked *

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.