0
what does <identifier> means in java
I had seen an object deceration like this :- Abhay<human> = new Abhay<human>(); what does <human> means is above example plzzzz explain me plzzz
2 Antworten
+ 1
generic type is like universal type of used variable or field.
class Store<Type> {
Type type;
Store( Type type) {
this.type = type;
}
}
public class Program {
public static void main(String[] args) {
Store<String> s1 = new Store<>("abcd");
Store<Integer> s2 = new Store<>(1234);
Store<Boolean> s3 = new Store<>(true);
}
}
+ 1
The <T> are for generic types. LinkedList<String> means that it's a LinkedList that will hold String objects, LinkedList<Human> means that it's a LinkedList that will hold Human objects.
If I make my custom object like EpicNewObject, I can make a LinkedList<EpicNewObject> that will hold EpicNewObjects.
The same for other objects that use the <T> type, it's to hold objects/define types in it's class without hardcoding it, as to allow any object type created.
I recommend looking up generic types, as I don't think I can explain it that well past that.