String vs StringBuffer vs StringBuilder in Java

1. Objective

After studying Array vs ArrayList in Java, we are going to explore the difference between String vs StringBuffer vs StringBuilder in Java. Moreover, we will see a brief intro of Java String, StringBuffer and StringBuilder. In addition to this comparison of string and StringBuffer in Java, we will look at the use of StringJoiner in Java. 

So, let us start String vs StringBuffer vs StringBuilder in Java.

String vs StringBuffer vs StringBuilder in Java

String vs StringBuffer vs StringBuilder in Java

2. What is Java String?

Prerequisites – Strings in Java, Java.util.StringJoiner

Strings are a group of characters that cannot be changed once created.

3. String vs StringBuffer vs StringBuilder Examples

The program below explains the three types that are String vs StringBuffer vs StringBuilder in Java.

Do you know What is StringBuffer in Java Programming?

class DataFlair
{
   public static void concat1(String s1)
   {
       s1 = s1 + "Flair";
   }
   public static void concat2(StringBuilder s2)
   {
       s2.append("Flair");
   }
   public static void concat3(StringBuffer s3)
   {
       s3.append("Flair");
   }
   public static void main(String[] args)
   {
       String s1 = "Data";
       concat1(s1);          System.out.println("String: " + s1);
       StringBuilder s2 = new StringBuilder("Data");
       concat2(s2);
       System.out.println("StringBuilder: " + s2);
       StringBuffer s3 = new StringBuffer("Data");
       concat3(s3);
       System.out.println("StringBuffer: " + s3);
   }
}

Output:
String: Data
StringBuilder: DataFlair
StringBuffer: DataFlair
Clarification:

  1. Concat1: In this strategy, we pass a string “Data” and perform “s1 = s1 + “Flair”. The string goes from main() isn’t changed, this is because of the way that String is permanent. Adjusting the benefit of string makes another protest and s1 in concat1() stores reference of a new string. References s1 in main() and cocat1() allude to various strings.
  2. Concat2: In this technique, we pass a string “Data” and perform “s2.append(“Flair”)”, which changes the real estimation of the string (in fundamental) to “DataFlair”. This is because of the straightforward reality that StringBuilder is impermanent and henceforth changes its esteem.
  3. Concat3: StringBuffer is like StringBuilder aside from one contrast that StringBuffer is string safe, i.e., different strings can utilize it with no issue. The string security brings a punishment of execution.

When to use which –

  • On the off chance that a string will stay steady all through the program, at that point utilize String class question on the grounds that a String object is changeless.
  • In the event that a string can change (illustration: bunches of rationale and tasks in the development of the string) and might be gotten to from a solitary string, utilizing a StringBuilder is adequate.
  • In the event that a string can change, and will be gotten to from numerous strings, utilize a StringBuffer on the grounds that StringBuffer is synchronous so you have string security.

Let’s Learn about StringTokenizer In Java

a. A change between sorts of strings in Java

In some cases, there is a need of changing over a string object of various classes like String, StringBuffer, StringBuilder to each other. The following are a few procedures to do likewise.
At the point when to utilize which one –

i. From StringBuffer and StringBuilder to String

These transformations can be performed utilizing toString() technique which is abrogated in both StringBuffer and StringBuilder classes.

The following is the java program to exhibit the same. Note that while we utilize toString() technique, another String object(in Heap region) is assigned and instated to character grouping presently spoke to by StringBuffer question, that implies the consequent changes to the StringBuffer protest don’t influence the substance of the String object.

public class Test
{
   public static void main(String[] args)
   {
       StringBuffer sbr = new StringBuffer("Data")
       String str = sbr.toString();
       System.out.println(str);
       String str1 = sbr.toString();
       System.out.println(str1);
       sbr.append("Flair");
       System.out.println(sbr);
       System.out.println(str);
   }
}

ii. From StringBuffer to StringBuilder or the other way around

This change is tricky, there is no immediate method to change over the same. For this situation, we can utilize a String class Object. We first change over StringBuffer/StringBuilder object String utilizing toString() technique and afterwards from String to StringBuilder/StringBuffer utilizing constructors. Below is the java program to show the same.

Java Extends vs Implements With Example Program

public class Test
{
   public static void main(String[] args)
   {
       StringBuffer sbr = new StringBuffer("Data");
       String str = sbr.toString();
       StringBuilder sbl = new StringBuilder(str);
       System.out.println(sbl);
   }
}

iii. From String to StringBuffer and StringBuilder

We can straightforwardly pass String class question StringBuffer and StringBuilder class constructors. As String class is unchanging in java, so to edit a string, we can perform same by changing over it to StringBuffer or StringBuilder class objects. The following is the java program to show the same.

public class Test
{
   public static void main(String[] args)
   {
       String str = "Data";
       StringBuffer sbr = new StringBuffer(str);
       sbr.reverse();
       System.out.println(sbr);
       StringBuilder sbl = new StringBuilder(str);
       sbl.append("Flair");
       System.out.println(sbl);
   }
}
Java Quiz

4. When to use StringJoiner over StringBuilder

StringJoiner is extremely helpful when you have to join Strings in a Stream.

How to Read Java Console Input | 3 Ways To Read Java Input

Errand: Suppose we need the string “[George:Sally:Fred]”, where we have given a string cluster that contains “George”, “Sally” and “Fred”.

StringJoiner give add(String str) strategy to connect the strings in light of provided delimiter, prefix and addition in the constructor, yet in the event that we utilize StringBuilder to play out our assignment then first we need to attach prefix and afterward emphasize through string exhibit and affix the required delimiter after every component lastly add the prefix. The following is the java program to show both ways.

import java.util.StringJoiner;
public class Test
{
   public static void main(String[] args)
   {
       String str[] = {"George","Sally","Fred"};
       StringJoiner sj = new StringJoiner(":", "[", "]");
       sj.add("George").add("Sally").add("Fred");
       String desiredString = sj.toString();
       System.out.println(desiredString);
       StringBuilder sb = new StringBuilder();
       sb.append("[");
       if(str.length>0)
       {
           sb.append(str[0]);
           for (int i = 1; i < str.length; i++)
           {
               sb.append(":").append(str[i]);
           }
       }
       sb.append("]");
       String desiredString1 = sb.toString();
       System.out.println(desiredString1);
   }
}

Output –
[George:Sally:Fred]

[George:Sally:Fred]

So, this was all about String vs StringBuffer vs StringBuilder in Java. Hope you like our explanation.

Let’s Discuss How to Detect & Reduce Deadlocks in java?

5. Conclusion

Hence, in this Java tutorial, we learned about the String, StringBuffer and StringBuilder class in Java. Moreover, we saw a comparison of String vs StringBuffer vs StringBuilder. Also, we discussed when to use StringJoiner over StringBuider. Furthermore, if you have any query regarding Java String vs StringBuffer vs StringBuilder, feel free to ask through the comment section.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.