position: fixed; top: auto !important; margin-left: 112px;

What is Generic ReturnType ?

In the previous tutorial we studied Generic Methods. How generic Methods handle different Types. Generic Methods also has a feature of handling return types.

Suppose you had written one generic method which finds maximum from three numbers. So you have passed an integer number. Now in future, you have to find the maximum of three characters. So do you want to write different method for that? No of course. You can use Generic return type instead of void.

Example :-

This example demonstartes how Generic method handles Generic Return types .

public class GenericReturnTypesDemo {
  public static void main(String[] args)
  public static <T extends Comparable<T>> T max(T a, T b, T c)
    T max = a;
    if(b.compareTo(a) > 0)
            max = b;
    if(c.compareTo(max) > 0)
      max = c;
    return max;

Output :-


Previous Next Article


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Copyright 2019. All rights reserved.