0

In working on an Euler problem as a new Java programmer, I have encountered something peculiar related to printing an array.

I do not understand why this is happening - the individual values appear to be correct, when printed individually, but the toString() is clearly creating a string which is not what I expect.

I would have expected either a compile error, or the array to be put into a concatenated list. Neither of these happened.

Note: I am not interested in "how to print an array?" but rather understanding why the toString() does NOT print the array. There are plenty of resources online available to find how to do so.

public class example {

    public static void main(String[] args) {

        /* Setup a string/int array converion*/
        int i=0;
        String nums = "123456";
        char[] splitNums = nums.toCharArray();
        int[] ints = new int[nums.length()];

        for (char c : splitNums) {
            ints[i++] = Character.digit(c,10);
        }

        //Print using the string/char[]
        System.out.println(nums);
        System.out.println(splitNums);

        //Values are clearly there
        for (int j : ints){
            System.out.println(j);
        }

        //What is toString() doing? 
        System.out.println(ints.toString());

    }

}

Output:

123456
123456
1
2
3
4
5
6
[I@4b71bbc9
enderland
  • 13,825
  • 17
  • 98
  • 152
  • 1
    @Makoto I don't actually care _how_ to do it, I am wanting to know _why_ that happens - the other question only explains how and does not even attempt to answer why. – enderland Feb 28 '15 at 20:43
  • What else do you want to know apart from my answer below? I can probably answer your question. – user3437460 Feb 28 '15 at 20:47

2 Answers2

3

The output [I@4b71bbc9 that you see is the output of the method Object.toString(). Arrays are objects in Java, but they don't have their own implementation of a toString() method - so java.lang.Object's version is called, which prints this kind of output (see the docs).

To print an array, do this instead:

System.out.println(Arrays.toString(ints));
Jesper
  • 202,709
  • 46
  • 318
  • 350
2

toString() are implicitly called when you are printing an object. Since ints is an array (treated as object) and not a primitive.

When your ints are placed within a println statement, the toString() method inherited from Class Object are invoked printing what you saw on your screen.

So why does it inherit from Class Object?

This is because every class in Java implicitly extends from Class Object.


To print an array with a println statement, you can do this:

System.out.println(Arrays.toString(arrayName));

It will print out the formatted array nicely for you:

Example:

[0, 1, 2, 3, 4]

About toString()

The toString() method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.

-From the API of toString-

So in general a toString() method makes a printed object meaningful and textually understandable of what the object represents. However there are a few exception when printing object such as String objects and char array. The reason which is self-explanatory (String already in String)

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • `toString() are implicitly called when you are printing an object.` That is true if that object is neither a `char[]` or a `String`. But that's just a detail. – Tom Feb 28 '15 at 20:59
  • @Tom I totally agree with you, I will add a little bit more to my solution, hopefully it helps OP to certain extend. – user3437460 Feb 28 '15 at 21:03
  • Thanks - this is what I was looking for. Coming from non-Java this is a bit strange to me that all objects inherit methods from a `Class` object. – enderland Feb 28 '15 at 22:36