Introduction to Generics in Java

In this tutorial I will explains you what is Generics in Java and why it was Introduced? This article is and Introduction to Generics in Java, which explains you the benefits of Generics in Java.

Introduction to Generics in Java

In this tutorial I will explains you what is Generics in Java and why it was Introduced? This article is and Introduction to Generics in Java, which explains you the benefits of Generics in Java.

Introduction to Generics in Java

Introduction to Generics in Java: What are the benefits of using Generics in Java?

The Generics Features was added to the Java with the release of Java 5.0. The Generics was the long-waited enhancement which is added to the Java 5.0. The Generics allows to use the type or method operate on the objects of various types, mainly for the compile time type safety checking. Generics was introduced to make the coding more robust and less error prone. Through compile-time type safety checking it removes bugs from the Java code.

In any software development project bugs are get introduced due to human error or simply missing some logic. So, the software testing was developed to test the software thoroughly and removed the bugs from it. Some bugs are easy to remove and some are very difficult to find and remove. The bugs may also get introduced in your application while adding a new feature to the existing applications.

The compile time bug detection is very easy and it is reported by the compiler. The runtime bug finding and fixing is also a big work. The Java Generics was introduced which actually checks the bugs at the compile time. For example if you are typecasting the String into Integer at runtime it will give you error. So, using the Generics you can reduce the bugs in your program.

Introduction to Generics in Java

What is Generics in Java?

Generics is introduced in the JDK version 5.0 and it is a facility for generic programming in the Java programming language. With the help of Generics in Java you can define a type or method to an object of various, which will be used for compile-time safety checking. Philip Wadler created Generic Java in the year 1998.

Following is simple example of Generics in Java:

List<String> list = new ArrayList<String>();
list .add("Hello");
Integer i = list .get(0); // (type error) Compile time error

In the above code we have used the Generics <String> while defining the array list. The type of to be stored in the array list is just the String. If we try to type cast the data into Integer it will give compile time error. So, this way compiler will be able to find our the error at the compile time in your program and remove the errors related to invalid types.

Why use Generics in Java program?

The Generics is introduced for the compile time checking of the types, so you can now use Generics in your Java make it more bug free. The compile time checking helps the programmers to find out the bugs at the development time and remove it from the code.

Generics allows the developers to use the types (classes/interfaces) while defining classes, interfaces and methods in your Java program. This is much like defining the formal parameters used while defining methods. This will enable the code to just accept the values of defined type only and it is checked at compile time.

Advertisement

Here are the benefits of using the Generics over the non-generic Java codes:

  • Stronger type checking: The Generics type checking are performed at the compile time and it reports if there is some violation of the types. So, your program are now more bug free.
     
  • Elimination of cast in the program: If you use the Generics in your program there is no need to cast to particular type in your code. For example in the above code we have define it as String type for the list and we don't have to cat it to String. We can simple write the code String s = list .get(0);
      
  • Use of generic algorithms: By using the Generics in your code you can use the generic algorithms that works with collections and other types. You will also make your code type safe and error free.

Generics Naming conventions

The Generics in Java follows some notations and the naming convention. The main problem of using the Generics is that the developers is not aware of the naming conventions of the Generics. After familiarizing these naming conventions and the notations you will find it very easy. Here is the naming conventions followed in the Java Generics:

Generic Term Meaning
Set<E> This is the Generic Type and here E is called formal parameter
Set<Integer> This is Parameterized type and Integer is actual parameter here
<T extends Comparable> This is bounded type parameter in Generics
<T super Comparable> This is bounded type parameter in Generics
Set<?> This is Unbounded wildcard in Generics
<? extends T> This is Bounded wildcard type in Generics
<? Super T> This is Bounded wildcards type in Generics
Set This is Raw type
<T extends Comparable<T>> This is Recursive type bound in the Generics

Conventions:

T - Is used to denote type
E - Is used to denote element
K - Is keys
V - Is the values
N - Is used for the numbers

Check the Advanced Java Tutorials.