Advertisement
Kvarz

Generic return type

Oct 28th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.40 KB | None | 0 0
  1.  
  2. public class Execution {
  3.  
  4.     public static void main(String[] args) {
  5.         System.out.println(max(2, 5, 6, 7));
  6.         System.out.println(max("apples", "lost", "chicken", "tots"));
  7.        
  8.     }
  9.     public static <T extends Comparable<T>> T max(T a, T b, T c, T d){
  10.         T m = a;
  11.         if(b.compareTo(a)>0){
  12.             m = b;
  13.         }
  14.         if(c.compareTo(b)>0){
  15.             m = c;
  16.         }
  17.         if(d.compareTo(c)>0){
  18.             m = d;
  19.         }
  20.         return m;
  21.     }
  22.    
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement