What does <t> (angle brackets) mean in Java?
I am currently learning Java and have recently been broken 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 <T> mean? Does this mean that I can create an object of type T ?
<T> is generic and can usually be read as "type T". It depends on the type to the left of <> what this actually means.
I donβt know what Pool or PoolFactory , but you also mention ArrayList<T> , which is a standard Java class, so I will talk to that.
Usually you will not see a βTβ there, you will see a different type. Therefore, if you see ArrayList<Integer> , for example, it means "An ArrayList of Integer s." Many classes use generics to limit the type of elements in a container, for example. Another example is HashMap<String, Integer> , which means "map with String keys and Integer values."
The example of your pool is a little different, because there you define the class. So in this case you are creating a class that someone else can create using a specific type instead of T. For example, I could create an object of type Pool<String> using the definition of your class. This would mean two things:
- My
Pool<String>will have aPoolFactory<String>interface with acreateObjectmethod that returnsStrings. - Inside the
Pool<String>will contain anArrayListstrings.
This is great news, because at another time I could come and create a Pool<Integer> that uses the same code, but has Integer wherever you see T in the source.
This is due to generics in java. If I mentioned ArrayList<String> , this means that I can only add an object of type String to this ArrayList.
Two main benefits of generics in Java:
- Reducing the number of throws in your program, which reduces the number of potential errors in your program.
- Improved code clarity
It is really easy. This is a new feature introduced in J2SE 5 . Specifying angular brackets after the class name means that you are creating a temporary data type that can store data of any type.
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 write anything and it will work the same. Try writing <ABC> instead of <T> .
This is just for convenience:
<T>is called any type<E>as element type<N>as the type of number<V>as value<K>as a key
But you can call it anything, it does not really matter.
In addition, Integer , String , Boolean , etc. are Java wrapper classes that help with type checking at compile time. For example, in the above code, obj is of type String , so you cannot add any other type to it (try obj.add(1) , it will make an error). Similarly, obj1 is of type Integer , you cannot add any other type to it (try obj1.add("hello") , there will be an error).
called the generic type. You can create an object pool object as follows:
PoolFactory<Integer> pool = new Pool<Integer>(); A generic parameter can only be a reference type. Thus, you cannot use primitive types such as int or double or char or other primitive types.
<> used to specify generics in Java.
T is the type parameter in this example. And no: instantiating is one of the few things you can't do with T
In addition to the tutorial linked above, Angelika Langers Generics FAQs are an excellent resource on this topic.
Generic classes are a type of class that takes a data type as a parameter when it is created. This type parameter is specified using angle brackets, and the type can change each time an instance of a new instance of the class is created. For example, let me create ArrayList objects for Employee and another for Company objects.
ArrayList<Employee> employees = new ArrayList<Employee>(); ArrayList<Company> companies = new ArrayList<Company>(); You will notice that we use the same ArrayList class to create both lists, and we pass the type Employee or Company using angle brackets. Having one universal class allows you to process several types of data, reducing the number of classes that perform similar tasks. Generics also helps reduce errors by providing all the powerful types that help the compiler point out errors. By specifying a type for ArrayList, the compiler throws an error if you try to add Employee to the Company list or vice versa.