Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ArrayList declarations

I'm currently studying for the Java OCA exam and came across a question relating to ArrayList declarations.

Which of the following is valid?:

1. ArrayList al1 = new ArrayList();
2. ArrayList al2 = new ArrayList<>();
3. ArrayList<> al3 = new ArrayList<>();
4. ArrayList<Double> al4 = new ArrayList<>();
5. ArrayList<Double> al5 = new ArrayList<Float>();

According to my book, answers 1,2 and 4 are valid. Answers 3 and 5 are invalid.

However, no proper explanation is given. All it does is show the standard way to declare an ArrayList:

ArrayList<E> al3 = new ArrayList<E>();

and mentions that it's also valid to declare the ArrayList without the generic part.

I'm also unable to find a decent article on this topic online. Can someone explain (or point me in the direction of a good article) the different permutations above?

Thanks in advance.

like image 958
Ambiorix Avatar asked Mar 01 '13 10:03

Ambiorix


2 Answers

1 is valid in all versions of Java

Here you are declaring the ArrayList without using Generics. This means regardless of what you add to the arraylist, when you get it back out it will be of type Object and will require casting to a type. This is the old way of using Collections in Java 1.4 (pre generics) and is supported for backwards compatibility. Nowadays you should always use Generics.


2 and 4 are valid in Java 7 only

The empty brackets: <> are Java7's new Type inference that means you don't have to specify the type twice. Note Java7, it wont work in older versions.

So in Java7

ArrayList<Double> al4 = new ArrayList<>();

Is the same as

ArrayList<Double> al4 = new ArrayList<Double>();

This link has more info on type inference: http://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html


3 is invalid because if you are using Generics you must specify a type in the variable declaration.


5 is invalid because if you declare a List with type Double you cannot then assign it a List that is of Type Float, you can only assign it a list of type Double.

So this would be valid:

ArrayList<Double> al5 = new ArrayList<Double>();
like image 108
cowls Avatar answered Sep 21 '22 14:09

cowls


Below 2 will be valid only in Java SE7. Java SE7 allows type inference so you don't need to provide type inside <>again.

  ArrayList<Double> al4 = new ArrayList<>();
  ArrayList al2 = new ArrayList<>();

On the other hand below one is valid on all Java versions; this is to ensure backward compatibility of non-generic code.

ArrayList al1 = new ArrayList();

Below is not allowed as Collection of Float is not a sub type of collection of Double. Moreover Float is not subtype of Double; so no question of it being a valid declaration. Note that even array version doesn't compile.

 Double[] dd = new Float[5];  //won't compile
 ArrayList<Double> al5 = new ArrayList<Float>(); //won't compile

Below one is not a valid declaration.

ArrayList<> al3 = new ArrayList<>();
like image 25
rai.skumar Avatar answered Sep 17 '22 14:09

rai.skumar