-2

Here is my code:

String[] magic = {"stick", "hat", "witch"};
String magic1 = magic.toString();
String magic2 = Arrays.toString(magic);

System.out.println(magic1);         // this is printing a memory location
System.out.println(magic2);         // this one prints: [stick, hat, witch]  

What is the difference between magic1 and magic2?

takendarkk
  • 3,347
  • 8
  • 25
  • 37

3 Answers3

1

Arrays are objects, but they don't change (override) its toString() method, which means they use default one, inherited from Object. If you read documentation of this method you will find:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

In your case

  • getClass().getName() returns [Ljava.lang.String which means
    • one dimensional array (because there is only one [)
    • of type which full name is java.lang.String
  • and Integer.toHexString(hashCode()) returns something like 1db9742 which is hexadecimal form of integer returned by hashCode() method.

Now if you take a look at code of Arrays.toString(Object[] array) (String[] is considered as Object[])

4531  public static String toString(Object[] a) {
4532 if (a == null)
4533 return "null";
4535 int iMax = a.length - 1;
4536 if (iMax == -1)
4537 return "[]";
4539 StringBuilder b = new StringBuilder();
4540 b.append('[');
4541 for (int i = 0; ; i++) {
4542 b.append(String.valueOf(a[i]));
4543 if (i == iMax)
4544 return b.append(']').toString();
4545 b.append(", ");
4546 }
4547 }

you will see that its purpose is to create string build from content of this array. It does this by iterating over all elements and adding their string representation to StringBuilder which is then used to create String which will be returned.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

It is basically a tailored toString which makes the output pretty.

What you see in the first toString is the memory address . Reason being that the variable-name is just that - a memory address( aka reference ).

Every class inherits toString, and can implement its own.

See Arrays class API

String[] names = {"Bob", "Dad", "Mom"};
String names1 = names.toString();
String names2 = Arrays.toString(names);

System.out.println(names1 );         
System.out.println(names2 );         

prints out:

[Ljava.lang.String;@1034bb5
[Bob, Dad, Mom]
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
  • 1
    thanks for the reply. my question is should not be the output be same for both of them ? why magic2 is not printing the reference then? – kalpa khan Jan 15 '15 at 00:49
0

Read the doumentation. I have copied the necessary information here.

public static String toString(Object[] a)

Returns a string representation of the contents of the specified array. If the array contains other arrays as elements, they are converted to strings by the Object.toString() method inherited from Object, which describes their identities rather than their contents.

The value returned by this method is equal to the value that would be returned by Arrays.asList(a).toString(), unless a is null, in which case "null" is returned.

public String toString()

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. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())