What does the method toString(obj[] a) do in java?


The toString(Object[] a) method of the java.util.Arrays class returns a string representation of the contents of the specified Object array. If the array contains other arrays of elements, they are converted to strings by the Object.toString() method inherited from Object, which describes their identities rather than their contents.

Example

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      Object[] ob1 = new Object[] { 10, 20 };
      System.out.println("The array is:");

      for (Object number : ob1) {
         System.out.println("Number = " + number);
      }
      System.out.println("The string representation of array is:");
      System.out.println(Arrays.toString(ob1));
   }
}

Output

The array is:
Number = 10
Number = 20
The string representation of array is:
[10, 20]

Sai Subramanyam
Sai Subramanyam

Passionate, Curious and Enthusiastic.

Updated on: 30-Jul-2019

68 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements