|
|
@@ -11,14 +11,15 @@ You have to specify which type you want to put into the class when you construct
|
|
11
|
11
|
|
|
12
|
12
|
For example you could construct a list of `Integers`:
|
|
13
|
13
|
|
|
14
|
|
-`List<Integer> someList = new LinkedList();`
|
|
|
14
|
+`List<Integer> someList = new LinkedList<>();`
|
|
15
|
15
|
|
|
16
|
16
|
Now `someList` can only contain `Integers`. You could also do:
|
|
17
|
17
|
|
|
18
|
|
-`List<String> someOtherList = new LinkedList()`
|
|
|
18
|
+`List<String> someOtherList = new LinkedList<>()`
|
|
19
|
19
|
|
|
20
|
20
|
Now `someOtherList` can only contain `Strings`.
|
|
21
|
21
|
|
|
22
|
22
|
Another constraint is that any type used with generics cannot be a [primitive type](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html), such as `int` or `long`.
|
|
|
23
|
+However, every primitive type has a corresponding reference type, so instead of `int` you can use [`Integer`](https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html) and instead of `long` you can use [`Long`](https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html).
|
|
23
|
24
|
|
|
24
|
25
|
It can help to look at an [example use case of generics](https://docs.oracle.com/javase/tutorial/java/generics/types.html) to get you started.
|