Java Generic ReturnTypes

Article by: Manish Methani

Last Updated: October 22, 2021 at 2:04pm IST
44 sec read

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)
  {
    System.out.println(max(10,23,45));
    System.out.println(max('c','d','z'));
  }
  
  public static > 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 :-

45
z

Learn to Build 5 Apps Without Coding using FlutterFlow

  • Lifetime access to all the 5 FlutterFlow Courses
  • Complete FlutterFlow training
  • All future course updates
  • Access via Codzify mobile app
  • 24h 42m of Learning Content
  • 5 FlutterFlow at Codzify just ₹1500 ($17.44)
  • Access to Dating App Course, Grocery App Course, Courses App Course, FlutterFlow AI Agents Course, Doctor Appointment Booking Supabase Course.
  • Clone Ready-Made Courses App instantly in FlutterFlow.
  • 30-Day 100% Money-Back Guarantee.

Learn More
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: [email protected]