Generic provides various types of advantages. By use of generic Java makes ease for the programmer. So let’s discuss the Advantages of generics in java.
1. Type-safety: Suppose you want to store the name of books in ArrayList and by mistake, you added an Integer value instead if String. The Compiler allows it, but the problem occurs when you want to retrieve it. So, the compiler throws the error at run time.
By the use of generic, the compiler shows the errors at compile time rather than the run time. It saves the programmer time because it’s difficult to find the error on runtime. Its always better to find the errors at compile time rather than the run time.
Let’s understand without generic :
import java.util.ArrayList; class Example { public static void main(String[] args) { // ArrayList without any type ArrayList listOfNames = new ArrayList(); listOfNames.add("Sachin"); listOfNames.add("Rahul"); listOfNames.add(10); // Compiler allows this String s1 = (String)listOfNames.get(0); String s2 = (String)listOfNames.get(1); // Causes Runtime Exception String s3 = (String)listOfNames.get(2); } }
Output: Exception in thread “main” java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader ‘bootstrap’) at Example.main(Example.java:18)
Let’s understand with generic concepts:
import java.util.ArrayList; class Example { public static void main(String[] args) { // ArrayList with string type ArrayList<String> listOfNames = new ArrayList<String>(); listOfNames.add("Sachin"); listOfNames.add("Rahul"); listOfNames.add(10); // Compiler doesn't allow this String s1 = (String)listOfNames.get(0); String s2 = (String)listOfNames.get(1); String s3 = (String)listOfNames.get(2); } }
Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: The method add(int, String) in the type ArrayList<String> is not applicable for the arguments (int) at Example.main(Example.java:12)
2. Typecasting is not required: As you can see in the above example, If we are not using generic ArrayList, during the retrieve data from ArrayList we have to typecast it. This typecast is needed every time. It’s a big problem for programmers.
So, the generic solve this problem because if we already know that, the data type list then we don’t need to cast it every time. It is the one of best advantages of generics in java
3. We can write a generic method, class, and Interface. So that we can reuse the code. As you can see the Collection framework provides many classes that are generic. You can use them according to your data type.
Let’s take an example of Map:
import java.util.HashMap; import java.util.Map; class Example { public static void main(String[] args) { // Map containing Integer as key and String as Value Map<Integer, String> mapIntString = new HashMap<Integer, String>(); mapIntString.put(1, "Hello"); mapIntString.put(2, "JAVA"); System.out.println("Value form Map"); for(Integer key : mapIntString.keySet()) System.out.println(mapIntString.get(key)); // Map containing Integer as key and Float as Value Map<Integer, Float> mapIntFloat = new HashMap<Integer, Float>(); mapIntFloat.put(1, 1.2f); mapIntFloat.put(2, 2.4f); System.out.println("Value form Map"); for(Integer key : mapIntFloat.keySet()) System.out.println(mapIntFloat.get(key)); } }
Output: Value form Map
Hello
JAVA
Value form Map
1.2
2.4
Example of Generic method that is resued according to data type:
public class ExampleWithGeneric { public static void main(String args[]) { ExampleWithGeneric obj = new ExampleWithGeneric(); System.out.println("Calling by integer values"); obj.showData(5, 10); System.out.println("Calling by String values"); obj.showData("Hello", "Java"); System.out.println("Calling by float values"); obj.showData(5.1f, 10.2f); } public <T> void showData(T a, T b) { System.out.println(a); System.out.println(b); } }