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
orPoolFactory
is, but you also mentionArrayList<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 "AnArrayList
ofInteger
s." Many classes use generics to constrain the type of the elements in a container, for example. Another example isHashMap<String, Integer>
, which means "a map withString
keys andInteger
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:Pool<String>
would have an interfacePoolFactory<String>
with acreateObject
method that returnsString
s.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 haveInteger
wherever you seeT
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:
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 keyBut 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 typeString
, so you can't add any other type to it (tryobj.add(1)
, it will cast an error). Similarly,obj1
is of theInteger
type, you can't add any other type to it (tryobj1.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:
<>
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 withT
.Apart from the tutorial linked above Angelika Langers Generics FAQ is a great resource on the topic.
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
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.
is called a generic type. You can instantiate an object Pool like this:
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.