Find the max of 3 numbers in Java with different data types

you can use this:

 Collections.max(Arrays.asList(1,2,3,4));

or create a function

public static int max(Integer... vals) {
    return Collections.max(Arrays.asList(vals)); 
}

If possible, use NumberUtils in Apache Commons Lang - plenty of great utilities there.

https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/math/NumberUtils.html#max(int[])

NumberUtils.max(int[])

Math.max only takes two arguments. If you want the maximum of three, use Math.max(MY_INT1, Math.max(MY_INT2, MY_DOUBLE2)).

Tags:

Java

Math

Max