In the texts of Java programs, when defining and using classes and interfaces, we often find constructions of the type class SomeClass<T> {...} or SomeClass<String> myObj = new SomeClass<String> or something else like that. What is it and why?

3 answers 3

These are Generic types, which will then allow to determine the type.

 public class GenericsType<T> { private T t; public T get(){ return this.t; } public void set(T t1){ this.t=t1; } public static void main(String args[]){ GenericsType<String> type = new GenericsType<>(); type.set("Pankaj"); //valid GenericsType type1 = new GenericsType(); //raw type type1.set("Pankaj"); //valid type1.set(10); //valid and autoboxing support } } 

Another example:

 public interface MyInyerface<E> { void print(E e); } public class MyClass implements MyInyerface<String> { @Override public void print(String s) { } class MyClassInteger implements MyInyerface<Integer>{ @Override public void print(Integer integer) { } } } 

By specifying the data type, methods can only accept this type. This is necessary if you need only a certain type of input, and if you passed a different type, you will receive an error not during compilation, but during the analysis of the code.

More details can be read here and here and here again

  • Please correct the Generic types, because generics may not only be at the interface - Andrew Bystrov
  • You showed <T>, but what about the top-starter question about <E>? - Kromster
  • @Kromster and what's the difference <T> or <E> )? - Senior Pomidor
  • I like with Java unfamiliar. Tell me, is there a difference between <T> or <E>? - Kromster
  • @Kromster: From the point of view of using none, it's just the name of the type parameter. All the same, that void f(int x) and void f(int y) differ only in the name of the parameter. - VladD

What you are talking about is the definition and / or use of so-called “generic” or “parameterizable” types (Generalized types, Generics). This is a mechanism that allows you to define a class or interface whose instances can work with data of different types , and the specific type of this data is indicated when using such a generic type, for example. when defining a variable or parameter of a method. A typical example of the use of such classes -

  ArrayList<String> stringList = new ArrayList<String>(); // список строк ArrayList<Integer> intList = new ArrayList<Integer>(); // список чисел stringList.add("123"); // OK stringList.add(123); // Ошибка! intList.add("123"); // Ошибка! intList.add(123); // OK 

When defining this type, in its header and in its body, an arbitrary identifier is used to indicate the type of parameter — the type with which a specific instance of the class or interface will work (in the body it is used as if it is already a known type to designate types etc.):

 class SomeClass < T > { ... private T value; public T getValue() { ... } } 

When using this type, a specific type is specified as a parameter with which an instance of this generic type should work:

 SomeClass< Integer > intInstance = new SomeClass< Integer >(); SomeClass< ArrayList > listInstance = new SomeClass< ArrayList>(); .... int i = intInstance.getValue(); // OK int j = listInstance.getValue(); // Error!!! ArrayList list = listInstance.getValue(); // OK 
Using generic types gives a lot of advantages:

  1. Less scribbling (the generalized class needs to be described only once);
  2. Higher reliability (you can not stick a number into the list, which should contain lines);
  3. Higher readability (it’s immediately obvious that ArrayList <String> is for strings)

This is just a general idea, there are still a number of nuances (type restrictions, patterns), but it’s better to read about these details in books.

    This means a parameterized type. Specifically, the <> operator (by the way, is called a diamond, or a diamond operator), who tells the compiler what type should come to him - he will reject all other types at the stage of semantic verification of the code.

    Only primitive shells or classes can be placed in this operator (<>) (note. List<int> list = new ArrayList<>(); not roll).

    About <E> - to be honest, I don’t remember what it means (this point is better to check with the links in the answer to @Senior Automator).

    You can also check if the type is inherited from another type <? extends SomeClass> <? extends SomeClass> , etc.

    • one
      <E> is not at all the same as <T> These are two different identifiers! - Pavel Mayorov
    • @PavelMayorov I said that I don’t remember exactly, I indicated in the answer. - Silento
    • one
      Well, correct the answer. - Pavel Mayorov