Last Updated: 26 March, 2023
Generics were introduced in Java 5 (J2SE 5), and the idea behind introducing generics in Java is to reduce bugs and add an extra layer of abstraction over data types in Java.
Generic provides compile-time type safety of code and protects from ClassCastException at runtime. Generics provides a feature to create a single class, interface, or method that could be able to perform operations on various types of objects.
Generics are denoted by the use of angle brackets, <>, which contain the type parameter.
In Java collections, generics are used to specify the type of objects to be stored in a collection instance. Without generics, we can store any type of object in a collection, i.e., non-generic. Now, generics force the programmer to store a specific type of object.
Generics only work on objects, not primitive types such as int, char, float etc.
The Java generics are similar to the templates in the C++ programming language.
Type-safety: It forces the programmer to store specific data types of objects only, not different data types.
Type casting is not required. After generic, we do not need to explicitly typecast.
Compile-Time Checking: Generics provides compile-time type safety that allows programmers to catch invalid types at compile time instead of run time.
The type parameters naming conventions are important to learn generics thoroughly. The common type parameters are as follows:
In Java, the generics feature is implemented using the following.
The Java generics allow the creation of generic methods that can perform the same functional operations with different data types of values.
We create a single generic method that can be called with arguments of different types. Each method call is handled appropriately by the compiler based on the types of arguments passed to the generic method.
Let's understand the implementation of the generic method with the help of the example given below.
Example 1: Generic Method Implementation in Java
Output
Hello Java
542373
55.75
true
Example 2: Generic Method Implementation in Java
Output
Printing Integer Array elements
1 2 3 4 5
Printing Float Array elements
10.1 20.2 30.3 40.4 50.5
Printing String Array elements
RED GREEN BLUE WHITE GRAY
Printing Boolean Array elements
true false true false
A generic class declaration is similar to a normal Java class; only an additional class name is followed by a type parameter section. A generic class works with different types.
Let's understand the implementation of the generic class with the help of the given example below.
Example 3: Generic Class Implementation in Java
Output
250
Hello Java
That's all guys, hope this Java article is helpful for you.
Happy Learning.
feedback@javabytechie.com
If the compiler erases all type parameters at compile time, why should we use Generics?
Ans. There are the following reasons to use generics in Java:
Can we use generics with an array?
Array doesn't support generics, and that's why Joshua Bloch suggested in Effective Java to prefer List over Array because List can provide compile-time type safety over Array.
Can you pass List <String> to a method that accepts List<Object>?
Ans. String is an object, so List<String> can be used where List<Object> is required, but this is not true. It will result in a compilation error. It does make sense if we go one step further because List<Object> can store anything, including String, Integer etc., but List<String> can only store strings.
List<Object> objectList;
List<String> stringList;
objectList = stringList; //compilation error incompatible types
What are the Type Parameters in Java Generics?
Ans. The type parameters naming conventions are important to learn generics thoroughly. The common type parameters are as follows:
How can we suppress the unchecked warnings in Java?
Ans. From Java 5 onwards, the java compiler (javac) generates an unchecked warning if we use combine raw types and generics type e.g.
List<String> list = new ArrayList();
we can use @SuppressWarnings("unchecked") annotation to suppress the unchecked warning in java.