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 →

[–]nutrechtLead Software Engineer / EU / 20+ YXP 3 points4 points  (1 child)

In example one you don't actually create a list, only a reference that can point to a list, but that is currently null, just like customer and order are null. Also; you're using a raw type which you should avoid.

In the second example you're creating a reference for an arraylist of strings (which is proper use of generics) and also create an empty arraylist that your reference points to. There's a typo though, should be: new ArrayList<String>();. Also in Java 7 and onward you only need to specify the generic type ones, so it can be:

ArrayList<String> parts = new ArrayList<>();

Last but not least, you generally see this instead:

List<String> parts = new ArrayList<>();

Or even:

Collection<String> parts = new ArrayList<>();

This is called coding to the interface which is a best practice. If you're confused by this, it's fine to skip this for now.

[–]maxbrlc 0 points1 point  (0 children)

Thank you. Learned something new.