Because ArrayList's toString() is implemented with the "How many elements were actually inserted" approach and not "How much space for objects is on the heap (pre)-allocated". If you don't have any items in the list, why would you care about it's capacity and memory allocation in its string representation? Answer from mykeesg on reddit.com
🌐
SourceTrail
sourcetrail.com › home › java › solved: initialize list with values
Solved: initialize list with values in Java - SourceTrail
December 30, 2023 - The most straightforward way of initializing a list with values is by using Add() method of list class. This method adds an element at the end of the list. ... List<String> list = new ArrayList<>(); list.add("Element1"); list.add("Element2"); ...
🌐
Sabe
sabe.io › blog › java-initialize-list
How to Initialize a List in Java | Sabe
December 20, 2022 - Finally, another way to do it is by initializing it in the constructor using double-brace initialization: JAVAList<String> namesList = new ArrayList<String>() {{ add("John"); add("Mary"); add("Peter"); }};
Discussions

Why no default values in ArrayList ??
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar 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: 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: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } 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. More on reddit.com
🌐 r/javahelp
9
2
October 30, 2023
java - Initialization of an ArrayList in one line - Stack Overflow
However, I'm not too fond of that ... end up with is a subclass of ArrayList which has an instance initializer, and that class is created just to create one object -- that just seems like a little bit overkill to me. What would have been nice was if the Collection Literals proposal for Project Coin was accepted (it was slated to be introduced in Java 7, but it's ... More on stackoverflow.com
🌐 stackoverflow.com
Initializing an empty ArrayList (Java)
Assuming both are not static variables the only difference should be the order of initialization. When you have something declared out of the constructor it is initialized first. It will run the constructor after all of the fields at the top have been initialized. Here is a stack overflow post about this topic which goes into more detail: https://stackoverflow.com/questions/4916735/default-constructor-vs-inline-field-initialization More on reddit.com
🌐 r/learnprogramming
3
9
March 31, 2018
How do I declare an ArrayList in a default constructor?
Take a look at https://www.w3schools.com/java/java_constructors.asp for some examples on how to initialise fields inside a constructor. Think about how you could copy what is being done in that link and change it to initialise size and books the way you need to. More on reddit.com
🌐 r/learnprogramming
4
1
November 2, 2021
🌐
Java67
java67.com › 2012 › 12 › how-to-create-and-initialize-list-arraylist-same-line.html
How to create and initialize List or ArrayList in one line in Java? Example | Java67
You can also see these Java Collections ... about ArrayList and other collection classes in Java. That's all on this Java tip about how to create and initialize a List in the same line in Java. This is a great tip that can save a lot of coding time if you frequently create and initialize a List with test ...
🌐
Reddit
reddit.com › r/javahelp › why no default values in arraylist ??
r/javahelp on Reddit: Why no default values in ArrayList ??
October 30, 2023 -

If I am not wrong, objects are created inside the heap memory so they have a default value, in the case of array it is 0 but why is there no default values in ArrayList why is the output empty for an ArrayList ?? since it is also created inside heap memory. then why this difference ?? since ArrayList has default/initial size 10 I was expecting 10 zeros or 10 null values, or something like that would have made more sense. But instead what we get is an empty ArrayList [ ] . why ??

int arr[]=new int[5];
for(int i=0;i<=arr.length-1;i++){
    
    System.out.print(arr[i]+" ");
    //output is 0 0 0 0 0
}


ArrayList num=new ArrayList();

System.out.println(num);
//output [ ]

Top answer
1 of 4
7
Because ArrayList's toString() is implemented with the "How many elements were actually inserted" approach and not "How much space for objects is on the heap (pre)-allocated". If you don't have any items in the list, why would you care about it's capacity and memory allocation in its string representation?
2 of 4
6
It doesn't have an initial size of ten. It has an initial capacity of ten. That means it can hold up to ten items before it's full and has to resize the capacity. You have to actually add ten items to get the size up to ten. With arrays, if you want to insert more items than the array can hold, you have to create a larger array, copy the old items, and add the new items. ArrayList has an array inside of it and handles that extra work for you. You don't have to deal with checking that the array can hold another item, creating a larger array, copying everything over, and adding the new items. Instead, you just call the add method as many times as you want and ArrayList takes care of that automatically. The capacity is how many items the array inside the ArrayList can hold before the array gets full. The size is how many items you've actually put into it. The reason why you get a bunch of zeros with the array is because the array doesn't have any difference between its size and capacity. So, when you create an array and you don't specify the elements, it has to fill it with something, and that something is the default value for that type. For primitive numbers, that default value is zero. For booleans, that default value is false. For chars, that default value is '\0'. For objects, that default value is null.
🌐
Baeldung
baeldung.com › home › java › java collections › guide to the java arraylist
Guide to the Java ArrayList | Baeldung
December 14, 2024 - We showed how to create an ArrayList instance, and how to add, find, or remove elements using different approaches. Furthermore, we showed how to add, get, and remove the first, or the last element using sequenced collections introduced in Java 21. The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project. ... Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
🌐
Quora
quora.com › How-do-you-assign-a-value-to-an-ArrayList-in-Java
How to assign a value to an ArrayList in Java - Quora
Answer (1 of 3): You cannot assign a value to an ArrayList as it is not a variable of primitive data type. The terminology of assigning value is used with primitive data types. An ArrayList is a dynamic resizable array implementation of the ...
Find elsewhere
Top answer
1 of 16
2787

It would be simpler if you were to just declare it as a fixed-size List (but with mutable elements) - does it have to be an ArrayList?

List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

Or if you have only one element:

List<String> places2 = Collections.singletonList("Buenos Aires");

This would mean that places2 is immutable (trying to change it in any way will cause an UnsupportedOperationException exception to be thrown).

To make a variable-size list that is a concrete ArrayList you can create an ArrayList from the fixed-size list:

ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));

And import the correct package:

import java.util.Arrays;
2 of 16
2240

Actually, probably the "best" way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way:

ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");

The catch is that there is quite a bit of typing required to refer to that list instance.

There are alternatives, such as making an anonymous inner class with an instance initializer (also known as an "double brace initialization"):

ArrayList<String> list = new ArrayList<String>() {{
    add("A");
    add("B");
    add("C");
}};

However, I'm not too fond of that method because what you end up with is a subclass of ArrayList which has an instance initializer, and that class is created just to create one object -- that just seems like a little bit overkill to me.

What would have been nice was if the Collection Literals proposal for Project Coin was accepted (it was slated to be introduced in Java 7, but it's not likely to be part of Java 8 either.):

List<String> list = ["A", "B", "C"];

Unfortunately it won't help you here, as it will initialize an immutable List rather than an ArrayList, and furthermore, it's not available yet, if it ever will be.

🌐
GeeksforGeeks
geeksforgeeks.org › java › initialize-an-arraylist-in-java
Initialize an ArrayList in Java - GeeksforGeeks
July 11, 2025 - // Java code to illustrate initialization // of ArrayList using add() method import java.util.*; public class Geeks { public static void main(String args[]) { // create a ArrayList String type ArrayList<String> al = new ArrayList<String>(); ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-of-arraylist-in-java
ArrayList of ArrayList in Java - GeeksforGeeks
July 11, 2025 - We have discussed that an array of ArrayList is not possible without warning. A better idea is to use ArrayList of ArrayList. ... // Java code to demonstrate the concept of // array of ArrayList import java.util.*; public class Arraylist { public static void main(String[] args) { int n = 3; // Here aList is an ArrayList of ArrayLists ArrayList<ArrayList<Integer> > aList = new ArrayList<ArrayList<Integer> >(n); // Create n lists one by one and append to the // master list (ArrayList of ArrayList) ArrayList<Integer> a1 = new ArrayList<Integer>(); a1.add(1); a1.add(2); aList.add(a1); ArrayList<In
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
// Without var ArrayList<String> cars = new ArrayList<String>(); // With var var cars = new ArrayList<String>(); ... This means the variable (cars) is declared as a List (the interface), but it stores an ArrayList object (the actual list).
🌐
CodeJava
codejava.net › java-core › collections › initialize-list-with-values
Java Initialize ArrayList with Values in One Line
July 18, 2024 - List<User> listUsers = ... an ArrayList object that wraps the list returned by Arrays.asList() method. Since Java 9, you can use the List.of() method to initialize a List with some initial values in a single line....
🌐
Coderanch
coderanch.com › t › 408008 › java › initializing-ArrayList
initializing ArrayList (Beginning Java forum at Coderanch)
August 26, 2007 - With an "old-style" array, I can ... ArrayList, it seems like I have to do ArrayList<Integer> myArray = new ArrayList<Integer>(); myArray.add(2000) myArray.add(100) myArray.add(40) myArray.add(60) for each element that I want to add....
🌐
Baeldung
baeldung.com › home › java › java array › initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - In this article, we explored different ways of initializing arrays in Java. Also, we learned how to declare and allocate memory to arrays of any type, including one-dimensional and multi-dimensional arrays. Additionally, we saw different ways to populate arrays with values, including assigning values individually using indices, and initializing arrays with values at the time of declaration.
🌐
Yawin
yawin.in › initialization-of-an-arraylist-in-one-line-using-java
Initialization of an ArrayList in One Line using Java – Yawin
Simplicity: Initializing an ArrayList in one line makes the code more concise and easier to read. You can initialize an ArrayList with multiple elements without having to add each element one by one.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-initialize-an-arraylist
How to Initialize an ArrayList in Java
One of the ways to initialize an ArrayList is to create it first and then add elements later using add() method. import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { // Creating an empty ArrayList with String type ArrayList<String> names = new ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-initialize-an-arraylist-in-java-with-values
How to Initialize an ArrayList in Java – Declaration with Values
April 21, 2023 - Alternatively, you can create an ArrayList with values/elements at the point of declaration by using the add method in an initializer block: import java.util.ArrayList; public class ArrayListTut { public static void main(String[] args) { ...
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › initialize an arraylist in java
Initialize an ArrayList in Java
August 4, 2023 - The Java ArrayList represents a resizable array of objects which allows us to add, remove, find, sort and replace elements. The ArrayList is part of the Collection framework and implements in the List interface. We can initialize an ArrayList in a number of ways depending on the requirement. In this tutorial, we will learn to initialize ArrayList based on some frequently seen usecases. // 1 - Empty ArrayList with initial capacity 10 ArrayList<String> list = new ArrayList<>(); //2 - Empty ArrayList with initial capacity 64 ArrayList<String> list = new ArrayList<>( 64 ); //3 - Initialize and pop
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-in-java
ArrayList in Java - GeeksforGeeks
This constructor is used to build an array list with the initial capacity being specified. ... Now, Using the constructors we have got ArrayList for further operations like Insertion,Deletion and Updation of the elements in ArrayList. ... import java.util.*; class GFG{ public static void main(String args[]){ // Creating an Array of string type ArrayList<String> al = new ArrayList<>(); // 1.
Published   1 month ago
🌐
Career Karma
careerkarma.com › blog › java › how to initialize arraylist in java
How to Initialize ArrayList in Java | Career Karma
December 1, 2023 - Type is the type of data our array list will store. arrayName is the name of the array list we are creating. new ArrayList<>() tells our program to create an instance of ArrayList and assign it to the arrayName variable.
🌐
Baeldung
baeldung.com › home › java › java list › java list initialization in one line
Java List Initialization in One Line | Baeldung
April 4, 2025 - Here’s a simple example: int intNum = 42; long longNum = intNum; assertEquals(42L, longNum); So, we may want to initialize a List<Long> in this way: List<Long> listOfLong = new ArrayList<Long>(Arrays.asList(1, 2, 3)); In the example, 1, 2, ...