A quick and easy way to join array elements with a separator (the opposite of split) in Java
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The opposite of splitting a string is joining values together with a separator. In Java, the easiest solution depends on what kind of array you have. For String[], String.join is usually the cleanest answer. For non-string values or more complex formatting, streams, Collectors.joining, or StringJoiner give you more control.
Use String.join for String[]
If your array already contains strings, this is the simplest option:
This is the direct inverse of many split use cases and is the best default for ordinary string arrays.
Use Streams for Non-String Arrays
If the array contains numbers or other objects, convert each element to text first.
This is also useful when you want filtering or transformation before joining.
Join Primitive Arrays
Primitive arrays such as int[] need a slightly different approach because they are not arrays of objects.
That is the clean way to handle primitive numeric arrays without manual loops.
Use StringJoiner for Incremental Construction
If values arrive over time instead of starting in a finished array, StringJoiner is a good fit.
This is useful in loops, builders, and formatting code where you do not want to manage separator placement manually.
Manual StringBuilder Still Has a Place
If you are on very old Java or need highly custom joining logic, a StringBuilder loop still works well.
This is more verbose than String.join, but it is flexible and explicit.
Watch Out for null Values
Joining becomes less obvious when arrays may contain null. Depending on the API and input, you may get "null" text or a NullPointerException.
With streams, you can normalize the values before joining:
If null values are possible, decide explicitly whether to skip them, replace them, or fail fast.
Common Pitfalls
The most common mistake is using Arrays.toString(array) and expecting a plain joined string. That method adds square brackets and formatting intended for debugging, not structured output.
Another issue is trying to use String.join directly on non-string arrays such as Integer[] or int[]. Convert elements to strings first.
Some code also manually appends separators at the end and then trims them later. That works, but it is usually less clean than String.join, Collectors.joining, or StringJoiner.
Finally, think about null handling up front. Joining code is simple until unexpected missing values show up.
Summary
- Use
String.joinforString[]and other string collections. - Use streams plus
Collectors.joiningfor object arrays that need conversion. - Use
mapToObjfor primitive arrays such asint[]. - Use
StringJoinerwhen values are added incrementally. - Avoid
Arrays.toStringwhen you need real separator-based joining output.

