Reputation: 151
I'm trying to build on my introductory Java knowledge and I'm branching out into Data Structures.
I'm looking at the ArrayList
and I'm not understanding the following:
List<String> myList = new ArrayList<String>();
I googled type parameter
but I don't really understand why it's necessary and what it's doing (both for the call to the constructor and in initialising the variable); if someone could explain it to me in simple speak, that would be great.
Thanks
Upvotes: 0
Views: 133
Reputation: 17
The main usage of generics is to provide type safety. If you do not use generics, objects will come out as a reference of type object. Before generics there was no way to declare the type of an arraylist,so its add() method took type object by default.
For simple eg. to understand generics is as follows
ArrayList<String> l=new ArrayList<String>();
l.add(null); // It will add type of string
Here, with generics you can now only put string objects in the ArrayList and the objects will come out as a reference of type string.
In short, with generics you can create type-safe collections where more problems are caught at compile time instead of run time
Upvotes: 0
Reputation: 41223
This is an example of Generics, but for a while you can get away without knowing the gory details of Generics -- tutorials will show you how to write a class like ArrayList
; for now you just need to know how to use it.
Prior to Generics, you would declare a List like this:
List l = new ArrayList();
... and a List contained a collection of objects. The Java runtime didn't care what kind of object you put in.
l.add(new Integer(3);
l.add("A String");
... and the Java runtime couldn't tell you what type the object you got out was, so you had to cast it:
String s = (String) l.get(1); // throws a ClassCastException if object is not a String
This meant that all kinds of programming errors that could be detected by the compiler, if only the language could express itself better, were only able to be caught at runtime.
So, instead, let's add something that says what the List contains:
List<String> l = new ArrayList<String>();
This means that you can only add()
Strings to the list, and anything you get()
from the list will be a String:
l.add(new Integer(1)); // compile error -- incompatible types
l.add("Hello"); // works
String s = l.get(0); // works, without casting
It works with polymorphism. So if Apple
and Banana
are subclasses of Fruit
:
List<Fruit> fruitList = new ArrayList<Fruit>();
fruitList.add(new Apple());
fruitList.add(new Banana());
If you look at the JavaDoc for List
, you'll see that it's documented as List<E>
- where E
is a placeholder for whatever type you plug in when you declare the variable.
The methods are documented in the same way: boolean add(E e)
, E get(int index)
, and so on.
Upvotes: 0
Reputation: 6667
First of all, I would strongly suggest that you read the Java generics tutorial.
Secondly, to answer your question simply. By using a generic type like this you are forcing the compiler to use strong type checking on the code using this instance.
If you defined your list as:
List myList = new ArrayList();
You would be able to add objects of any type to it, such as:
myList.add(new Integer());
myList.add(new Long());
By declaring the list as:
List<String> myList = new ArrayList<String>();
You are telling the compiler that this list will only accept Strings, so:
myList.add(new Integer());
will throw a compile time error.
Upvotes: 6
Reputation: 534
Generics tell to compiler that ArrayList can contain only String objects.
Upvotes: 0
Reputation: 3749
In Java 7 you can use the Diamond operator instead (think of it as inverse type inference, if that helps), so:
List<String> myList = new ArrayList<>();
It means that you can only add objects to this List that are of class String
or sub-classes of String
.
Generics are generally more useful with Interfaces than Concrete classes.
Upvotes: 1
Reputation: 4640
To understand this, you'll need to understand how generics work in java. In short words, the type allows the Generic ArrayList to cast each element to a type and return each object as type.
The Implementation looks something like this
public class ArrayList<T>{
public void add(T element)
public <T> get()
}
In your example, this allows you to use the array list with Strings
In the background objects still were objects but where castet for each read / write access.
So you have something like a typesafety ( which can be broken as you can read here).
Upvotes: 0
Reputation: 807
With this declaration you're just setting the data type of the ArrayList. In this way you ensure that the ArrayList is an array of strings
Upvotes: 0
Reputation: 9479
The type parameter in your case String says the ArrayList can hold only String and no other types allowed. It also helps when you retrieve the value from the ArrayList you would be sure that the output is String and can directly assign to a String.
Upvotes: 0