Java Intermediate

0% completed

Previous
Next
Generic Methods

Generic methods are a powerful feature of Java that allow you to write a single method that can operate on different types of data while providing compile-time type safety. By defining a method with type parameters, you can create flexible and reusable code that works with any type specified at the time of method invocation. This lesson explains the syntax for defining and calling generic methods, accompanied by examples to illustrate their usage.

Syntax for Defining Generic Methods

A generic method is declared with type parameters before the return type. The general syntax is:

Image
  • <T>: Declares a type parameter named T. You can use any valid identifier (e.g., E, K, V).
  • ReturnType: The return type of the method, which can be the generic type T or any other type.
  • ParameterType parameter: The method parameters may include the generic type T to accept arguments of various types.

Syntax for Calling Generic Methods

When you call a generic method, you do not need to specify the type parameter explicitly because of type inference. The compiler infers the type from the context.

  • Example Call:

    methodName(argument);
  • Explicit Type Specification (optional):

    <Type>methodName(argument);

Examples

Example 1: Generic Method to Print an Array

This example demonstrates a generic method that prints the elements of an array. The method works with any type of array, such as integers, strings, or custom objects.

Java
Java

. . . .

Example Explanation:

  • Generic Method Definition:
    • The method printArray is declared with a type parameter <T>, allowing it to work with any array type.
  • Iteration:
    • A for-each loop iterates over the array, printing each element.
  • Type Inference:
    • When calling printArray, the compiler automatically determines the type (Integer or String) from the provided array.

Example 2: Generic Method to Swap Two Elements in an Array

This example demonstrates a generic method that swaps two elements in an array. The method can be used with arrays of any type.

Java
Java

. . . .

Example Explanation:

  • Generic Method Definition:
    • The method swap is declared with <T> so it works with any array type.
  • Swapping Logic:
    • It swaps the elements at positions i and j using a temporary variable.
  • Usage:
    • In the main() method, a String array is defined and the swap method is called to swap elements at indices 1 and 2.
  • Outcome:
    • The array elements are rearranged as expected, demonstrating the method's versatility across types.

Generic methods enable you to write flexible, reusable code that works with various data types while ensuring type safety at compile time. With generic methods, you can avoid repetitive code and eliminate the need for casting. By mastering generic methods, you can enhance code reusability and robustness in your Java applications.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next