Generics in Java
Agenda
- Introduction to Generics in Java
- Generics classes
- Bounded Types
- Generic methods & wild card character(?)
- Communication with non-generic code
- Conclusions
Introduction to Generics in Java
The generics concept in java was introduced in Java 1.5 to deal with type safe objects.
The main objectives of Generics is
- To provide type safety and
- To resolve type casting problems.
Case 1 : Type Safety
In the above code, we have declared an array of String types. So, we can add only string type of objects. If we try to add any other types we will get Compile time errors.
So, we can say that Array is type safe because it allows a specific type of objects.
Let's apply the same code in the Collection.
In ArrayList we can add any type of objects. So, It is not type safe.
Array is type safe because we can store only specific type of objects in array but Collection is not type safe because we can add any type of objects in Collection.
For example, If our programing requirement is to store only string type of objects and we choose ArrayList. By mistake if we are trying to add any other type of objects we will not get get compile time error because ArrayList allows different types of objects. But the program may fail at runtime.
In ArrayList we can add any type of objects. So, It is not type safe.
Array is type safe because we can store only specific type of objects in array but Collection is not type safe because we can add any type of objects in Collection.
For example, If our programing requirement is to store only string type of objects and we choose ArrayList. By mistake if we are trying to add any other type of objects we will not get get compile time error because ArrayList allows different types of objects. But the program may fail at runtime.
Case 2 : Type Casting
In the above code snippet we can see while assigning the value of index 0 to name1 variable type casting is not required.
But in the Collection, type casting is mandatory because get method returns Object type. If we do not type cast, we will get runtime exception which is ClassCastException.
So, to achieve type safety, resolve type casting problems and to detect the bugs at compile time, Sun People introduced Generics in Java 1.5
How to create generic version of ArrayList object.
For example, to hold only String type of objects we can create generic version of ArrayList object is as follows :
Type Safety in ArrayList through Generics
In the above code, We are getting compile time error if we add Integer type to Generic ArrayList.
Through generics we are getting type safety in Collection.
Communication with non-generic code
Here, we have to discuss 2 things like what will happen if we send
- Generic object to non-generic area and
- Non-generic object to generic area.
It is decided by the location, the location where object is currently present based on that behavior of the object is defined.
It means If we send a generic object to non-generic area, it starts behaving like non-generic object.
And if we send a non-generic object to generic area, it starts behaving like generic object.
Example 1: Sending generic object to non-generic area
Example 2: Sending non-generic object to generic area
Output :-
Type safety & type casting both are applicable at compile time. Hence generics concept also applicable only at compile time but not at run time.
At the time of compilation, as the last step generics syntax will be removed and hence for JVM, generics syntax will not be available at run time.
Let's take an example to understand it.
Output:-
If we compile above class we get compile time error, because at the time of compilation, as last step Generics syntax is removed.
So after removal of generics, the above code becomes as follows
And hence both the methods have same signature it throws compile time error.
Both declarations are equal.
At compile time
The following 3 steps take place:
- Compile the code normally by considering generics syntax.
- Remove generic syntax
- Compile the resultant code.
Post a Comment