Java code notation

I stumbled upon this code this morning, and I have absolutely no idea what that means. Can someone explain to me what these are <T>? For example:

public class MyClass<T>
...

some bits of code then 

private Something<T> so;

private OtherThing<T> to;

private Class<T> c;

thank

+5
source share
5 answers

You are faced with "generics." They are very well explained in this guide .

In short, they let you specify the type that contains the storage class, for example, Listor Set. If you write Set<String>, you stated that this set should contain only Strings and will receive a compilation error if you try to put something else there:

Set<String> stringSet = new HashSet<String>();
stringSet.add("hello"); //ok.
stringSet.add(3);
      ^^^^^^^^^^^ //does not compile

, , , , :

public abstract class AbstClass<T extends Variable> {

, Variable, , Variable.

, , AbstClass, :

public void doThing(AbstClass<?> abstExtension) {

? - , " , AbstClass Variable".

+13

, , Generics. Java 1.5.

. , .

+5

, List Array. . ?

Generics . <T>, , some type. class MyList<T> { ... }, a list that holds some type.

MyList<Integer> listOfInts , MyList<String> listOfStrings , MyList<MyClass> listOfMyClass.

+1

, Java generics, . , " ", " " ​​ .. " - T, T - ", T. , Java , ++; Java , ( ) . ( ), Object. .

0
source

Since no one has mentioned this yet, there is a very detailed guide / FAQ / generic tutorial that can be found on the Angelika Langer website .

0
source

All Articles