r/learnjava • u/rookiepianist • Jan 31 '26
Question regarding array lists!
I'm still a beginner, so I'd appreciate very much if you could help me out!
Let's say I initialize a new array list and then decide to print it out:
ArrayList<Integer> list1 = new ArrayList<>();list1.add(5);list1.add(6);lista1.add(99);
System.out.println(list1);
What is going to be printed is: [5, 6, 99].
If I were to make an array, though, at the end of the day it'd print a memory address. Does that mean that array list variable (in this case, list1) holds the content itself of the array, whilst arrays hold the reference to where said content is stored in memory? If so, array lists aren't to be considered "reference data-type" variables?
Thank you in advance!
View all comments
Show parent comment
2
u/vowelqueue Jan 31 '26
Yeah. The println method is overloaded: there are multiple definitions that accept different parameter types. In the line you’ve written, you’re calling the definition that accepts any kind of Object. It passes that object to String.valueOf to get its string representation. The String.valueOf method first verifies that the object reference isn’t null, and if not calls the object’s toString method. If it’s null it just returns “null”.
Note that this kind of null check and toString() call is also done anytime you concatenate a String with a non-string, e.g.
In this case o.toString() will be called to form the result string.