Java – Convert Array to String
To convert Array to String in Java, call Arrays.toString() method and pass the array as argument. Arrays.toString() returns a String with the elements enclosed in square brackets.
Examples
In the following example, we take two integer arrays: arr1 and arr2, and check if these two arrays are equal using Arrays.equals() method.
Main.java
</>
Copy
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = {25, 87, 69, 55};
String str = Arrays.toString(arr);
System.out.println(str);
}
}
Output
[25, 87, 69, 55]
Now, let us take an array of Strings, convert this array to String, and print to console.
Main.java
</>
Copy
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] arr = {"apple", "banana", "cherry"};
String str = Arrays.toString(arr);
System.out.println(str);
}
}
Output
[apple, banana, cherry]
Conclusion
In this Java Tutorial, we learned how to convert given Array to String in Java using Arrays.toString() method with examples.