Question
I need to combine two String arrays into one array in Java.
void f(String[] first, String[] second) {
String[] both = ???;
}
What is the simplest and most practical way to concatenate these two arrays?
Short Answer
By the end of this page, you will understand how array concatenation works in Java, why arrays cannot grow automatically, and how to combine two String[] values into a new array using standard Java techniques such as System.arraycopy, loops, and streams.
Concept
In Java, an array has a fixed size. Once you create it, you cannot expand it. That is why concatenating two arrays does not mean modifying one of them to make room for the other. Instead, you create a new array large enough to hold both arrays, then copy the elements from the first array and the second array into it.
For example, if first has length 2 and second has length 3, the combined array must have length 5.
This matters because arrays are common in Java, especially when working with low-level APIs, method parameters, configuration values, and performance-sensitive code. Understanding that arrays are fixed-size helps you choose the right tool:
- Use an array when the size is known or fixed.
- Use a List when you need something that can grow or shrink.
For concatenating arrays, the core idea is always the same:
- Create a new array with enough space.
- Copy the first array into the beginning.
- Copy the second array after it.
Mental Model
Think of an array like a row of numbered boxes.
If you have one row with 3 boxes and another row with 2 boxes, you cannot "stretch" the first row to fit more boxes. Instead, you build a new row with 5 boxes and move everything into it.
- Boxes
0tofirst.length - 1hold the first array. - Boxes starting at
first.lengthhold the second array.
So concatenation in Java arrays is really create a new container and copy values into it.
Syntax and Examples
Basic approach with System.arraycopy
void f(String[] first, String[] second) {
String[] both = new String[first.length + second.length];
System.arraycopy(first, 0, both, 0, first.length);
System.arraycopy(second, 0, both, first.length, second.length);
}
How it works
new String[first.length + second.length]creates a new array large enough for both inputs.- The first
arraycopycopies all elements fromfirstintobothstarting at index0. - The second
arraycopycopies all elements fromsecondintobothstarting at indexfirst.length.
Complete example
import java.util.Arrays;
public class Main {
{
String[] first = {, };
String[] second = {, };
String[] both = [first.length + second.length];
System.arraycopy(first, , both, , first.length);
System.arraycopy(second, , both, first.length, second.length);
System.out.println(Arrays.toString(both));
}
}
Step by Step Execution
Consider this example:
String[] first = {"red", "blue"};
String[] second = {"green", "yellow"};
String[] both = new String[first.length + second.length];
System.arraycopy(first, 0, both, 0, first.length);
System.arraycopy(second, 0, both, first.length, second.length);
Step 1: Create the input arrays
firstcontains 2 elements:"red","blue"secondcontains 2 elements:"green","yellow"
Step 2: Create the result array
String[] both = new String[first.length + second.length];
first.lengthis2second.lengthis2- total size is
4
Real World Use Cases
Array concatenation appears in many practical Java programs:
- Command-line tools: combine default arguments with user-provided arguments.
- Configuration loading: merge built-in values with environment-specific values.
- Data processing: join batches of values before passing them to another method.
- Testing: combine base test data with case-specific inputs.
- API adapters: merge headers, tokens, or parameter arrays before sending data onward.
Example: combining default and extra tags:
String[] defaultTags = {"java", "backend"};
String[] extraTags = {"arrays", "strings"};
String[] allTags = new String[defaultTags.length + extraTags.length];
System.arraycopy(defaultTags, 0, allTags, 0, defaultTags.length);
System.arraycopy(extraTags, 0, allTags, defaultTags.length, extraTags.length);
This pattern is simple, fast, and common when an API specifically requires arrays.
Real Codebase Usage
In real projects, developers often choose one of these patterns depending on context.
1. Utility method for reuse
If concatenation happens in several places, it is usually wrapped in a helper method.
public static String[] concat(String[] first, String[] second) {
String[] result = new String[first.length + second.length];
System.arraycopy(first, 0, result, 0, first.length);
System.arraycopy(second, 0, result, first.length, second.length);
return result;
}
This keeps calling code clean.
2. Guard clauses for null
In production code, arrays may sometimes be null.
public static String[] concat(String[] first, String[] second) {
if (first == null) return second == null ? new String[0] : second.clone();
if (second == null) return first.clone();
String[] result = new String[first.length + second.length];
System.arraycopy(first, 0, result, , first.length);
System.arraycopy(second, , result, first.length, second.length);
result;
}
Common Mistakes
1. Trying to use + like strings
Broken code:
String[] both = first + second;
Why it fails:
- Java allows
+for strings and numbers. - It does not concatenate arrays this way.
2. Forgetting to create enough space
Broken code:
String[] both = new String[first.length];
System.arraycopy(second, 0, both, first.length, second.length);
Why it fails:
bothis too small.- Copying the second array will cause an
ArrayIndexOutOfBoundsException.
Fix:
String[] both = new String[first.length + second.length];
3. Using the wrong destination start index
Broken code:
System.arraycopy(second, 0, both, 0, second.length);
Why it is wrong:
Comparisons
| Approach | Example | Best for | Notes |
|---|---|---|---|
System.arraycopy | Copy first and second into a new array | Most common and efficient array concatenation | Clear and fast |
Arrays.copyOf + System.arraycopy | Copy first, then append second | Readable code | Convenient when starting from the first array |
| Manual loop | Use for loops to copy values | Learning or custom logic | More verbose |
| Stream API | Stream.concat(...) | Functional-style code | Less direct for simple arrays |
ArrayList |
Cheat Sheet
Quick reference
Concatenate two String[] arrays
String[] both = new String[first.length + second.length];
System.arraycopy(first, 0, both, 0, first.length);
System.arraycopy(second, 0, both, first.length, second.length);
Alternative
String[] both = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, both, first.length, second.length);
System.arraycopy parameters
System.arraycopy(source, sourceStart, destination, destinationStart, length);
Important rules
- Arrays in Java have fixed length.
- Concatenation creates a new array.
- The destination array must be large enough for all elements.
- The second copy should start at index
first.length. - If
nullis possible, check for it before using.length.
Common pattern
String[] concat(String[] first, String[] second) {
String[] result = [first.length + second.length];
System.arraycopy(first, , result, , first.length);
System.arraycopy(second, , result, first.length, second.length);
result;
}
FAQ
How do you concatenate two arrays in Java?
Create a new array with combined length, then copy the first and second arrays into it using System.arraycopy or a loop.
Can I use + to join arrays in Java?
No. The + operator does not concatenate arrays in Java.
What is the easiest way to combine two String[] arrays?
A common choice is System.arraycopy, because it is simple, built into Java, and efficient.
Is there a built-in one-line method for array concatenation in Java?
Not for plain arrays in core Java. You usually combine Arrays.copyOf and System.arraycopy, or use streams.
Should I use arrays or ArrayList?
Use arrays when the size is fixed. Use ArrayList when you need to add or remove items often.
What happens if one array is empty?
The code still works. The result will simply contain the elements from the non-empty array.
What happens if one array is null?
You will get a NullPointerException unless you handle first.
Mini Project
Description
Build a small Java utility method that merges two arrays of tags into one result array. This demonstrates the standard array concatenation pattern used in real applications when data comes from two separate sources.
Goal
Create a reusable method that concatenates two String[] arrays and prints the combined result.
Requirements
- Create a method named
concatthat accepts twoString[]parameters. - Return a new array containing all elements of the first array followed by all elements of the second array.
- Use standard Java array copying instead of converting to a list.
- In
main, call the method with sample data and print the result. - Ensure the code runs as a complete Java program.
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert a Java Stack Trace to a String
Learn how to convert a Java exception stack trace to a string using StringWriter and PrintWriter, with examples and common mistakes.