Printing Arrays by Hacking the JVM

DZone 's Guide to

Printing Arrays by Hacking the JVM

Peter Lawrey provides a novel way to get around the most common question in Java forums - how to print arrays.

· Java Zone ·
Free Resource

One the most common gotchas in Java, is knowing how to print arrays.  If an answer on how to print an array get more than 1000 upvotes, you have to wonder if there is a simpler way.  Just about every other popular language has that simpler way, so it's not clear to me why Java still does this.

Unlike other JDK classes, arrays don't have a particularly sane toString() as it is inherited from Object.

It Prints the Type and Address Right?

Actually, it doesn't print the address, it just looks as cryptic as one. It prints the internal representation of the type, and thehashCode() of the object.  As all arrays are an Object, they have a hashCode() and a type and a synchronized lock, and every thing else an Object has, but no methods specific to an array. This is why the toString() isn't useful for arrays.

What Does it Look Like Unhacked?

If I run the following program.

public class ObjectTest {

    boolean[] booleans = {true, false};

    byte[] bytes = {1, 2, 3};

    char[] chars = "Hello World".toCharArray();

    short[] shorts = {111, 222, 333};

    float[] floats = {1.0f, 2.2f, 3.33f, 44.44f, 55.555f, 666.666f};

    int[] ints = {1, 22, 333, 4_444, 55_555, 666_666};

    double[] doubles = {Math.PI, Math.E};

    long[] longs = {System.currentTimeMillis(), System.nanoTime()};

    String[] words = "The quick brown fox jumps over the lazy dog".split(" ");


    public void testToString() throws IllegalAccessException {

        Map<String, Object> arrays = new LinkedHashMap<>();

        for(Field f : getClass().getDeclaredFields())

            arrays.put(f.getName(), f.get(this));




it prints.










I think that is obvious to everyone. o_O Like the fact that J is the internal code for a long and L is the internal code for a Java class. Also is the code for boolean when is unused.

What Can We Do About It?

In this program it's we end up having to write a special toString method for object needs to be called by our special method for printing a Map.Entry.  Repeat this many times throughput your program and it's just easier to avoid using arrays in Java because they are hard to debug.

What About Hacking the JVM?

What we can do is change the Object.toString().  We have to change this class as it is the only parent of arrays we have access to. We cannot change the code for an array as it is internal to the JVM.  There is no byte[] java class file for example for all the byte[] specific methods.

Take a copy of the source for java.lang.Object and replace the toString() with

public String toString() {

        if (this instanceof boolean[])

            return Arrays.toString((boolean[]) this);

        if (this instanceof byte[])

            return Arrays.toString((byte[]) this);

        if (this instanceof short[])

            return Arrays.toString((short[]) this);

        if (this instanceof char[])

            return Arrays.toString((char[]) this);

        if (this instanceof int[])

            return Arrays.toString((int[]) this);

        if (this instanceof long[])

            return Arrays.toString((long[]) this);

        if (this instanceof float[])

            return Arrays.toString((float[]) this);

        if (this instanceof double[])

            return Arrays.toString((double[]) this);

        if (this instanceof Object[])

            return Arrays.deepToString((Object[]) this);

        return getClass().getName() + "@" + Integer.toHexString(hashCode());


and in Java <= 8 we can add this class to the start of the bootclasspath by adding to the command line


(or wherever your classes have been compiled to) and now when we run our program we see

booleans=[true, false]

bytes=[1, 2, 3]

chars=[H, e, l, l, o,  , W, o, r, l, d]

shorts=[111, 222, 333]

floats=[1.0, 2.2, 3.33, 44.44, 55.555, 666.666]

ints=[1, 22, 333, 4444, 55555, 666666]

doubles=[3.141592653589793, 2.718281828459045]

longs=[1457629893500, 1707696453284240]

words=[The, quick, brown, fox, jumps, over, the, lazy, dog]

just like in you would in just about any other language.


While this is a cool trick, the best solution is that they finally fix Java so it produces a sane output for arrays. It knows you need one and provides it, but hides it away in a class you have to google to find, so that every new Java developer has to have a WTF moment the first time they try to work with arrays.

java ,jvm

Published at DZone with permission of Peter Lawrey , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}