Reputation: 23236
What is the difference between the two :
first :
ArrayList<String> linkList = new ArrayList<String>();
second :
ArrayList linkList = new ArrayList<String>();
Or is there any difference ?
Upvotes: 3
Views: 188
Reputation: 21329
Also in the first case linkList is of type String and in the second case is of type Object.
Upvotes: 0
Reputation: 46219
ArrayList<String> linkList = new ArrayList<String>();
uses generics to ensure type safety.
ArrayList linkList = new ArrayList<String>();
doesn't. As @BruceMartin points out, this means that the lines
linkList.add(0);
String element = (String) linkList.get(0);
gives a compile time error in the first case, but fails at runtime with the second declaration.
As another example, to get()
a String
from the two alternatives, the second variant would require a cast:
first:
String element = linkList.get(0);
second:
String element = (String) linkList.get(0);
Upvotes: 9
Reputation: 1833
At compile time: the first one uses generics, ensures type safety and code readability.
At runtime: they are the same.
Upvotes: 3