# Print an Array in Java With Arrays.toString and deepToString

> Use Arrays.toString to print a readable representation of a simple Java array, or Arrays.deepToString for multidimensional arrays and nested complex objects

**URL:** https://sentry.io/answers/print-an-array-in-java/

---

## The Problem

What is the simplest way to print an array in Java? Passing an array directly to `System.out.println` produces an object reference rather than a printed representation of the array contents.

```java
class Main {
    public static void main(String[] args) {
        String[] products = new String[]{"Coffee", "Tea", "Chocolate Bar"};

        System.out.println(products); // will print something like [Ljava.lang.String;@49476842
    }
}
```

How can I get the output `["Coffee", "Tea", "Chocolate Bar"]` instead?

## The Solution

We can print simple arrays in Java using the method [`Arrays.toString()`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Arrays.html#toString(java.lang.Object%5B%5D)):

```java
import java.util.Arrays;

class Main {
    public static void main(String[] args) {
        String[] products = new String[]{"Coffee", "Tea", "Chocolate Bar"};

        System.out.println(Arrays.toString(products)); // will print ["Coffee", "Tea", "Chocolate Bar"]
    }
}
```

This will also work for arrays containing non-`String` types, such as ints or doubles.

If we have an array containing other arrays, or complex custom objects, we will need to use [`Arrays.deepToString()`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Arrays.html#deepToString(java.lang.Object%5B%5D)) to print out an accurate representation:

```java
import java.util.Arrays;

class Main {
    public static void main(String[] args) {
        String[][] productRanges = new String[][]{{"Coffee", "Tea"}, {"Granola Bar", "Chocolate Bar"}};

        System.out.println(Arrays.toString(productRanges)); // will print [[Ljava.lang.String;@49476842, [Ljava.lang.String;@78308db1]
        System.out.println(Arrays.deepToString(productRanges)); // will print [[Coffee, Tea], [Granola Bar, Chocolate Bar]]
    }
}
```

---

*Source: [sentry.io/answers/print-an-array-in-java/](https://sentry.io/answers/print-an-array-in-java/)*
