The strings in Java are considered an important aspect of programming since strings are utilized almost everywhere, such as in Arrays, lists, and other data structures to perform the required tasks on the textual data. Therefore understanding how the strings work and what methods are used by the string class in Java is important. One of the methods that connects/joins two strings in Java is known as the join() method.
In this article, we will observe the working of the join() method in Java using different scenarios and examples.
How to Use the String join() Method in Java?
The string join() method in Java is responsible for joining the two strings with each other using a delimiter as per the requirement or choice. This method of Java returns the string using a delimiter in concatenated form. It is used in applications like printing date and time in Java.
For better understanding let us have a look at the syntax of the join() method first for the strings and then for the iterable objects in Java.
For strings
String join(CharSequence delimiter, CharSequence... elements)
In the above syntax for strings:
- The CharSequence delimiter represents the delimiter that will be used.
- Next shows the elements that are separated through the delimiter.
For iterable objects
String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
The only difference in the syntax occurs when the iterable objects like the object of an ArrayList are declared.
Example 1: Implementation Without a Delimiter
The below example is just implemented to show the working of the join() method when it is implemented without a delimiter in Java.
//Declare the class
class JoinExample1 {
//The main() method of Java
public static void main( String args[] ) {
//Create an object for the string
String output = String.join("Java", "is", "easy");
System.out.println("THE STRING USING THE join() METHOD IS " + output);
}
}
In the code above, when the delimiter is not entered with the string while implementing the join() method, an ambiguous output appears.
Output
The output printed is difficult to read and understand since there are no spaces or a delimiter between the
Strings.

Example 2: Simple Implementation of the join() Method in Java
The code example below shows a simple and easy implementation of the join() method in Java.
//Declare the class
class JoinExample1 {
//The main() method of Java
public static void main( String args[] ) {
//Create an object for the string and its delimiter
String deli = ":";
String output = String.join(deli, "Java", "is", "easy", "to", "Learn");
System.out.println("THE STRING USING THE join() METHOD IS " + output);
}
}
In the above code example:
- A class is declared as “JoinExample1” that implements the join() method.
- In the main method of Java, two strings are declared as “deli” and “output”.
- The string declared as “output” applies the delimiter “:” to join the strings together using the join() method.
- The result is printed using the println() method.
Output
The output below shows that the string “Java is easy to learn” is printed with a delimiter.

Example 2: Printing Date Using join() Method
One of the applications of the join() method is that it is used to print the DateTime values by declaring the delimiter of your choice as elaborated in the code example below.
//Declare the class
class JoinExample2 {
//The main() method of Java
public static void main( String args[] ) {
//Create an object for the string and its delimiter
String deli = "/";
String output = String.join(deli, "October", "10", "2023");
System.out.println("THE DATE USING THE join() METHOD IS " + output);
}
}
In the above Java code, the delimiter “/” is used for separating the month, date, and year.
Output
The below output shows that the date is printed using the join() method separated by a delimiter of our choice.

Example 3: Printing Time Using join() Method
Similar to the date, the time can also be printed using the join() method in Java as depicted below.
//Declare the class
class JoinExample3 {
//The main() method of Java
public static void main( String args[] ) {
//Create an object for the string and its delimiter
String deli = ":";
String output = String.join(deli, "12", "53", "57");
System.out.println("THE TIME USING THE join() METHOD IS " + output);
}
}
In the above code, the delimiter “:” is used to print the time using the join() method.
Output
The output below prints the time using the join() method that concatenates the hour, minute, and seconds as 12:53:57.

Example 4: The join() Method to Implement Iterable
The join() method of string can be implemented on ArrayList, Linked list, etc. In the code below, the elements declared in the ArrayList are combined using a specific delimiter.
import java.util.ArrayList;
class JoinExample4 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
// Add the elements
list.add("Programming");
list.add("in");
list.add("Java");
list.add("is");
list.add("easy");
String output;
//The delimiter that joins the string using the delimiter
output = String.join("-->", list);
//Print the output
System.out.println("THE join() METHOD WITH ITERABLE " +output);
}
}
In this Java code:
- The package for ArrayList is imported.
- A class is declared as “JoinEXample4”.
- Next, an object “list” is declared to insert elements to an ArrayList using the add() method of Java.
- The join() method uses the delimiter “–>” to join the elements declared in the ArrayList.
- The output is printed according to the declared input.
Output
In the below output, the elements of the ArrayList are printed in concatenated form using the join() method with the help of the delimiter declared as “–>”.

Exception While Working with String join() Method
When working with the string join() method, certain exceptions might arise in the code. In the code below when the delimiter is declared as “null”, Java throws an exception that “null” can not be a delimiter.
class StringJoinExample5
{
public static void main(String argvs[])
{
String a = null;
String output = String.join(a, "October", "10", "2023");
System.out.println("THE DATE USING THE join() METHOD IS " + output);
}
}
Output
The output below shows the “NullPointerException” which means that a null value is declared as a delimiter which is not allowed in Java.

Ambiguity in the Code
When the delimiter is not declared and the next element to be joined is also null, an ambiguity is displayed on the screen. The code below shows that the string “Java” is joined with a null element which raises an ambiguity in the code.
class JoinExceptions
{
// main method
public static void main(String argvs[])
{
String str;
str = String.join("Java", null);
System.out.println(str);
}
}
Output
The output below shows that “null” is considered “ambiguous”

The second part of the output window displays a pointer on null, which shows that “null” can not be added using the join() method.

This sums up the implementation of the string join() method in Java.
Conclusion
The join() method of string class in Java connects the string with other strings or iterable (with or without a delimiter). Java developers recommend the use of a delimiter while joining strings, as it removes ambiguity and enhances the code readability. In this article, we have implemented the join() method of Java in an easy and effective manner.