Java Arrays.toString(boolean[] a) method example

In this tutorial, You will learn how to get a string representation the array. Java Arrays.toString(boolean[] a) method returns a string representation.

Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Elements are converted to strings as by String.valueOf(boolean). Returns "null" if a is null.

syntax

public static String toString(boolean[] a)

Parameters

a - the array whose string representation to return.

Returns

A string representation.

Java Arrays toString(boolean[] a) example

  • Initialize the boolean[] array boolean[] array = { true, false, true, true };
  • Print the elements of original array in for loop.
  • Arrays.toString(array); It returns a string representation of array, it consists of a list of the array's elements, enclosed in square brackets ("[]").
package com.technicalkeeda;

import java.util.Arrays;

public class App {

    public static void main(String[] args) {
        boolean[] array = {
            true,
            false,
            true,
            true
        };

        System.out.println("The elements of array is :- ");
        for (boolean number: array) {
            System.out.print(number + " ");
        }

        System.out.println("\nThe string representation of array is :- ");

        System.out.println(Arrays.toString(array));
    }
}

output

The elements of array is :- 
true false true true 

The string representation of array is :- 
[true, false, true, true]