What is the difference between List and ArrayList reference variables in Java?

Tram Ho

1. Set the problem.

In this article I will learn with you two ways to declare variables in Java. Why can we use both Interface and Class as variable types? Which way will be more advantageous and why? I note that here I only mention non-primitive data types (primitive types) because primitive data types have their own declarations.

For example: Here I take the example of Interface List and Class ArrayList . Why? Because this can be said as a typical example, many of you have seen in the code on the network or in the project but did not pay much attention.

Requirements: Understand the difference between Interface and Class, what is polymorphism in object-oriented programming.

2. Example analysis.

You’ve probably seen this declaration a lot! Let’s analyze this line of code a bit. The left side declares a variable named listOfStudents with the variable type List and the data type as the Student object. On the right, we have a new operator to initialize an ArrayList object. Here we will create a variable that refers to a memory area that contains Student objects.

As we have shown we can fully declare the following:

Question 1: So how to declare correctly?

The answer is that all the way is correct, but only the way to make our program more flexible. Basically we understand that a class can implement one or more interfaces. Here, the ArrayList class implements the List interface, which means List is supertype while ArrayList is subtype . In Java and other object-oriented programming languages, a supertype variable can store (reference) an object of subtype. In conclusion, the declaration (1) is nothing wrong, and the declaration (2) is the standard declaration and there is nothing to discuss.

Question 2: If the way (2) is correct, do I declare it under (1) is it good?

The answer is, of course, “yes!”, If there is nothing interesting, what people say. Now try to think if you don’t want to create an ArrayList object but instead “new Vector <> ()” or “new LinkedList <> ()” ? Due to the fact that our variable may receive return values ​​from certain functions. What if each function returns a type? That is the first advantage of the declaration (1). The reason is because the ArrayList, Vector or LinkedList classes are implemented from Interface List. (This is a pretty good example of such polymorphism …)

Question 3: Is there anything else interesting?

Of course “yes!”. At least who do they say what to do. As in question 2, I mentioned that the variable can receive values ​​from many functions and each function can return a different type such as this: (Because in fact each data structure will fit a data type either single-threaded or multithreaded, so different return functions are required)

Eh, so writing like this is just a job that has to write up to 3 functions. Are you kidding me! No wait, I haven’t said it yet. Watch this code:

That, we will convert the function’s return type into List and also pass the data structure that you want to use. Which type you pass will operate on that type and return a List. When retrieving the return value from the function, we can use a variable of type List or cast to the corresponding data type.

3. Conclusion

  1. Theoretically declaring the way (1) or (2), it creates an object that we declare after the new operator is different only in the type of variable we refer to that object.
  2. Declaring in a manner (1) will take advantage of the polymorphism in object-oriented programming and make our program more flexible, more scalable later.

4. References.

  1. https://www.java67.com/2016/01/difference-between-list-and-arraylist-variable-in-java.html
Share the news now

Source : Viblo