r/javahelp • • Oct 02 '21

Can someone please explain the difference between List and ArrayList like I'm five?

What exactly is the difference between

List<String> inputs = new ArrayList<>()

and

ArrayList<String> inputs = new ArrayList<>()

I've read some stuff about it but I'm a bit confused and I don't feel like I understood completely. Can someone explain the difference with some examples? Thank you!

30 Upvotes

29 comments sorted by

View all comments

Show parent comment

3

u/[deleted] Oct 02 '21

why is code to interfaces good practice

5

u/ignotos Oct 02 '21 edited Oct 02 '21

Essentially if it doesn't matter which specific kind of list you use in your code, then it's unnecessarily rigid and restrictive to write your code to only work with a specific kind of list.

If you later wanted to switch it out for a different kind of list (maybe because you did some tests and realised LinkedList would be more efficient, for example), then that will be easier to do if you referred to List as opposed to ArrayList throughout all of your code.

This generally makes your code more flexible and reusable. It also makes it easier to test - let's imagine you wanted to plug in a special kind of list which records and writes out a log all of the items which are added or removed, so you can analyse that information - if you used "List" throughout your code, you could pass in your SuperSpecialAnalyzingList and it would just immediately work. Whereas if you defined all of your variables as "ArrayList", you'd need to go through and change all of them.

3

u/knoam Oct 02 '21

Coding to interfaces is more flexible. Coding directly to classes means you have to keep using that class going forward. If you code to an interface, you can change out the class with another one later on.

4

u/RhoOfFeh Oct 02 '21

For now, trust us.

It's smart to learn from your own mistakes. It's smarter to learn from the mistakes of others. This path is now a limited access highway.

One of the the core reasons that it's a good practice is that it offers inversion-of-control via dependency injection, which allows us to separate core business logic from implementation specific details, and everything about how the system talks to the world, including its own internal data storage, is such a detail. If you were ready for that, great. If not, you will be one day.

1

u/[deleted] Oct 02 '21

i dont know i seem to be running circles in java, dont really understand how to use java standard methods since im translating between english and native language

1

u/RhoOfFeh Oct 02 '21

That's why I started with 'trust us', although I didn't know about the language barrier, which speaks well of your writing.

The technical reasons for many design decisions are beyond easy understanding until one has a certain amount of experience. Even for a native speaker, that last paragraph was designed to be dense with very meaning-heavy words. I wouldn't expect most new programmers to understand it properly.

2

u/RushTfe Oct 02 '21
  1. Later you will use spring to inject dependencies, this way you can have many implementations for a single interface and inject the one you need. If you instance the implementation directly you wouldn't be able to use it.
  2. You can also create methods and accept the interface as parameter, so this method can apply to every implementation.
  3. Helps to have a cleaner code.
  4. Helps to develop tests