Home > Java > javaTutorial > Use Java's Math.max() function to compare the size of two values ​​and return the larger value

Use Java's Math.max() function to compare the size of two values ​​and return the larger value

王林
Release: 2023-07-24 18:37:15
Original
2900 people have browsed it

Use java's Math.max() function to compare the size of two values ​​and return the larger value

In java, we often need to compare the size of two values ​​and return the larger value. Fortunately, Java has a very convenient built-in function Math.max() to help us accomplish this task.

Math.max() function accepts two parameters, then compares their sizes and returns the larger value. Below is a simple code example showing how to use the Math.max() function.

public class MaxValue {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int maxValue = Math.max(a, b);
        System.out.println("较大的值是:" + maxValue);
    }
}
Copy after login

In this example, we define two integer variables a and b and assign them the values ​​​​of 10 and 20 respectively. We then called the Math.max() function to compare the sizes of a and b and assigned the larger returned value to the variable maxValue. Finally, we print the larger value using the System.out.println() function.

When you run this code, the output will be "The larger value is: 20". Because 20 is larger than 10, the larger value returned is 20.

In addition to integers, the Math.max() function can also be used to compare floating point numbers, long values, etc. We just need to pass the corresponding parameters to the Math.max() function.

public class MaxValue {
    public static void main(String[] args) {
        double a = 3.14;
        double b = 2.18;
        double maxValue = Math.max(a, b);
        System.out.println("较大的值是:" + maxValue);
    }
}
Copy after login

In this example, we define two double type variables a and b and assign them the values ​​​​of 3.14 and 2.18 respectively. We then called the Math.max() function to compare the sizes of a and b and assigned the larger returned value to the variable maxValue. Finally, we print the larger value using the System.out.println() function.

When you run this code, the output will be "The larger value is: 3.14". Because 3.14 is larger than 2.18, the larger value returned is 3.14.

In short, using the Math.max() function can very conveniently compare the size of two values ​​​​and return the larger value. Whether it is an integer or a floating point number, the Math.max() function can do the job and help us simplify the code and improve efficiency. I hope this article can help you better understand and use the Math.max() function.

The above is the detailed content of Use Java's Math.max() function to compare the size of two values ​​and return the larger value. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template