0

First of all, yes, I know I have to iterate through an array and print its elements one by one in order to actually "print" an array. I was just wondering what the output of explicitly printing an array such as in the code below actually means. My guess is that it's the memory location of the array?

I know it's a fairly newbie question but I'm new to java (and coding in general) so please enlighten me.

public class Tests {
    public static void main(String[] args) {

        String[] strArray = {"1", "2", "3"};
        System.out.println(strArray);

    }
}
Xyds
  • 3
  • 2

2 Answers2

5

The println(Object) method invokes Object::toString on its parameter, so you are really asking about the meaning of the toString method on arrays. And arrays do not override toString, so the result comes from the implementation of toString in Object.

The default implementation of toString includes the name of the object's class and a numeric representation derived from its object identity. In the absence of a better toString, this allows you to see the object's type, as well as be able to differentiate different instances (since they'll have different identities). This is a "least common denominator" approach; it is a reasonable minimum the system can provide.

Brian Goetz
  • 90,105
  • 23
  • 150
  • 161
0

It's the default toString method of Object class. It's the name of the class plus @ plus the return value of hashCode method, which may be implemented differently in every class.

Programmer
  • 803
  • 1
  • 6
  • 13
  • @BrianGoetz I suggest you add that interesting fact about `identityHashCode` to your Answer here. I had never noticed that method. – Basil Bourque May 22 '22 at 18:06