What does <T> (angle brackets) mean in Java?
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
?
<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 Integer
s." 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 interfacePoolFactory<String>
with acreateObject
method that returnsString
s. - Internally, the
Pool<String>
would contain anArrayList
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.
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).
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:
- Reducing the number of casts in your program, thus reducing the number of potential bugs in your program.
- Improving code clarity
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.