Introduction to Generics in Java

In this article, we will discuss new feature introduced in Java 1.5 version called Generics

Before starting with introduction part, we will go through few things which will help us to understand the main objective/motive behind introducing Generics feature in Java 1.5 version

 

Background (pre-era of Generics):

To store group of objects in Java, we have either

  1. Arrays
  2. Collection

 

Let us start with Arrays first, then slowly we will move into Collection

Arrays:

  • Whenever, we create Arrays then we always provide data-type or reference-type like int, String or float, etc. (class or interface for reference-type)
  • That’s we can assure that all the elements present inside Arrays are of same data-type or reference-type
  • Assume that, if we create String Arrays then it can accommodate String-type of objects only (of course within its specified size-limit)
  • Suppose, if we try to add/assign other type of element to String Array then compile-time error is thrown
  • Which makes developer task easy to rectify error at compile-time, without taking it to run-time
  • For example, in the below screen-capture when we tried to assign int type to String Array, then immediately compile-time error is thrown stating “Type mismatch: cannot convert from int to String”. Therefore, it proves that Arrays are type-safe

  • Since, Arrays stores similar type of objects and restricts other type of objects to be stored, therefore it is very easy to extract or get objects from Arrays at the time of retrieval without explicit type-casting
  • For example, in the below screen-capture we are retrieving Arrays data without any explicit type-casting

  • Hence, Arrays are type-safe as well as it isn’t required to perform explicit type-casting at the time of retrieval

 

Let us move on and discuss Collection taking ArrayList as example

Collection:

  • With Collection, we can always store any type of objects
  • So, therefore Collection aren’t type-safe.; we can add String, Integer or any other reference-type to same Collection object
  • In below example as shown in the screen-capture, ArrayList object holds String, Integer or reference-type and there isn’t any compile-time error thrown by compiler
  • Note: warning shown in the screen-capture is because of higher version JDK used
  • Assume that, for our requirement we have created ArrayList object and stored only String type of Objects to ArrayList, then also it is mandatory to perform explicit type-casting at the time of retrieval
  • In below example as shown in the screen-capture, we have created ArrayList and stored only String type of objects but at the time of retrieval we are still performing explicit type-casting

  • Hence, Collection aren’t type-safe and also it is mandatory to perform type-casting at the time of retrieval

 

Question: Now, one may ask when Arrays helps to store group of similar objects which assures type-safety while storing and also it isn’t required to perform explicit type-casting, then why one should to move to Collection

The answer is very simple,

  • Arrays are fixed in size and
  • Collection are grow-able in nature and also as objects removed from Collection it shrinks
  • Which is not possible in Arrays

 

Let us move on to understand what is the need of Generics in Java

Need of Generics in Java:

To overcome above listed problem in Collection Sun (now Oracle) people introduced Generics in Java 1.5 version

The main motto behind Generics are,

  1. To provide type-safety while storing objects
  2. To resolve type-casting problem, at the time of object retrieval

Syntax of Generics w.r.t ArrayList:

Assume that, to store group of String objects then we should declare ArrayList as follows

ArrayList<String> gal =new ArrayList<String>();
  • This ArrayList stores String type of objects only
  • In case, if we try to add any other type of objects like Integer then compile-time error is thrown stating “The method add(String) in the type ArrayList<String> is not applicable for the arguments (Integer)
  • Similarly, it isn’t required to do explicit type-casting as ArrayList stores String-type of objects only (as you can see from above screen-capture)
  • Syntax explanation: after ArrayList, define open angle-bracket (<) & closed angle-bracket (>) and between them specify type of objects to be stored. For example, in above case, it is String object

 

Hence, with generics we can resolve both problems i.e.; to provide type-safety and type-casting problem

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute or tricky situation you faced during Interview hours, then share with us. We will include that code here

 

References:

 

Happy Coding !!
Happy Learning !!

Generics classes in Java