Java Generics is the method or class that can be used to on the parameter of different types like int. char, string etc. They are called parameterized types. For example there can be a sort generic that sorts an array of integers, characters and strings depending on the input. Hence Generics provide the feature of type safety.
Java Generics were introduced in 2014 in J2SE 5 version to include type safe objects. There are two types of generics in Java:
- Generic method
- Generic class
Java Generic method
A Java generic method is a method that has a single definition for various types of arguments. The Java compiler handles each method specifically for each type of parameter passed while the method is called.
While declaring a generic method, the set of parameters is defined inside angle braces <>. This section of parameters is written before the method return type. The parameter section can contain one or more types of parameters. Each data type is separated from another by comma. The syntax of defining a generic method is:
Here, void is the return type of the method having name methodName and <Parameter>. The type parameter is also called type variable. The method body of a generic method is similar to the body of a regular method. In some cases type parameters can also define the return type of method as well as the type of own arguments of the method.
The code below demonstrates the concept of Generic Method:
// File name is GenericMethod.java public class GenericMethod { // generic method displayArray public static < E > void displayArray( E[] inputArr ) { // Displays the array elements for(E arrElement : inputArr) { System.out.printf("%s ", arrElement); } // Prints new line System.out.println(); } public static void main(String args[]) { // Creates arrays of Integer, Double and Character types Integer[] intArray = { 10, 20, 30, 40, 50 }; Double[] doubleArray = { 1.12, 2.23, 3.34, 4.45 }; Character[] charArray = { 'H', 'E, 'L', 'L', 'O', '!' }; System.out.println("The Array of type integer contains the following elements:"); displayArray(intArray); // passes an Integer type array System.out.println("\nThe Array of type double contains the following elements:"); displayArray(doubleArray); // passes a Double type array System.out.println("\nThe Array of type character contains the following elements:"); displayArray(charArray); // passes a Character type array } }
In the above code, user defines a generic method called displayArray() that displays the elements of the arrays. The program calls the method three times, each time with a different type of array. The same method prints array elements of different types. The output of the above code after compilation is:
Java Generic class
Generic class declaration in Java is similar to a non generic class declaration. A generic class also contains a parameter section. The generic class can also be called as parameterized classes. Another name for these is parameterized types as these classes can accept more than one types of parameters. The parameters are written in angular braces <> in generic class declaration just like in C++.
The syntax of declaring a generic class is:
Here the class name is ClassName having <Parameter>.
The code below demonstrates the concept of Java Generic Class. The code contains a generic class Example having parameter E.
// File name is Example.java public class Example<E> { private E e; public void add(E e) { this.e = e; } public E get() { return e; } public static void main(String[] args) { Example<Integer> integerExample = new Example<Integer>(); Example<String> stringExample = new Example<String>(); integerExample.add(new Integer(101)); stringExample.add(new String("Hello World!")); System.out.printf("Integer Value is :%d\n\n", integerExample.get()); System.out.printf("String Value is :%s\n", stringExample.get()); } }
The output of the above code after compilation is:
Advantages of Java Generics
- The main advantage of generics is that they provide type safety of objects.
- User also does need to use type casting of objects in Generics.
- There is less chance of errors in the code as generics perform compile time checking. The good programming practice is to get rid of bugs on compile time rather than doing so on run time.