Blog Image

Use of Generics in java  

In this blog I will explain you, How to work with Generics, Where to use generics, If We use Generics then what is the benefits we have. Specially in this blog i explain you how to use generics concept in java collection. Before java 5 Generics are not there, In java 5 Generics is added.

The main reason of adding Generics in java are Provide type safety, resolve type casting problem and removing risk of ClassCastException (ClassCastException is very common while working with Collection classes before java 5).
Example:-
The List interface represents a list of Object. That means we can put any object into a List. Here is an example:

List listObject = new ArrayList();

listObject.add(new Integer(15));
listObject.add("This is a String");

we added any object to this list, so we need to cast these objects at the time of getting the object value,
like bellow :-
Integer integerData = (Integer) listObject.get(0);
String stringData   = (String) listObject.get(1);

If we don't type cast the object properly, then it throws ClassCastException.
If we type cast the object like bellow then it is not showing any compile time error but it is throwing run time exception i.e ClassCastException.
Integer integerData1 = (Integer) listObject.get(0);
Integer integerData2 = (Integer ) listObject.get(1);/* ClassCastException */

To seeing this type of problem in java 5 Generics concept introduce,

With the Java Generics features we can set the type of the collection to limit what kind of objects can be inserted into the collection. And we don't need to cast the values when getting from the collection.
Example of a Java Generic features:-
List<String> listOfStringObject = new ArrayList<String>();
listOfStringObject.add("This is a String");
String stringValue = listOfStringObject.get(0);

Here we don't need to type cast while getting the value from list object.
If we try to get the object into a different object attribute then it is showing compile time error, we can not compile that code, like bellow
Integer intValue = listOfStringObject.get(0); /*compile time error*/

See the bellow example for more understanding :-
package com.srimanjavagroup.generics;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class JavaGenericsExample {
    public static void main(String args[]) {
        List<String> listOfStringObject = null;
        String singleStringValue = null;

        listOfStringObject = new ArrayList<String>();
        listOfStringObject.add("Sriman Java Group Admin");
        listOfStringObject.add("Sriman Java Group Empower");
        /* listOfStringObject.add(555); */ /* Here it is showing compile time error */

        singleStringValue = listOfStringObject.get(1);/* Here type casting is not required */
        System.out.println("The 2nd String value is : " + singleStringValue);

        Iterator<String> iteratorString = listOfStringObject.iterator();
        while (iteratorString.hasNext()) {
            System.out.println(iteratorString.next());
        }
    }
}

Output :-
The 2nd String value is : Sriman Java Group Empower
Sriman Java Group Admin
Sriman Java Group Empower

Please refer to the Generics.zip file attached to this blog for reference.

Attachments

About author

User Image
ManasPanda

Myself Manas Ranjan Panda and I am from Kalahandi, a District of state Odisha. I am a java Developer in one of the Software Company. I am Technically sound in Java Programming and I am very much comfortable with Web Application Development. I have good knowledge on Spring framework, Web Services, Hibernate, Restful Services, oracle, MySQL, HTML, CSS, JavaScript, Ajax, Json etc.

1

-Comments

Be the first person to write a comment for this Blog
  • author image
    by:
      MaheswaraD
      16-12-2017 06:57:37 PM

    Nice blog. thanks for sharing your knowledge it' very helpful for us

Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.