Home core java Java Generics Introduction

Java Generics Introduction

Java Generics

  • Java Generics one of important feature added in Java 5.
  • Enables types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Type parameters provide a way for you to re-use the same code with different inputs.
  • Benefits are – 1. Stronger type checks at compile time. 2. Resolves type casting problems. 3. Enabling programmers to implement generic algorithms.

Case(i) : Understanding type checks at compile time (Type Safe) :

Type checking at compile time in Arrays.

String[] students = new String[10];
students[0] = "Ajay";
students[1] = "Vijay";
students[2] = "Suresh";
students[3] = new Integer(5);//complier throws Type mismatch error
  • Arrays are always type safe, there is always  a guarantee that array can hold a particular type of objects.
  • For example if our programming requirement is to hold String type of objects it is recommended to use String array. In the case of string array we can add only string type of objects by mistake if we are trying to add any other type we will get compile time error.
  • Error is- Type mismatch: cannot convert from Integer to String

Type checking at compile time in Collections.

ArrayList students = new ArrayList();
students.add("Ajay");
students.add("Vijay");
students.add("Suresh");
students.add(new Integer(5));// No Compile time Error: Collections are not type safe.
  • Collections are not type safe (if we are not using Generics).
  • For example if our programming requirement is to hold only String objects by mistake if we add any other type we wont get any compile time error, but there may be chance of failing the program at runtime.

Case(ii) : Understanding type casting issues in Collections  :

Type Casting while retrieving elements from Arrays .

String[] students = new String[10];
students[0] = "Ajay";
students[1] = "Vijay";
students[2] = "Suresh";
 
String student1 = students[0];// Type Casting not Required
  • In the case of arrays at the time of retrieval it is not required to perform any type casting.

Type Casting while retrieving elements from Collection.

ArrayList students = new ArrayList();
students.add("Ajay");
students.add("Vijay");
students.add("Suresh");
        
String student1 = students.get(0);//Compiler Error: Type mismatch: cannot convert from Object to String
  • In the case of collection at the time of retrieval compulsory we should perform type casting otherwise we will get compile time error.

How Generics resolves type casting and provides type safety?

  • To overcome the above problems of collections(type-safety, type casting) generics introduced in java 1.5v . Main objectives of generics are: 1)  To provide type safety to the collections. 2)  To resolve type casting problems.
  • To hold only string type of objects we can create a generic version of ArrayList as follows.
//Generics version of code
ArrayList<String> students = new ArrayList<String>();//Added Generics type parameter
students.add("Ajay");
students.add("Vijay");
students.add("Suresh");
students.add(new Integer(5));//Compile time error: Type Mismatch
        
String student1 = students.get(0);// No Type Casting required
  • For this ArrayList we can add only string type of objects, by mistake if we are trying to add any other type we will get compile time error that is through generics we are getting type safety.
  • At the time of retrieval it is not required to perform any type casting we can assign elements directly to string type variables.

In this article Covered Answers for :
1. What is Generics in java?
2. What are the advantages of generics in java?
3. How generics provides type safe in java?
4. How generics resolves type casting problems in java?
5. Why generics in java?

Prashanth Kumar Chilukamari
Prashanth Kumar Chilukamari
Prashanth is a post graduate in master of computer applications, and a professional software engineer with focus on Java, Spring boot, micro services and pivotal cloud. Follow him on LinkedIn or Twitter or Facebook.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories