2

When I create an ArrayList object and add other objects to it, printing out the ArrayList object will print out the memory references of the objects inside. However, if I add String to the ArrayList object, it will not print out the memory references of the String but rather the actual String value. String is also an object of a class right, so why does it not print out the String memory reference?

4
  • 2
    String overrides Object.toString. The other objects do not Commented Apr 5, 2015 at 20:48
  • Oh ok, thanks. That's a good, concise answer =p Commented Apr 5, 2015 at 20:55
  • Many other classes in addition to String implement toString() method, as your own classes can. And number which what you see calling "native" Object#toString() - is not memory reference of object, but its hash code. Commented Apr 5, 2015 at 21:00
  • String#toString Commented Apr 5, 2015 at 21:08

3 Answers 3

3

The toString method for a Collection in Java (which is what an ArrayList extends) uses String.valueOf on each of its elements: http://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html#toString()

The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

String.valueOf is simply grabbing the toString value of the object: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#valueOf(java.lang.Object)

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

Sign up to request clarification or add additional context in comments.

Comments

1

When you print a List, the List's toString() method is called, which in turn calls to toString() method of its elements.

The Object, which every class extends, class has a toString() method that creates the "memory address" output you're seeing (actually it's not the memory address, but anyway). The String class overrides the toString() method to return its contents.

To fix the output "problem", override the toString() method in you classes of the other objects you're adding to your List to return something "human readable".

Comments

0

toString() method of Object class is overridden by Class AbstractCollection. ArrayList extends AbstractList and AbstractList extends AbstractCollection. Method detail is mentioned below.

public String toString() Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

Overrides: toString in class Object

Returns: a string representation of this collection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.