Table of contents
1.
Introduction
2.
What are Generics?
3.
Significance of Generics
4.
Role of Generics in Collections
5.
Example of Generics
5.1.
Output
6.
Frequently Asked Questions
6.1.
What is a JIT compiler?
6.2.
What is the platform?
6.3.
What are the main differences between the Java platform and other platforms?
6.4.
What gives Java its 'write once and run anywhere' nature?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Generics in Java

Author Aditya Anand
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Generics, which were first introduced in JDK 5, had a significant impact on Java in two ways. For starters, it gave the language a new syntactical element. Second, it resulted in changes to several of the core API's classes and methods.Some programmers were hesitant to utilise generics because they constituted such a significant change to the language. With the release of JDK 6, however, generics can no longer be overlooked. Simply simply, you'll be using generics if you're working in Java SE 6. Generics, fortunately, are simple to use and bring considerable benefits to Java programmers.

The syntax, theory, and application of generics are all covered in this article. It also demonstrates how generics provide type safety in previously challenging instances.

Let’s get started!

What are Generics?

Generics refers to parameterized types at their most basic level. Parameterized types are useful because they let you to design classes, interfaces, and methods that have the kind of data they act on as a parameter. It is possible to design a single class, for example, that automatically works with several sorts of data using generics. Generic refers to a class, interface, or method that works with a parameterized type, as in generic class or generic method.

Significance of Generics

Generics can be used to create type-safe classes, interfaces, and methods that function with various types of data. Many algorithms are logically the same regardless of the type of data on which they are applied.

For example, whether a stack stores elements of type Integer, String, Object, or Thread, the mechanism that supports it is the same.

With generics, you may describe an algorithm once, regardless of the type of data, and then apply it to a wide range of data types without having to do any additional work. Generics give expressive capability to the language and significantly alter the way Java code is produced.

Also see, Duck Number in Java

Role of Generics in Collections

The Collections Framework is the one element of Java that has been significantly influenced by generics. The collection classes have always worked with any sort of object. Generics provide the benefit of allowing collection classes to be used with complete type safety. Generics enabled a significant improvement to an existing function in addition to introducing a powerful new language element. This is why generics are such a valuable addition to the Java language.

  • Before JDK 1.5, the Java collection framework was non-generic. It has been generic since version 1.5.
  • The new generic collection in Java allows you to have a collection with only one type of object. Because it is now type-safe, typecasting is no longer necessary at runtime.

The old non-generic example of creating a java collection.

ArrayList al=new ArrayList();

The new generic example of creating a java collection.

ArrayList<String> al=new ArrayList<String>();

The data type is specified in angular braces in the generic collection. Only specified types of objects are allowed in ArrayList now. A compile-time error occurs if you try to add another type of object.

Example of Generics

Let's start with a simple generic class example. Two classes are defined in the following programme. The first is Gen, which is a generic class, and the second is GenDemo, which is a demo that uses Gen.

// Example of a simple generic class.
// When an object of type Gen is created, type parameter T will be replaced by a real type.

class Gen<T> 
{
  public static void main(String args[]) 
  {
    T ob; //object of type T is declared
    // in the constructor pass a reference to an object of type T.
    Gen(T o) {
    ob = o;
    }
    // Return ob.
    T getob() {
    return ob;
    }
  }

 
  // print the type of T.
  void showType() {
    System.out.println("Type of T is " +
    ob.getClass().getName());
  }
}

 
// Demonstrate the generic class.
class GenDemo 
{
  public static void main(String args[]) 
  {
    // A Gen reference for Integers is created.
    
    Gen<Integer> iOb;
    
    // Create a Gen<Integer> object and assign its reference to iOb. Autoboxing is used  to encapsulate the value 48 within an Integer object.
    iOb = new Gen<Integer>(48);
    
    // Show the type of data used by iOb.
    iOb.showType();
    
    // Get the value in iOb. No cast is needed.
    int v = iOb.getob();
    System.out.println("value: " + v);
    System.out.println();
    
    // Create a Gen object for Strings.
    Gen<String> strOb = new Gen<String>("Generics Test");
   
     // Show the type of data used by strOb.
    strOb.showType();
    
    // Get the value of strOb that no cast is needed.
    String str = strOb.getob();
    System.out.println("value: " + str);
  }
}
You can also try this code with Online Java Compiler
Run Code

Output

Type of T is java.lang.Integer value: 88 
Type of T is java.lang.String value: Generics Test


Practice by yourself on java online compiler.

Frequently Asked Questions

What is a JIT compiler?

JIT (Just-In-Time) compiler: It is employed in order to enhance performance. JIT compiles bits of the bytecode with similar functionality at the same time, reducing the amount of time it takes to compile. The term "compiler" refers to a translator from a Java virtual machine's (JVM) instruction set to the instruction set of a specific CPU.

 

What is the platform?

The hardware or software environment in which a piece of software is run is referred to as a platform. Software-based and hardware-based platforms are the two sorts of platforms. The software-based platform is provided by Java.

 

What are the main differences between the Java platform and other platforms?

Other platforms may be hardware platforms or software-based platforms, but Java is a software-based platform.

Other hardware platforms can only contain the hardware components, whereas Java is processed on top of them.

 

What gives Java its 'write once and run anywhere' nature?

The bytecode is what you're looking for. The Java compiler translates Java applications into class files (byte code), which are a language that sits between source code and machine code. This bytecode is platform agnostic, meaning it can be run on any computer.

Conclusion

In this article, we have extensively discussed Generics in Java and their implementation in java.

We have learned what is java generics, why they are so important and their significance.

We have also learnt their role in collections.

Finally, we have seen example implementations.
 

We hope that this blog has helped you enhance your knowledge regarding Generics in Java and if you would like to learn more, check out our articles on Micro services in javastatic keyword in java. Do upvote our blog to help other ninjas grow.

Recommended Readings:

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more.!

Happy Reading!

Live masterclass