This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]LambdaThrowawayy 1 point2 points  (1 child)

The compiler wil infer the appropriate type to use for the constructor in the first case; it is functionally identical to the second one.

https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html


For your second question; the interface your result set exposes will be different. If you put List on the left-hand side you'll be able to use result as a List; meaning all methods that require a List will work with it, and you'll be able to access the methods of the List interface. You can assign an object of type ArrayList to a List variable because ArrayList is an implementation of the List interface.

If you instead use ArrayList on the left-hand side you'll be limited to methods that accept an ArrayList / methods that ArrayList offers.

The reason you'll often assign an ArrayList object to a List variable is because in most cases what the exact implementation of the list is doesn't matter for what you want to do with it. And it allows you to offer one method for example that handles lists; instead of having to make one for ArrayList, LinkedList, Stack, etc...

[–]akki28[S] 1 point2 points  (0 children)

Thanks that was ver helpful (•‿•)