Table of contents
1.
Introduction
2.
Syntax
3.
Parameters
4.
Return Value
5.
Example
5.1.
Example 1
5.2.
Example 2
5.3.
Example 3
5.4.
Example 4
6.
Frequently Asked Questions
6.1.
Can Math.min() be used with more than two numbers?
6.2.
Does Math.min() work with negative numbers?
6.3.
Can Math.min() compare numbers of different data types?
7.
Conclusion
Last Updated: Nov 12, 2024
Easy

Java Math min() method

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Math is an important part of our everyday lives. It impacts our daily lives in ways we might not notice. Just like our daily lives, it is an important part of programming also because it helps us solve problems, think logically, and make decisions that make our code better and more effective. In programming, a common task is finding the smallest number in a group of numbers. The Math.min function in Java makes this task easy. It's a simple but powerful tool that quickly finds the smallest of two numbers, whether they are integers, doubles, or other types of numbers. This function is very important for any programmer who needs a fast and reliable way to get the minimum value in Java. 

Java Math min() method

In this article, we'll discuss the Math.min() method in Java, its syntax, parameters, return value, and examples to help you understand how to use it effectively in your code.

Syntax

The syntax for using the Math.min() method in Java is : 

Math.min(a, b)


In this syntax, `a` & `b` are the two numbers you want to compare. The method returns the minimum value between these two numbers.

Parameters

The Math.min() method takes two parameters:

1. `a`: The first number to compare.
 

2. `b`: The second number to compare.


Both `a` and `b` can be of any numeric data type, such as int, long, float, or double. If the parameters have different data types, the method automatically performs necessary type conversions.

Return Value

The Math.min() method returns the minimum value between the two numbers passed as parameters. The return value has the same data type as the parameters. If the parameters have different data types, the method returns the minimum value in the larger data type.

For example, if you pass an int & a double to Math.min(), the method will return a double value.

Example

Let's look at a few examples to understand how to use the Math.min() method in Java:

Example 1

public class MathMinExample {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int minValue = Math.min(a, b);
        System.out.println("The minimum value is: " + minValue);
    }
}
You can also try this code with Online Java Compiler
Run Code


In this example, we have two integer variables, `a` and `b`, with values 10 and 20, respectively. We use the Math.min() method to find the minimum value between `a` and `b` and store the result in the `minValue` variable. Finally, we print the minimum value using System.out.println().


Output:

The minimum value is: 10


As expected, the Math.min() method returns 10, which is the minimum value between 10 & 20.

Example 2

public class MathMinExample1 {
    public static void main(String[] args) {
        double x = 3.14;
        double y = 2.71;
        double minValue = Math.min(x, y);
        System.out.println("The minimum value is: " + minValue);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

The minimum value is: 2.71


In this example, we compare two double values, `x` and `y`, using Math.min(). The method returns the minimum value, which is 2.71.

Example 3

public class MathMinExample2 {
    public static void main(String[] args) {
        long a = 1000000000L;
        int b = 1000000;
        long minValue = Math.min(a, b);
        System.out.println("The minimum value is: " + minValue);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

The minimum value is: 1000000


Here, we compare a long value, `a`, with an int value, `b`. The Math.min() method automatically performs type conversion and returns the minimum value as a long, which is 1000000.

Example 4

public class MathMinExample3 {
    public static void main(String[] args) {
        int a = -10;
        int b = 5;
        int c = 0;
        int minValue = Math.min(a, Math.min(b, c));
        System.out.println("The minimum value is: " + minValue);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output

The minimum value is: -10


In this example, we find the minimum value among three integers: 'a`, `b`, and `c`. We use nested Math.min() calls to compare `b` and `c` first and then compare the result with `a`. The method returns the minimum value, which is -10.

Frequently Asked Questions

Can Math.min() be used with more than two numbers?

Yes, you can use nested Math.min() calls to find the minimum among multiple numbers.

Does Math.min() work with negative numbers?

Yes, Math.min() works with negative numbers & correctly identifies the minimum value.

Can Math.min() compare numbers of different data types?

Yes, Math.min() automatically performs type conversion when comparing numbers of different data types.

Conclusion

In this article, we discussed the Math.min() method in Java, which is used to find the minimum value between two numbers. We learned about its syntax, parameters, and return value and saw examples of how to use it effectively in code. Math.min() is a simple and powerful method that can be used in many situations to compare and find the minimum value among numbers.
You can also check out our other blogs on Code360.

Live masterclass