Generics in Java

MD Aminul Islam Oct 12, 2023
  1. How the Generics Ensures the Type in Java
  2. An Example With the Generic Method in Java
  3. An Example With the Generic Class in Java
Generics in Java

When you declare a general type of object for a class, it doesn’t contain any feature for providing the safety of the type. But you can easily add this safety feature to your object using the generics.

There are two kinds of generics available in Java, the first one is the generic method, and the second one is the generic class.

The generic method will allow you to use a method in a more general way as it contains the type parameters, and the actual type cites these parameters.

On the other hand, the generic class is mostly like the non-generic class.

But the only difference is that it has a section for the type parameters. Besides, you can use multiple parameters.

In this article, we are going to discuss the generics in Java. Also, we will discuss the topic using examples that illustrate the generics and explain these codes part by part.

How the Generics Ensures the Type in Java

Let us discuss how a Generics class ensures type in your program. When you create a program and tries to compile it, the Generic class checks whether any misuse of the type occurred during the compilation.

If any incompatible type is found, it will show an error, and the compilation will fail.

An Example With the Generic Method in Java

In our example below, we will see the generic method’s implementation. The code will be like this:

class GenericMethod {
  // Declaring a generic method
  static <T> void MyDisplay(T data) {
    System.out.println("Your input is: " + data);
  }

  // Our controlling method ( Main method )
  public static void main(String[] args) {
    // Calling a generic method with an Integer argument
    MyDisplay(17);

    // Calling a generic method with a String argument
    MyDisplay("This is a string");

    // Calling a generic method with a double argument
    MyDisplay(16.0);
  }
}

We already commented on the purpose of each line for our example above. Now when you run the above example of code, you will get an output like the one below.

Your input is: 17
Your input is: This is a string
Your input is: 16.0

An Example With the Generic Class in Java

In our example below, we will see the generic class’s implementation. Here, the name of our generic class is GenericClass with the type <T>.

The code will be like this:

class GenericClass<T> {
  // Declaring a "T" type object
  T MyObj;
  GenericClass(T MyObj) {
    this.MyObj = MyObj;
  } // Creating a constructor
  public T getObject() {
    return this.MyObj;
  }
}

// Our controlling method ( Main method )
class Main {
  public static void main(String[] args) {
    // Declaring an instance of Integer type
    GenericClass<Integer> IntObj = new GenericClass<Integer>(19);
    System.out.println("The number is: " + IntObj.getObject());

    // Declaring an instance of String type
    GenericClass<String> StrObj = new GenericClass<String>("This is a string.");
    System.out.println(StrObj.getObject());
  }
}

We already commented on the purpose of each line for our example above. Now when you run the above example of code, you will get an output like the one below.

The number is: 19
This is a string.

An important note here, as a parameter type, we can’t use primitives such as int, char, float, or double.

Please note that the code examples shared here are in Java, and you must install Java on your environment if your system doesn’t contain Java.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Java Generics