Java Math min() Method

The Math.min() method in Java is used to return the smaller of two values.

Table of Contents

  1. Introduction
  2. min() Method Syntax
  3. Examples
    • min(double a, double b)
    • min(float a, float b)
    • min(int a, int b)
    • min(long a, long b)
  4. Real-World Use Case
  5. Conclusion

Introduction

The Math.min() method returns the smaller of two specified values. This method is part of the Math class in Java and is used to perform comparisons between two values of the same data type.

min() Method Syntax

The Math.min() method is overloaded to handle different primitive data types: doublefloatint, and long. Each version returns the smaller of the two specified values.

The syntax for the min() method varies depending on the types of the arguments:

min(double a, double b)

public static double min(double a, double b)

min(float a, float b)

public static float min(float a, float b)

min(int a, int b)

public static int min(int a, int b)

min(long a, long b)

public static long min(long a, long b)

Parameters:

  • a: The first value to compare.
  • b: The second value to compare.

Returns:

  • The smaller of the two specified values.

Examples

min(double a, double b)

The min(double a, double b) method returns the smaller of two double values.

Example

public class MinDoubleExample {
    public static void main(String[] args) {
        double value1 = 3.5;
        double value2 = 2.8;

        double result = Math.min(value1, value2);

        System.out.println("The smaller value between " + value1 + " and " + value2 + " is " + result);
    }
}

Output:

The smaller value between 3.5 and 2.8 is 2.8

min(float a, float b)

The min(float a, float b) method returns the smaller of two float values.

Example

public class MinFloatExample {
    public static void main(String[] args) {
        float value1 = 3.5f;
        float value2 = 2.8f;

        float result = Math.min(value1, value2);

        System.out.println("The smaller value between " + value1 + " and " + value2 + " is " + result);
    }
}

Output:

The smaller value between 3.5 and 2.8 is 2.8

min(int a, int b)

The min(int a, int b) method returns the smaller of two int values.

Example

public class MinIntExample {
    public static void main(String[] args) {
        int value1 = 5;
        int value2 = 8;

        int result = Math.min(value1, value2);

        System.out.println("The smaller value between " + value1 + " and " + value2 + " is " + result);
    }
}

Output:

The smaller value between 5 and 8 is 5

min(long a, long b)

The min(long a, long b) method returns the smaller of two long values.

Example

public class MinLongExample {
    public static void main(String[] args) {
        long value1 = 100000L;
        long value2 = 50000L;

        long result = Math.min(value1, value2);

        System.out.println("The smaller value between " + value1 + " and " + value2 + " is " + result);
    }
}

Output:

The smaller value between 100000 and 50000 is 50000

Real-World Use Case

Finding the Minimum Value in an Array

In real-world scenarios, the Math.min() method can be used to find the minimum value in an array of numbers. This is useful in various applications such as data analysis, financial calculations, and more.

Example

public class MinValueInArrayExample {
    public static void main(String[] args) {
        int[] values = {3, 5, 7, 2, 8, 6};

        int minValue = values[0];
        for (int value : values) {
            minValue = Math.min(minValue, value);
        }

        System.out.println("The minimum value in the array is " + minValue);
    }
}

Output:

The minimum value in the array is 2

Conclusion

The Math.min() method in Java provides a way to compare two values and return the smaller one. By understanding how to use this method and its overloaded versions, you can handle various comparison operations and solve problems that require finding the minimum value. Whether you are working with double, float, int, or long values, the min() method offers a reliable tool for determining the smaller of two values.

Comments