.

Java Tutorial: Generics in Java

Generics

  • The generic classNameand methods allow coder to represent with individual method and classNamedeclaration
  • It supports compile time error checking possible
  • When generic method or / and classNameis implemented, any type of value can be passed for generic component

Generic Methods

  • This method permits variety of data types. Based on the value type, the generic method works accordingly
  • It has angular brackets <> to represent type parameter
  • it comes before the return_typ of a method

Example

The underlying program displays how a generic method is achieved for generalisation

generics img1

Generic Classes

  • Its classNamename is trailed by type
  • It has angular brackets <> to represent type parameter
  • It includes minimum 1 or more type variables
  • All those types are delimited by comma (,)

Example

The underlying program displays how a generic classNameis achieved for generalisation

generics img2
.