### /com/in28minutes/java/generics/GenericsExamples.java
```
package com.in28minutes.java.generics;
import java.util.ArrayList;
public class GenericsExamples {
static ArrayList multiplyNumbersBy2(ArrayList numbers) {
ArrayList result = new ArrayList();
for (int i = 0; i < numbers.size(); i++) {
// TYPE CAST is required
int value = (Integer) numbers.get(i);
result.add(value * 2);
}
return result;
}
static void addElement(ArrayList something) {
something.add(new String("String"));
}
public static void main(String[] args) {
// Older code - Before Java 5
ArrayList numbers = new ArrayList();
numbers.add(5);
numbers.add(6);
// javac gives warning because multiplyNumbersBy2(ArrayList) is invoked
// with a Specific ArrayList
// and in the method multiplyNumbersBy2 an element is added to ArrayList
// com/rithus/generics/GenericsExamples.java uses unchecked or unsafe
// operations.
// Recompile with -Xlint:unchecked for details.
System.out.println(multiplyNumbersBy2(numbers));// [10, 12]
// javac gives warning because addElement(ArrayList) is invoked with a
// Specific ArrayList
// and in the method addElement, an element is added to the list.
// com/rithus/generics/GenericsExamples.java uses unchecked or unsafe
// operations.
addElement(numbers);
// Throws runtime exception - java.lang.ClassCastException
System.out.println(multiplyNumbersBy2(numbers));
// New code - After
}
// javac -Xlint:unchecked com/rithus/generics/GenericsExamples.java
// com/rithus/generics/GenericsExamples.java:14: warning: [unchecked]
// unchecked call to add(E) as a member of the raw type java.util.ArrayList
// result.add(value * 2);
// ^
// com/rithus/generics/GenericsExamples.java:21: warning: [unchecked]
// unchecked call to add(E) as a member of the raw type java.util.ArrayList
// something.add(new String("String"));
// ^
// 2 warnings
}
```
### /com/in28minutes/java/generics/GenericsExamples2.java
```
package com.in28minutes.java.generics;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Animal {
}
class Dog extends Animal {
}
public class GenericsExamples2 {
static void doSomethingArray(Animal[] animals) {
// do Something with Animals
}
static void doSomethingList(List animals) {
// do Something with Animals
}
static void doSomethingListModified(List extends Animal> animals) {
// Adding an element into a list declared with ? is prohibited.
// animals.add(new Animal());//COMPILER ERROR!
// animals.add(new Dog());//COMPILER ERROR!
}
static void doSomethingListModifiedSuper(List super Dog> animals) {
// Adding an element into a list declared with ? is prohibited.
// animals.add(new Animal());//COMPILER ERROR!
// animals.add(new Dog());//COMPILER ERROR!
}
/* even for interfaces extends keyword should be used */
static void doSomethingListInterface(List extends Serializable> animals) {
// Adding an element into a list declared with ? is prohibited.
// animals.add(new Animal());//COMPILER ERROR!
// animals.add(new Dog());//COMPILER ERROR!
}
public static void main(String[] args) {
Animal[] animalsArray = { new Animal(), new Dog() };
Dog[] dogsArray = { new Dog(), new Dog() };
List animalsList = Arrays.asList(animalsArray);
List dogsList = Arrays.asList(dogsArray);
// Array method can be called with Animal[] or Dog[]
doSomethingArray(animalsArray);
doSomethingArray(dogsArray);
// List method works with List
// Gives compilation error with List.
doSomethingList(animalsList);
// List not compatible with List
// doSomethingList(dogsList);//COMPILER ERROR
// Method declared with List extends Animal> compiles
// with both List and List
doSomethingListModified(animalsList);
doSomethingListModified(dogsList);
// Method declared with List super Dog> compiles
// with both List and List
// List of any super class of Dog is fine.
// List of any Subclass of Dog is not valid parameter.
doSomethingListModifiedSuper(animalsList);
doSomethingListModifiedSuper(dogsList);
// A method declared with List