safakozdek's blog

By safakozdek, history, 7 years ago, In English

Hello,

I would like to know about the meaning of the output that i get when i tried Arrays.toString method on a multi dimensional array. I know that we use Arrays.deepToString for displaying multidimensional arrays in Java.

However the output looked very strange to me. What does that thing starting with "[" refers to ? Is it something like the place that arrays stored in memory? (btw I use Eclipse)

Thanks.

Source Code:

public static void main (String args[]) {

int arr[][] = {{1,2,3,4,5},{1},{1}};

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

}

Output:

[[I@4554617c, [I@74a14482, [I@1540e19d]

  • Vote: I like it
  • +7
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it +2 Vote: I do not like it

So I won't claim to be an expert on the topic, but I know enough that I should be able to answer your question.

First, Arrays.toString() iterates through all the elements in the array and simply calls the toString() function for each element individually. When you do this with things like primitives or objects with well defined toString() functions, you get reasonable results.

Somewhat unfortunately, all arrays have default to the useless implementation of toString(). Instead of overriding the toString() function like pretty much every other object in the language, they leave you with quite a mystery. When toString() is called on an array of primitives, you get a pattern like so:

1.) "[" to indicate that everything after this is apart of an array

2.) getClass.getName() -> A string that should properly identify what the object is. For primitives, this is usually just a capital letter that starts the object name (i.e 'I' for int or 'C' for char). For Class types, it typically gives the path to the Class file in the java library (i.e. java.lang.String for String)

3.) "@" for no real reason other than to separate part 2 from part 4

4.) The hexadecimal representation of the objects hashCode.

So yeah, there is a lot of nuance to the "getClass.getName()" part which I skip over because either it doesn't pertain to the question you asked or I don't know my self. In any event, I hope I helped in some way.