- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why do we need generics in Java?
Reference types
As we know a class is a blue print in which we define the required behaviors and properties and, an interface is similar to class but it is a Specification (containing abstract methods).
These are also considered as datatypes in Java, unlike other primitive datatypes a literal of these kind of types points/refers to the location of the object. They are also known as reference types.
Generics
Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically.
By defining a class generic you are making it type-safe i.e. it can act up on any datatype. To understand generics let us consider an example −
Example
In the following Java example we are defining a class named Student whose constructor (parameterized) accepts an Integer object. While instantiating this class you can pass an integer object
class Student{ Integer age; Student(Integer age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } } public class GenericsExample { public static void main(String args[]) { Student std = new Student(25); std.display(); } }
Output
Value of age: 25
Passing any other object to the constructor of this class generates a compile time exception
public static void main(String args[]) { Student std = new Student("25"); std.display(); }
Compile time errorCompile
GenericsExample.java:12: error: incompatible types: String cannot be converted to Integer Student std = new Student("25"); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error
The age of a student could be passed as (an object of) String value, Float, Double etc. If you want to pass a value of another object you need to change the constructor as −
class Student{ String age; Student(String age){ this.age = age; } } Or, class Student{ Float age; Student(Float age){ this.age = age; } }
When you declare generic types they can act upon any datatypes and, they are known as parameterized types. You cannot use primitive datatypes in generics.
Creating generic types
To create a class of generic type by using generic parameter T or, GT as −
class Student <T>{ T obj; }
Where T (generic parameter) represents the datatype of the object you can pass to the constructor of this class. This will be determined at the compilation time.
While instantiating the class you need to/can choose the type of the generic parameter as −
Student<Float> obj = new Student<Float>();
Example
We can rewrite the above example using generics as −
class Student<T>{ T age; Student(T age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } } public class GenericsExample { public static void main(String args[]) { Student<Float> std = new Student<Float>(25.5f); std.display(); } }
Output
Value of age: 25.5
Now, while instantiating the Student class you can pass desired type of object as a parameter as −
class Student<T>{ T age; Student(T age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } } public class GenericsExample { public static void main(String args[]) { Student<Float> std1 = new Student<Float>(25.5f); std1.display(); Student<String> std2 = new Student<String>("25"); std2.display(); Student<Integer> std3 = new Student<Integer>(25); std3.display(); } }
Output
Value of age: 25.5 Value of age: 25 Value of age: 25
Why do we need generics
Usage of generics in your code you will have the following advantages −
Type check at compile time −usually, when you use types (regular objects), when you pass an incorrect object as a parameter, it will prompt an error at the run time.
Whereas, when you use generics the error will be at the compile time which is easy to solve.
Code reuse − You can write a method or, Class or, interface using generic type once and you can use this code multiple times with various parameters.
For certain types, with formal types, you need to cast the object and use. Using generics (in most cases) you can directly pass the object of required type without relying on casting.
Using generic types you can implement various generic algorithms.
- Related Articles
- Why do we need inner classes in Java?
- Why do we need a wrapper class in Java?
- Why do we need KDD?
- Why do we need Energy?
- Why do we need weakMaps in Javascript?
- Why do we need Good Manners?
- Why do we need a Database
- Why do we need Computer Networks?
- Why do we need shell scripting?
- Why do we need Data Encryption?
- Why do we need private methods in an interface in Java 9?
- Why do we need to study Physics?
- Why do we need to store food?
- Do we need forward declarations in Java?
- Why do we need a separate Data Warehouse?
