I have seen what is used for ArrayList<String> but I have no idea what it is. Who can explains without references to the wise reference orakl.

  • 2
    This is a way not to write a generic type if it is clearly visible from the type of object to which it is assigned. Example List<Integer> list = new ArrayList<>() but Object list = ArrayList<Integer>() - pavel
  • you did not quite understand me, I reformulated the question. - ishidex2
  • one
    Do not minus! When I was a newcomer I asked myself this question and could not find an answer! - Yuriy SPb ♦
  • one
    Triangular brackets .. - Peter Samokhin
  • one
    @SergeyGornostaev, I read it once, but either it wasn’t about generics, or I didn’t get to them, or they were incomprehensibly shown ... As a result, I understood them only when I had to finish the code at work were used. It was then that I had to understand and then love) - YuriySPb ♦

1 answer 1

Generics are used to write classes in which a part of the variables (or return values ​​of the methods) does not have a predetermined type and allows the use of different types for internal variables. For example, in the case of List can create lists of numbers, strings, or any other classes. The type transferred to the <> triangle brackets becomes the array type that is used inside the List implementation, and the get and set methods of the List implementation start giving away this type defined by the programmer.

Thus, generics allow you to write generalized implementations of classes with previously unknown types of internal variables. It is very comfortable and cool, even though at first it’s not clear)

Example:

 public class MyClass<T> { T foo; public T getFoo() { return foo; } public void setFoo(T foo) { this.foo = foo; } } 

Because in this class the type of the variable foo is not predefined, it can be of any type.

 //Ρ‚ΠΈΠΏ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Ρ‘Π½ Π½Π°ΠΌΠΈ ΠΊΠ°ΠΊ Integer, Π·Π½Π°Ρ‡ΠΈΡ‚ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π΅Π³ΠΎ (ΠΈ Π΅Π³ΠΎ ΠΏΠΎΡ‚ΠΎΠΌΠΊΠΎΠ²) ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΠΏΡ€ΠΈ пользовании этого экзСмпляра MyClass<Integer> integerFoo = new MyClass<>(); integerFoo.setFoo(5); //integerFoo.setFoo("5"); - ошибка компиляции, Ρ‚.ΠΊ. String Π½Π΅ Integer //Ρ‚ΠΈΠΏ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Ρ‘Π½ Π½Π°ΠΌΠΈ ΠΊΠ°ΠΊ String, Π·Π½Π°Ρ‡ΠΈΡ‚ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π΅Π³ΠΎ (ΠΈ Π΅Π³ΠΎ ΠΏΠΎΡ‚ΠΎΠΌΠΊΠΎΠ²) ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΠΏΡ€ΠΈ пользовании этого экзСмпляра MyClass<String> stringFoo = new MyClass<>(); stringFoo.setFoo("5"); //stringFoo.setFoo(5); - ошибка компиляции, Ρ‚.ΠΊ. Integer Π½Π΅ String 

If there were no generics, then, for example, using lists would be a pain β€” when retrieving an object from it, you would have to manually cast the desired type each time (or, worse, create one list implementation for each data type for a list ):

 //Π±Π΅Π· Π΄ΠΆΠ΅Π½Π΅Ρ€ΠΈΠΊΠΎΠ² List listOfObjects = new ArrayList(); listOfObjects.add("string"); //трСбуСтся ΠΊΠ°ΡΡ‚Π°Π½ΡƒΡ‚ΡŒ Ρ€ΡƒΠΊΠ°ΠΌΠΈ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ ΠΊ строкС String stringFromObjectsList = (String) listOfObjects.get(0); //с Π½ΠΈΠΌΠΈ List<String> listOfStrings = new ArrayList<>(); listOfStrings.add("string"); //НЕ трСбуСтся ΠΊΠ°ΡΡ‚ΠΎΠ²Π°Ρ‚ΡŒ Ρ€ΡƒΠΊΠ°ΠΌΠΈ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ ΠΊ строкС String stringFromStringList = listOfStrings.get(0); 
  • Thank you, can you give an example? - ishidex2
  • Straight out, just read a book about java everywhere they were used and the book itself was not explained - ishidex2
  • @Duoxx, example added to the answer) - YuriySPb ♦
  • Yes, a useful thing. - ishidex2