What Does <T> Mean In Java?

What Does <T> Mean In Java?

·

3 min read

Play this article

<T> is a conventional letter that stands for "Type". <T> refers to the concept of Generics in Java. You can use any letter, but <T> is widely preferred.

What Does Generic Mean?

Generic is a way to parameterize a class, method, or interface.

Let's look at an example:

package Generics;

class House<T>{

    T doorNumber;

    public House(T doorNumber) {
        this.doorNumber = doorNumber;
    }

    public void print(){
        System.out.println("Your house number is: " + this.doorNumber);
    }
}

In the code above:

  1. You have a class called House that can accept an unspecified Object type.

  2. Then, you have a field called doorNumber, which can accept any Object type.

  3. Ultimately, you declare a parameterized constructor and print out the door number.

The user can decide whether this door number is an Integer, a String, a Float, etc.

You can only use Objects. Primitives are not supported because Generics are intended to be a feature used at compile-time. When you use Generics, the T goes away in place of anything that can extend the Object class, and primitives don't have this trait.

Let's say that you want the doorNumber to be a String.

public class GenericsExample {
    public static void main(String[] args) {
        House<String> mainHouse = new House<>("14a");
        mainHouse.print();      
    }
}

The outcome would be:

Your house number is: 14a

You replace the <T> with "String" and enter the house number in the constructor.

You can use multiple types if, for example, you want the class to accept more than one Object. You can add another letter to say that we want the class to accept another Generic.

package Generics;

class House<T, V>{

    T doorNumber;
    V streetName;

    public House(T doorNumber, V streetName) {
        this.doorNumber = doorNumber;
        this.streetName = streetName;
    }

    public void print(){
        System.out.println("You live at: " + this.doorNumber + " " + this.streetName);
    }
}

public class GenericsExample {
    public static void main(String[] args) {
        House<Integer, String> mainHouse = new House<>(14, "Tellson Avenue");
        mainHouse.print();

    }
}

The outcome would be:

You live at: 14 Tellson Avenue

So far you've seen examples of a Generic used at the class level. But you can also have Generic methods and interfaces.

Related: What Is The Meaning of "?" in Java?

Generic Method

package Generics;

class House{

    public <T> void print(T doorNumber){
        System.out.println("You live at house number: " + doorNumber);
    }

}

public class GenericsExample {
    public static void main(String[] args) {
        House mainHouse = new House();
        mainHouse.<Integer>print(14);

    }
}

The method accepts any Object, and it prints out the doorNumber which will be of any Object type. In this case, you want the method to accept an Integer.

The outcome would be:

You live at house number: 14

Generic Interface

First, create an interface.

package Generics;

interface Property<T>{
    void hasBalcony(T balcony);
}

Then, implement the interface.

package Generics;

public class House implements Property<String> {

    @Override
    public void hasBalcony(String balcony) {
        System.out.println("Is there a balcony in the room? " + balcony);
    }

    public static void main(String[] args) {
        House mainHouse = new House();
        mainHouse.hasBalcony("YES");
    }

}

The outcome is:

Is there a balcony in the room? YES

Conclusion:

This article has shown you the benefits of using Generics:

  1. Better compile-time checking: if you use an Object type different from the one you specified, the compiler will tell you.

  2. Reusability: you can use a class, method or interface multiple times because you decide which Object type to apply based on what you're trying to achieve.

  3. It's great for data structures and algorithms: ArrayList and Hashmap are a couple of examples that make use of Generic.

I hope you've found this helpful.

Until next time! 🙋🏾‍♀️

FURTHER READING:

Did you find this article valuable?

Support Maddy by becoming a sponsor. Any amount is appreciated!