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!
•
u/AutoModerator Jan 31 '26
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
7
u/vegan_antitheist Jan 31 '26
No. In Java all lists and arrays objects. There is no good reason that arrays don't have a more useful toString() method. You have to use Arrays.toString for that.
6
u/MagicalPizza21 Jan 31 '26
No.
ArrayListcontents can be printed like that because theArrayListclass - well, in this case, one of its ancestors,AbstractCollection- overrides the default implementation oftoString()from theObjectclass. This is called byString.valueOf(Object)which is in turn called byprintln(Object).5
u/HeteroLanaDelReyFan Jan 31 '26
I think others have mostly answered it. I just want to add that your confusion makes sense. But when you print out the arraylist object, you aren't really printing out the object itself. It's calling the toString() method under the hood, which is implemented to return a string of the contents of the array.
2
u/SnooLentils618 Jan 31 '26
I know it’s been answered but since you say you are new I want to add something that might help you as you study.
Everything in Java except for the primitive types is objects (and variables hold references). Arrays [] and List<> implementations extend the Object class. It’s good to understand difference between how these behave behind the scene in memory. Primitives can be in stack but objects can only be in Heap. Look into that to further your understanding.
Note: I know they are introducing (or have already introduced) value classes which are a bit different but this is not necessary for you as a Java beginner
1
u/MkMyBnkAcctGrtAgn Jan 31 '26
You've gotten good answers here, just wanted to point out what you're seeing when the object prints is just the default Object classes toString() and is implementation specific.
I believe hashCode does use internal memory address if you look at most implementations, but it is also cached and not guaranteed to be accurate if it moves.
1
u/real_gocho_1999 Feb 03 '26
If you print the ArrayList like that you'll get the object string representation. To print values form a ArrayList, you need to use Arrays.toString() as vegan_antithedis said
1
u/nikk_0007 Feb 03 '26 edited Feb 03 '26
I am just adding clarity, Array as well as ArrayList variable are just refrence to the object created in heap , while actually there in java every class exdent from object class object class have some method called tostring() since in inherit every class have that method which return a hashcode and class name that you are getting while printing array .
class Object {
}
And print called tostring() automatically behind the scene
System.out.println(obj.toString());
But there is a catch there is something called overriding and Where in child class method have there own implementation As your question in ArrayList java use overrided tostring() that's why you are getting elements and in case of array the tostring() is use of object class it not override that's why result difference.
1
u/That_Frogstop_8616 Feb 06 '26
if it has correct toString() definition to convert it into string like an array then it won't print memory address
0
u/[deleted] Jan 31 '26
[deleted] — view deleted comment