Java

Math.max Method in Java

The java max() method returns the maximum of two numbers. The numbers compared should be of the same type. They can be of int/int, long/long, float/float, or double/double. For this, the Java.lang.math class has to be imported (but not obligatory).

Comparing int/int

The syntax is:

static int max(int a, int b)

Example program:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            int x = 2;  
            int y = 7;
            int z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is 7. The class Math is in the java.lang.* package. In the max method, either x or y can be typed first. The following program outputs 7, between -7 and +7:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            int x = -7;  
            int y = +7;
            int z = Math.max(y, x);
            System.out.println(z);
        }  
    }

The output is 7. The following program outputs -3, between -7 and -3:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            int x = -7;  
            int y = -3;
            int z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is -3.

If the two numbers are the same, the result is the same value, as illustrated in the following program:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            int x = 5;  
            int y = 5;
            int z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is 5.

Comparing long/long

A long data type is an integer type. It should be used when the ranges of integers wanted are more than that for the int.

The syntax is:

static long max​(long a, long b)

Example program:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            long x = 200000000;  
            long y = 700000000;
            long z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is 700000000. The class Math is in the java.lang.* package. In the max() method, either x or y can be typed first. The following program outputs 700000000, between -700000000 and +700000000:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            long x = -700000000;  
            long y = +700000000;
            long z = Math.max(y, x);
            System.out.println(z);
        }  
    }

The output is 700000000. The following program outputs -300000000, between -700000000 and -300000000:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            long x = -700000000;  
            long y = -300000000;
            long z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is -300000000.

If the two numbers are the same, the result is the same value, as illustrated in the following program:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            long x = 500000000;  
            long y = 500000000;
            long z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is 500000000.

Comparing float/float

A float number is a number with a decimal (fractional) part.

The syntax is:

static float max​(float a, float b)

Example program:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            float x = 2.5f;  
            float y = 7.5f;
            float z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is 7.5. The class Math is in the java.lang.* package. In the max method either x or y can be typed first. The following program outputs 7.5, between -7.5 and +7.5:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            float x = -7.5f;  
            float y = +7.5f;
            float z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is 7.5. The following program outputs -3.5, between -7.5 and -3.5:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            float x = -7.5f;  
            float y = -3.5f;
            float z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is -3.5 .

If the two numbers are the same, the result is the same value, as illustrated in the following program:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            float x = -2.5f;  
            float y = -2.5f;
            float z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is -2.5.

Comparing double/double

A double number is similar to a float number, but it is more precise and does not need the suffix f.

The syntax is:

static double max​(double a, double b)

Example program:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            double x = 2.5;  
            double y = 7.5;
            double z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is 7.5. The class Math is in the java.lang.* package. In the max() method, either x or y can be typed first. The following program outputs 7.5, between -7.5 and +7.5:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            double x = -7.5;  
            double y = +7.5;
            double z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is 7.5 for +7.5. The following program outputs -3.5, between -7.5 and -3.5:

   import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            double x = -7.5;  
            double y = -3.5;
            double z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is -3.5 .

If the two numbers are the same, the result is the same value, as illustrated in the following program:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            double x = 2.5;  
            double y = 2.5;
            double z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is 2.5.

Numbers of Different Types

Long and int can be compared. However, the result of any other combination is not reliable, especially if the numbers are close. The following comparison between int and long takes place without any problem, and the result is valid:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            int x = 15;  
            long y = 14;
            long z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is 15. In this kind of comparison, the result (return) should be a long type.

The following program compares an int and a double without even a warning message:

    import java.lang.*;

    class TheClass {

        public static void main(String[] args) {  
            int x = 8;  
            double y = 2.5;
            double z = Math.max(x, y);
            System.out.println(z);
        }  
    }

The output is 8.0. The int 8 was converted to a float 8.0. Though no warning message was given, it is still not advisable to mix types, except for int and long. Types should not be mixed, for comparison, because a float or a double is hardly represented exactly.

Conclusion

Java Math.max() static method compares numbers of the same type; and returns the bigger number. If the numbers are the same, the same number is returned. Types should not be mixed in comparison, except for int and long.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.