StringJoiner setEmptyValue() method in Java Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report The setEmptyValue(CharSequence emptyValue) of StringJoiner sets the sequence of characters to be used when determining the string representation of this StringJoiner and no elements have been added yet, that is, when it is empty. A copy of the emptyValue parameter is made for this purpose. Note that once an add method has been called, the StringJoiner is no longer considered empty, even if the element(s) added correspond to the empty String. Syntax: public StringJoiner setEmptyValue(CharSequence emptyValue) Parameters: This method accepts a mandatory parameter emptyValue which is the characters to return as the value of an empty StringJoiner Returns: This method returns this StringJoiner itself, so the calls may be chained Exception: This method throws NullPointerException when the emptyValue parameter is null Below examples illustrates the setEmptyValue() method: Example 1: Java // Java program to demonstrate // setEmptyValue() method of StringJoiner import java.util.StringJoiner; public class GFG { public static void main(String[] args) { // Create a StringJoiner StringJoiner str = new StringJoiner(" "); // Print the empty StringJoiner System.out.println("Initial StringJoiner: " + str); // Add an emptyValue // using setEmptyValue() method str.setEmptyValue("StringJoiner is empty"); // Print the StringJoiner System.out.println("After setEmptyValue(): " + str); // Add elements to StringJoiner str.add("Geeks"); str.add("forGeeks"); // Print the StringJoiner System.out.println("Final StringJoiner: " + str); } } Output:Initial StringJoiner: After setEmptyValue(): StrigJoiner is empty Final StringJoiner: Geeks forGeeks Example 2: To demonstrate NullPointerException Java // Java program to demonstrate // setEmptyValue() method of StringJoiner import java.util.StringJoiner; public class GFG { public static void main(String[] args) { // Create a StringJoiner StringJoiner str = new StringJoiner(" "); // Print the empty StringJoiner System.out.println("Initial StringJoiner: " + str); try { // Add a null emptyValue // using setEmptyValue() method str.setEmptyValue(null); } catch (Exception e) { System.out.println("Exception when adding null" + " in setEmptyValue(): " + e); } } } Output:Initial StringJoiner: Exception when adding null in setEmptyValue(): java.lang.NullPointerException: The empty value must not be null Time complexity: O(1). Auxiliary Space: O(1). Comment More infoAdvertise with us Next Article StringJoiner setEmptyValue() method in Java C code_r Follow Improve Article Tags : Java Java - util package Java-Functions Java-StringJoiner Practice Tags : Java Similar Reads StringJoiner add() method in Java The add(CharSequence newElement) of StringJoiner adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null, then "null" is added.Syntax: public StringJoiner add(CharSequence newElement) Parameters: This method takes a mandatory parameter newElem 1 min read StringJoiner merge() method in Java The merge(StringJoiner other) of StringJoiner adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty. If the given StringJoiner is empty, the call has no effect.Syntax: public StringJoiner merge(StringJoiner other) Parameters: This method accepts 3 min read StringJoiner length() method in Java The length() of StringJoiner is used to find out the length of the StringJoiner in characters. It returns the length of the String representation of this StringJoiner. Note that if no add methods have been called, then the length of the String representation (either prefix + suffix or emptyValue) wi 2 min read StringJoiner toString() method in Java The toString() of StringJoiner is used to convert StringJoiner to String. It returns the current value, consisting of the prefix, the values added so far separated by the delimiter, and the suffix, unless no elements have been added in which case, the prefix + suffix or the emptyValue characters are 2 min read StringBuilder append() Method in Java In Java, the append() method of StringBuilder class is used to add data to the end of an existing StringBuilder object. It supports appending different data types like strings, integers, characters, and booleans by making it easy for dynamic string manipulation.Example 1: Here, we use the append() m 4 min read Like