Convert an Object Array to String in Java
Declaration :
1 |
public static String toString(Object[] a) |
Explanation :
Purpose | The java.util.Arrays.toString(Object[] a) method returns a string representation of the contents of the specified Object array. If the array contains other arrays as elements they are converted to strings by the Object.toString() method inherited from Object which describes their identities rather than their contents. |
Parameters | a ===> This is the array whose string representation to return. |
Return Value | This method returns a string representation of a. |
Exception | NA |
Java Program : Example
Below example will explain java.util.Arrays.toString() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.c4learn; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // Initialize an object array Object[] arrObj = new Object[] { 10, 20, 30, 40, 50 }; // Print the object array System.out.println("The array is:"); for (Object num : arrObj) { System.out.println("Element Value is " + num); } System.out.println("\nString Representation :"); System.out.println(Arrays.toString(arrObj)); } } |
Output of Program :
1 2 3 4 5 6 7 8 9 |
The array is: Element Value is 10 Element Value is 20 Element Value is 30 Element Value is 40 Element Value is 50 String Representation : [10, 20, 30, 40, 50] |