Java Generics - Java

What is Java Generics?

it might be nice if we could write a single type technique that might sort the factors in an Integer array, a String array, or an array of any type that helps ordering.

Java universal techniques and ordinary lessons permit programmers to specify, with a single method assertion, a hard and fast of associated strategies, or with a single elegance statement, a fixed of related types, respectively.

Generics additionally offer assemble-time type safety that lets in programmers to trap invalid sorts at bring together time.

the use of Java usual idea, we'd write a well-known approach for sorting an array of items, then invoke the commonplace technique with Integer arrays, Double arrays, String arrays and so on, to kind the array elements.

Generic techniques

You may write a single generic method declaration that can be called with arguments of various kinds. Primarily based on the varieties of the arguments passed to the generic technique, the compiler handles every technique name appropriately. Following are the guidelines to outline customary strategies −

All generic technique declarations have a kind parameter segment delimited via attitude brackets (< and >) that precedes the technique's return type ( < E > in the next instance).

Each type parameter section contains one or more type parameters separated by using commas. A type parameter, also known as a kind variable, is an identifier that specifies a popular type name.

The kind parameters can be used to claim the go back type and act as placeholders for the styles of the arguments handed to the conventional method, which are referred to as real kind arguments.

A widespread method's frame is said like that of some other method. be aware that type parameters can represent best reference kinds, no longer primitive sorts (like int, double and char).


Following example illustrates how we are able to print an array of different kind the usage of a single well-known technique –

This will produce the following result −


Bounded Type Parameters

There may be times when you'll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for.
To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound.


Following example illustrates how extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces). This example is Generic method to return the largest of three Comparable objects –

This will produce the following result −


Generic Classes

A generic class declaration looks like a non-generic class declaration, except that the class name is followed by a type parameter section.
As with generic methods, the type parameter section of a generic class can have one or more type parameters separated by commas. These classes are known as parameterized classes or parameterized types because they accept one or more parameters.


Following example illustrates how we can define a generic class –

This will produce the following result −


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Java Topics