What does \t mean in Java? This means to insert a new tab at this specific point in the text. In the below example, "\t" is used inside the println statement. It is similar to pressing the tab on our keyboard.
What Does Angle Bracket Mean? The angle bracket (< or >), which is also called an “inequality sign” for its use in mathematics, is a kind of sideways caret that can be used to include tags or pieces of code. This ASCII set of characters is common in web design and other types of coding projects.
AnyType is actually used in java7+. It can be said that its usage is same as that of E or T or something. As the name suggests it can any type of generic data. for example, when I implemented my generic linked list in Java, I used.
The angled brackets represent an inner product. The best known one is the scalar product or the dot product. If u=(u1,u2,u3) and v=(v1,v2,v3), then the dot product is given by ⟨u,v⟩=u1v1+u2v2+u3v3.
<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:
Pool<String>
would have an interface PoolFactory<String>
with a createObject
method that returns String
s.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.
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 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 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:
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.
<>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With