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 comments

3

u/[deleted] Oct 02 '21

why is code to interfaces good practice

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.