Generics in Java

Generics Many algorithms work in a similar way irrespective of the data types on which they are applied on All Stacks work in a similar way irrespective of the type of object they hold Generics help to write algorithms independent of a data type These algorithms can be applied to a variety of data types The data type is parameterized A class, interface or a method that works on a parameterized type is called a generic

In above code, T represents a data type. While declaring an object of List, the programmer can decide the actual type for T.

  Just like String objects are stored and retrieved in the above example, the List class can be used for any kind of object A generic can restrict the type of object that can be used as the parameter A generic List class that performs arithmetic operations on the… Read more“Generics in Java”