Understanding generics in Java
- Eclipse: Oxygen
- Java: 1.8
This tutorial deals with the type parameters that are used in generics in Java. The type parameters related to generics are described here. Here its use in the program is shown. Users will be able to better use generics with this.
In the following program we write generic functions that can be called with different types of arguments depending on the type of arguments passed to the generic method, the compiler manages each method without any runtime error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
class Data<T>{ private T myVariable; public Data(T myVariable) { super(); this.myVariable = myVariable; } public T getMyVariable() { return myVariable; } @Override public String toString() { return "Data [myVariable=" + myVariable + "]"; } } public class App { public static void main(String[] args) { List<Data<Object>> elements = new LinkedList<>(); elements.add(new Data<Object>("Some text")); elements.add(new Data<Object>(1.0)); elements.add(new Data<Object>(1)); elements.add(new Data<Object>('%')); elements.add(new Data<Object>(5.0f)); App app = new App(); app.printList(elements); } void printList(List<Data<Object>> list) { ListIterator<Data<Object>> iterator = list.listIterator(); while (iterator.hasNext()) { System.out.println("Element: " + iterator.next());} } } |
In the following program, we demonstrate that using generics converts runtime exceptions into a compile-time exception.
Here we want to apply the restriction at the time of defining List, hence we create a list with String specified. Now compiler does not allow any other data type value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
class Data<T>{ private T myVariable; public Data(T myVariable) { super(); this.myVariable = myVariable; } public T getMyVariable() { return myVariable; } @Override public String toString() { return "Data [myVariable=" + myVariable + "]"; } } public class App { public static void main(String[] args) { List<Data<String>> elements = new LinkedList<>(); elements.add(new Data<String>("Some text")); elements.add(new Data<String>("One")); elements.add(new Data<String>("Two")); elements.add(new Data<String>(1.0)); elements.add(new Data<String>(1)); App app = new App(); app.printList(elements); } void printList(List<Data<String>> list) { ListIterator<Data<String>> iterator = list.listIterator(); while (iterator.hasNext()) { System.out.println("Element: " + iterator.next().getMyVariable()); } } } |
Output
1 2 3 4 5 |
Exception in thread "main" java.lang.Error: Unresolved compilation problems:     The constructor Data<String>(double) is undefined     The constructor Data<String>(int) is undefined |
In the following program, we demonstrate the entire operation through the Integer object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
class Data<T>{ private T myVariable; public Data(T myVariable) { super(); this.myVariable = myVariable; } public T getMyVariable() { return myVariable; } @Override public String toString() { return "Data [myVariable=" + myVariable + "]"; } } public class App { public static void main(String[] args) { List<Data<Integer>> elements = new LinkedList<>(); elements.add(new Data<Integer>(1)); elements.add(new Data<Integer>(2)); elements.add(new Data<Integer>(3)); elements.add(new Data<Integer>(4)); elements.add(new Data<Integer>(5)); App app = new App(); app.printList(elements); } void printList(List<Data<Integer>> list) { ListIterator<Data<Integer>> iterator = list.listIterator(); while (iterator.hasNext()) { System.out.println("Element: " + iterator.next().getMyVariable() * 10); } } } |
Output
1 2 3 4 5 6 7 8 9 |
Element: 10 Element: 20 Element: 30 Element: 40 Element: 50 |
Contributed by: Poonam Tomar