What does <T> (angle brackets) mean in Java?

JavaGenerics

Java Problem Overview


I am currently studying Java and have recently been stumped by angle brackets(<>). What exactly do they mean?

public class Pool<T>{
    public interface PoolFactory<T>{
        public T createObject();
    }
    this.freeObjects = new ArrayList<T>(maxsize)
}

What does the <T> mean? Does it means that I can create an object of type T?

Java Solutions


Solution 1 - Java

<T> is a generic and can usually be read as "of type T". It depends on the type to the left of the <> what it actually means.

I don't know what a Pool or PoolFactory is, but you also mention ArrayList<T>, which is a standard Java class, so I'll talk to that.

Usually, you won't see "T" in there, you'll see another type. So if you see ArrayList<Integer> for example, that means "An ArrayList of Integers." Many classes use generics to constrain the type of the elements in a container, for example. Another example is HashMap<String, Integer>, which means "a map with String keys and Integer values."

Your Pool example is a bit different, because there you are defining a class. So in that case, you are creating a class that somebody else could instantiate with a particular type in place of T. For example, I could create an object of type Pool<String> using your class definition. That would mean two things:

  • My Pool<String> would have an interface PoolFactory<String> with a createObject method that returns Strings.
  • Internally, the Pool<String> would contain an ArrayList of Strings.

This is great news, because at another time, I could come along and create a Pool<Integer> which would use the same code, but have Integer wherever you see T in the source.

Solution 2 - Java

It's really simple. It's a new feature introduced in J2SE 5. Specifying angular brackets after the class name means you are creating a temporary data type which can hold any type of data.

Example:

class A<T>{
	T obj;
	void add(T obj){
        this.obj=obj;
    }
	T get(){
        return obj;
    }
}
public class generics {
	static<E> void print(E[] elements){
		for(E element:elements){
			System.out.println(element);
		}
	}

	public static void main(String[] args) {
		A<String> obj=new A<String>();
		A<Integer> obj1=new A<Integer>();
		obj.add("hello");
		obj1.add(6);
		System.out.println(obj.get());
		System.out.println(obj1.get());
	
		Integer[] arr={1,3,5,7};
		print(arr);
	}
}

Instead of <T>, you can actually write anything and it will work the same way. Try writing <ABC> in place of <T>.

This is just for convenience:

  • <T> is referred to as any type
  • <E> as element type
  • <N> as number type
  • <V> as value
  • <K> as key

But you can name it anything you want, it doesn't really matter.

Moreover, Integer, String, Boolean etc are wrapper classes of Java which help in checking of types during compilation. For example, in the above code, obj is of type String, so you can't add any other type to it (try obj.add(1), it will cast an error). Similarly, obj1 is of the Integer type, you can't add any other type to it (try obj1.add("hello"), error will be there).

Solution 3 - Java

It is related to generics in java. If I mentioned ArrayList<String> that means I can add only String type object to that ArrayList.

The two major benefits of generics in Java are:

  1. Reducing the number of casts in your program, thus reducing the number of potential bugs in your program.
  2. Improving code clarity

Solution 4 - Java

is called a generic type. You can instantiate an object Pool like this:

PoolFactory<Integer> pool = new Pool<Integer>();

The generic parameter can only be a reference type. So you can't use primitive types like int or double or char or other primitive types.

Solution 5 - Java

<> is used to indicate generics in Java.

T is a type parameter in this example. And no: instantiating is one of the few things that you can't do with T.

Apart from the tutorial linked above Angelika Langers Generics FAQ is a great resource on the topic.

Solution 6 - Java

Generic classes are a type of class that takes in a data type as a parameter when it's created. This type parameter is specified using angle brackets and the type can change each time a new instance of the class is instantiated. For instance, let's create an ArrayList for Employee objects and another for Company objects

ArrayList<Employee> employees = new ArrayList<Employee>();
ArrayList<Company> companies = new ArrayList<Company>();

You'll notice that we're using the same ArrayList class to create both lists and we pass in the Employee or Company type using angle brackets. Having one generic class be able to handle multiple types of data cuts down on having a lot of classes that perform similar tasks. Generics also help to cut down on bugs by giving everything a strong type which helps the compiler point out errors. By specifying a type for ArrayList, the compiler will throw an error if you try to add an Employee to the Company list or vice versa.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLaughyView Question on Stackoverflow
Solution 1 - JavamgiucaView Answer on Stackoverflow
Solution 2 - JavaRuturaj MohantyView Answer on Stackoverflow
Solution 3 - Javauser831722View Answer on Stackoverflow
Solution 4 - JavaRafiekView Answer on Stackoverflow
Solution 5 - JavaJoachim SauerView Answer on Stackoverflow
Solution 6 - JavaAdiiiView Answer on Stackoverflow