0

I am noticing when I create an array list of any specific type of object I need to initialize it like this:

List<Object> objectList = new ArrayList<Object>();

Why must I initialize the right hand side containing the Object? I.e., why can't I initialize it without identifying the Object class again? - like

List<Object> objectList = new ArrayList<>();

If I can, why can I do it and what is the benefit (or loss thereof) of doing it either way?

I have researched this, and the only thing I am finding is the initialization including the second object. I want to know the 'why' of the way this is structured.

I am aware (now) that this question has an explicit answer (I found What is the point of the diamond operator in Java 7? here), but I left this because the research was very difficult without knowing the term 'diamond operator'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jason V
  • 623
  • 1
  • 10
  • 30
  • 1
    `new ArrayList<>()` <- Thats the diamond operator and it was added with java 7. The benefit is that you have to type a little less, the loss is that you need at least java 7 for it to work. – OH GOD SPIDERS Jun 13 '17 at 14:25

5 Answers5

1

They are exactly the same; if you put nothing, it will use the declaration class. It's popularly called the diamond operator (you can look it up), though it's not an operator.

Basically if you leave it empty, and it's not an anonymous inner class (this will be fixed in Java 9) it will use the declaration class as the instantiation class. Since generics are not covariant, that's usually what you want.

PS. It was introduced in Java 7, if you are using older versions it is required to add the class inside the angle brackets.

PPS. Here's everything you need to know about it ;)

Luan Nico
  • 5,376
  • 2
  • 30
  • 60
  • diamond operator? Thanks for that tip! let me look at that and you might end up with a +1! – Jason V Jun 13 '17 at 14:23
  • Thank you! i was able to find a very detailed explanation here https://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7 It is a disguised question if you don't know the term 'diamond operator' – Jason V Jun 13 '17 at 14:26
  • 1
    Indeed, it's often very hard to research about a concept you don't know the name. I often find myself in this situations, and sometimes this site helps: symbolhound.com. It let's you search exactly what you want; for instance, try: http://symbolhound.com/?q=java+%3C%3E ;) – Luan Nico Jun 13 '17 at 14:28
  • Thankfully we have resources like SO that help guide us to the right place! this was a very easy answer, but i spent a LOT of time researching to no avail. – Jason V Jun 13 '17 at 14:31
1

Generics were introduced in Java 1.5, and the diamond operator was introduced in Java 1.7 as a convenient way of declaring generic types.

In most cases, when working above Java 1.7, you would use the diamond operator for the sake of brevity. In some cases, you might still declare the type if readability warrants showing the generic type in the statement.

Zircon
  • 4,677
  • 15
  • 32
1

Since Java 7 you can use diamond operator <> to create generics.

 List<Object> objectList = new ArrayList<>();

It is correct in JDK 7+.

Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
0

Generally:

  • ArrayList<Object> is specifically a list of Objects.
  • ArrayList<> is a list without a concrete type (which means you can't add anything to the list except NULL).

In your situation they are both equal as you are declaring the type already.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Drahoš Maďar
  • 517
  • 2
  • 6
  • 22
  • the question was more specifically addressing the right hand side, with the inclusion or absence of the specifier. The term 'Diamond operator' is exactly what i was looking for:) – Jason V Jun 13 '17 at 14:34
  • Yes, diamond operator.. Without it its raw type. – Drahoš Maďar Jun 13 '17 at 14:36
0

It is possible to use both versions.The shortened version is introduced in 2009 as part of Java 7. This allows you to write more compact (and readable) code by saving repeated type arguments.

Yohannes Gebremariam
  • 2,225
  • 3
  • 17
  • 23