user1577173
user1577173

Reputation: 91

Creating an ArrayList

I've seen an arry list set up as follows

private ArrayList<Student> students; //instance variable

public CollegeCommunity()//constructor

{
    students = new ArrayList<Student>();
}

And as follows

 ArrayList exampleArray = new ArrayList();

I understand why one has an instance variable/constructor and the other just is simply declared but I'm unsure as to why the first contains <Student> and the other has no <...> What is the difference between the two?

Upvotes: 0

Views: 462

Answers (5)

obataku
obataku

Reputation: 29656

The difference lies in the language feature known as generics, which are explained in the Java Tutorials trail here.

exampleArray is declared using the raw type of the generic class ArrayList...

A raw type is the name of a generic class or interface without any type arguments.

Raw types have no generic type information, and thus are identical to uses of the class before generics were introduced.

On the other hand, students uses a generic parameterized type.

A generic type is a generic class or interface that is parameterized over types.

Generics are useful in that they allow the compiler to have more type information and thus ease programming by allowing it to effectively infer types, which is used in enforcing strong typing and eliminating casts.

The raw ArrayList is almost equivalent to ArrayList<Object>. The difference is highlighted in Effective Java.

Just what is the difference between the raw type List and the parameterized type List<Object>?

Loosely speaking, the former has opted out generic type checking, while the latter explicitly told the compiler that it is capable of holding objects of any type. While you can pass a List<String> to a parameter of type List, you can't pass it to a parameter of type List<Object>. There are subtyping rules for generics, and List<String> is a subtype of the raw type List, but not of the parameterized type List<Object>.

As a consequence, you lose type safety if you use raw type like List, but not if you use a parameterized type like List<Object>.

Upvotes: 3

Jack
Jack

Reputation: 133669

The difference is just a hint to the compiler. The declaration new ArrayList<Student>() enforces the fact that you are going to store just instances of Student class inside the specified collection. This allows Java to keep strong type-safety through generics.

At runtime nothing won't change since Java has type erasure but this strong constraint will help you: the compiler will raise errors whenever you will try to use the collection with a different time compared to the one you used to declared it. It's called parametric polymorphism.

The raw declaration new ArrayList() instead instantiates a list on which there is no assumption for the type of objects you are going to store inside it: this means that everything can be stored but you will need to cast back objects when retrieving them. In addition you won't have any type safety since at compile time the compiler wouldn't be able to check what you will try to insert inside the collection.

Upvotes: 0

Noah Passalacqua
Noah Passalacqua

Reputation: 802

the reason there is the <Students> next to ArrayList is because you need to specify what type of data the ArrayList will be holding. in this case the person is telling java that he want an ArrayList that contains the data type of class Students

without doing this it will not work.

Upvotes: 0

Reimeus
Reimeus

Reputation: 159874

The first ArrayList declatation has the generic type defined whereas exampleArray has not. It is advisable where possible to use generics to avoid the need for casting:

E.g.

ArrayList<Student> students = ...;
Student student = students.get(0);

versus:

ArrayList exampleArray = ...;
Example example = (Example)exampleArray.get(0);

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 191058

students is using what is called generics. Generics constrain the types that can be passed to the ArrayList. exampleArray does not specify a type.

Upvotes: 0

Related Questions