Java StringJoiner Vs StringBuilder Classes

By Preeti Jain Last updated : February 02, 2024

In this tutorial, we will learn about the StringJoiner and StringBuilder Classes in Java with their usages, syntaxes, examples, and differences.

What is StringJoiner class in Java?

The StringJoiner class is a final class available in java.util package, which is used to concatenate strings. It provides an add() method to join the strings based on a given delimiter, prefix, and suffix in the constructor simultaneously.

Syntax

Below is the syntax to create an object of the StringJoiner class:

StringJoiner object_name = new StringJoiner(delimeter,prefix,suffix);

Example of StringJoiner Class

Let's suppose we are expecting the string like "{"Java"; "C"; "C++"; "Ruby"}" where we have given a string array that contains "Java" , "C" , "C++" , "Ruby"

import java.util.StringJoiner;

public class StringJoinerClass {
  public static void main(String[] args) {
    // Creating a constructor of StringJoiner class
    StringJoiner strj = new StringJoiner(";", "{", "}");

    // add few elements to StringJoiner for concatenation 
    // by using add(String str) method
    strj.add("Java");
    strj.add("C");
    strj.add("C++");
    strj.add("Ruby");

    // Display concatenation of strings by StringJoiner class
    System.out.println("The result after concatenation is :" + strj);
  }
}

Output

The output of the above example is:

The result after concatenation is :{Java;C;C++;Ruby}

What is StringBuilder class in Java?

StringBuilder class is of java.lang package. It is also used to concatenate the strings and to create mutable sequences of characters. StringBuilder class provides a method append() to append the strings. During the creation of its object, there is no need to pass parameters like delimiter, prefix, and suffix.

Syntax

Below is the syntax to create an object of the StringBuilder class:

StringBuilder strb = new StringBuilder();

Example of StringBuilder Class

Let's suppose we are expecting the string like "{"Java"; "Programming"}" where we have given a string array that contains "Java", "Programming". We will see what methodology follows to solve this problem by using the StringBuilder class.

import java.lang.StringBuilder;

class StringBuilderClass {
  public static void main(String[] args) {
    // Creating a constructor of StringBuilder 
    // class with passing no argument
    StringBuilder strb = new StringBuilder();

    // add few elements to StringJoiner for concatenation 
    // by using add(String str) method

    // First we will append prefix if we want
    strb.append("{");

    // Second we will append required delimeter after every element
    strb.append("Java");
    strb.append(";").append("C");
    strb.append(";").append("C++");

    // Finally we will append suffix if we want
    strb.append("}");

    // Display concatenation of strings by StringBuilder class
    System.out.println("The result after concatenation is :" + strb);
  }
}

Output

The output of the above example is:

E:\Programs>javac StringBuilderClass.java

E:\Programs>java StringBuilderClass
The result after concatenation is :{Java;C;C++}

Differences between StringJoiner and StringBuilder classes in Java

StringJoiner and StringBuilder classes can be used to modify the strings. They can be used based on their purposes. StringJoiner class is thread-safe and used for constructing a sequence of characters separated by the parameters whereas, the StringBuilder class does not provide thread safety but it is more flexible than the StringJoiner class represents a mutable sequence of characters.

The table below shows the differences based on the features:

FeatureStringJoinerStringBuilder
Mutability StringJoiner class represents immutable character of sequences. StringBuilder class represents mutable character of sequences.
Thread safety StringJoiner class is thread-safe. StringBuilder class is not thread-safe.
Performance StringJoiner class is slower. StringBuilder class is faster.
Use case StringJoiner class should be used for joining multiple strings. StringBuilder class should be used for modifying a string multiple times.

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.