Arrays.toString() in Java with Examples
Last Updated : 25 Nov, 2024
The Arrays.toString() method belongs to the Arrays class in Java. It converts an array into its string representation consisting of a list of the array's elements. In the case of an Object Array, if the array contains other arrays as elements, their string representation shows memory addresses instead of contents.
Example:
Below is the simplest way to print an array as a string using Arrays.toString() method:
Java import java.util.Arrays; public class ArrayToString { public static void main(String[] args) { // create an integer array int[] n = {1, 2, 3, 4,}; // print the array using // Arrays.toString() System.out.println(Arrays.toString(n)); } }
Syntax of Arrays.toString() method
public static String toString(int[] array)
public static String toString(Object[] array)
Parameters: array: The array, whose string representation to return.
Return Type:
- String representation of the array's elements.
- If the array is
null
, it returns the string "null".
Examples of Arrays.toString() in Java
Using Arrays.toString()
with Primitive Arrays
When we need to print or log the contents of a primitive array, we use Arrays.toString()
method that converts a primitive array into a string representation.
Java // Java program to demonstrate // working of Arrays.toString() // with primitive arrays import java.util.Arrays; class GFG { public static void main(String[] args) { // create different types of arrays and // print their contents using Arrays.toString() boolean[] arr1 = new boolean[] { true, true, false, true }; byte[] arr2 = new byte[] { 10, 20, 30 }; char[] arr3 = new char[] { 'g', 'e', 'e', 'k', 's' }; double[] arr4 = new double[] { 1, 2, 3, 4 }; float[] arr5 = new float[] { 1, 2, 3, 4 }; int[] arr6 = new int[] { 1, 2, 3, 4 }; long[] arr7 = new long[] { 1, 2, 3, 4 }; Object[] arr8 = new Object[] { 1, 2, 3, 4 }; short[] arr9 = new short[] { 1, 2, 3, 4 }; System.out.println(Arrays.toString(arr1)); System.out.println(Arrays.toString(arr2)); System.out.println(Arrays.toString(arr3)); System.out.println(Arrays.toString(arr4)); System.out.println(Arrays.toString(arr5)); System.out.println(Arrays.toString(arr6)); System.out.println(Arrays.toString(arr7)); System.out.println(Arrays.toString(arr8)); System.out.println(Arrays.toString(arr9)); } }
Output[true, true, false, true] [10, 20, 30] [g, e, e, k, s] [1.0, 2.0, 3.0, 4.0] [1.0, 2.0, 3.0, 4.0] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
Using Arrays.toString()
with Object Arrays
When we want to print an array of objects, and the objects either override toString() method, we use Arrays.toString()
method that invokes the toString()
method of each object and provides their string representations. If not overridden, it defaults to the class name and memory address.
Java // Java program to demonstrate // working of Arrays.toString() // for user defined objects import java.util.Arrays; class Main { public static void main(String[] args) { Student[] arr = { new Student(1, "a", "UP"), new Student(2, "b", "MP"), new Student(3, "c", "Delhi") }; System.out.println(Arrays.toString(arr)); } } // class to represent a student class Student { // instance variables int r; String n, a; // Constructor public Student(int r, String n, String a) { this.r = r; this.n = n; this.a = a; } // Overriding the toString() method to return // student details in a formatted string @Override public String toString() { return "Roll No: " + this.r + ", Name: " + this.n + ", Address: " + this.a; } }
Output[Roll No: 1, Name: a, Address: UP, Roll No: 2, Name: b, Address: MP, Roll No: 3, Name: c, Address: Delhi]