Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "List<Integer> list = new ArrayList<Integer>(); " actually mean?

I am pretty new to Java and I wanted to know what this actually means:

List<Integer> list = new ArrayList<Integer>(); //Example 1

To distinguish this question from others, I've read the posts about polymorphism and the difference between Example 1 and Example 2, and I understand that Example 1 allows for "programming to interface." I also understand that with Example 1, list can easily be changed to LinkedList without affecting the rest of the code.

ArrayList<Integer> list = new ArrayList<Integer>(); //Example 2

But what I want to know what Example 1 actually means. Does it create a new List? Or does it create a new ArrayList? Does the resulting object have the properties of a List? Or does the resulting object have the properties of an ArrayList? Could I implement methods that an ArrayList uses on list without a compile error?

This is my first time posting a question, so please let me know if I can make any improvements.

like image 452
Caroline Yi Avatar asked Apr 18 '17 02:04

Caroline Yi


People also ask

What does ArrayList Integer mean?

An ArrayList contains many elements. But in Java 8 it cannot store values. It can hold classes (like Integer) but not values (like int). Int notes.

Why do we do List new ArrayList?

List list = new ArrayList(); the rest of your code only knows that data is of type List, which is preferable because it allows you to switch between different implementations of the List interface with ease.

What is List and ArrayList in Java?

The List is an interface, and the ArrayList is a class of Java Collection framework. The List creates a static array, and the ArrayList creates a dynamic array for storing the objects. So the List can not be expanded once it is created but using the ArrayList, we can expand the array when needed.

Why is Integer used in ArrayList?

However, you have to use Integer as the type because ArrayLists can only hold objects, not primitive values. All primitive types must be wrapped in objects before they are added to an ArrayList. For example, int values can be wrapped in Integer objects, double values can be wrapped in Double objects.


2 Answers

In both your examples, only the expression new creates instances of objects (the right hand side of the =), the left hand side only defines a variable that points to the object that you created.

In both cases, the object that you create is of type ArrayList.

List is an interface, not a class, and interfaces themselves cannot be instantiated. But ArrayList implements the interface List, so you can assign an instance of ArrayList to a variable of type List.

The advantage of Example 1 is that you can later decide to create another implementation of List (like LinkedList) and all your code that was using the List type for variables will still work without any change. While in Example 2, if you later decide that you need a different type of List, you need to change the type of all your variables that point to the actual object.

Your questions:

But what I want to know what Example 1 actually means. Does it create a new List? Or does it create a new ArrayList?

It creates a new ArrayList.

Does the resulting object have the properties of a List? Or does the resulting object have the properties of an ArrayList?

The resulting object has all the properties of an ArrayList.

However, through the variable list which as type List, you can only access the methods defined in the interfaces List.

But you can use type-casting to access the methods in ArrayList like this, if you later need to (but there is little reason as ArrayList doesn't have much beyond what's in List)

List<Integer> list = new ArrayList<Integer>(); //Example 1
ArrayList<Integer> arrayList = (ArrayList<Integer>) list; // type-cast
like image 79
Erwin Bolwidt Avatar answered Oct 03 '22 09:10

Erwin Bolwidt


As you might already know, List is an interface and ArrayList is a class. It means List is more generals whereas ArrayList is more specific.

In example 1, you are not able to invoke the additional methods supported by ArrayList e.g. you cannot invoke ensureCapacity because it is implemented inside Arraylist class.

Unlike example 2, you can invoke all functionalities that are supported by ArrayList. The benefit of using example 1 is to increase flexibility and minimize code changes in case the implementation changes, e.g. in future you decide to use LinkedList. You no need to worry about making any changes to all the lines where list variable has been used.

like image 27
Fernando Tan Avatar answered Oct 03 '22 09:10

Fernando Tan