×

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type.

Syntax:

  1. public static double min (double a, double b)
  2. public static float min (float a, float b)
  3. public static int min (int a, int b)
  4. public static long min (long a, long b)

Parameters:

The parameters ‘a’ and ‘b’ represent the two values.

Return Value:

The min() method returns the smaller of a and b.

  • It returns the result of same value, if the arguments have same value
  • It returns NaN, if either value is NaN.
  • It returns negative zero, if one argument is positive zero and the other is negative zero.

Example 1:

public class JavaMathMinExample1 {
    public static void main(String[] args) {
        int a = 67;
        int b= 56;
        //returns the smaller number between a and b
        System.out.println("The smaller number between "+a+" and "+ b+ "
is : "+Math.min(a,b));
    }
}

Output:

The smaller number between 67 and 56 is : 56

Example 2:

public class JavaMathMinExample2 {
    public static void main(String[] args) {
        Double a = 67.0d;
        Double b= Double.NaN;
        //returns NaN, if either value is NaN
        System.out.println("The smaller number between "+a+" and "+ b+ "
is : "+Math.min(a,b));
    }
}

Output:

The smaller number between 67.0 and NaN is : NaN

Example 3:

public class JavaMathMinExample3 {
    public static void main(String[] args) {
        float a = 0f;
        float b=-0f;
        //returns negative zero, if one argument is positive zero and the
other is negative zero
        System.out.println("The smaller number between "+a+" and "+ b+ "
is : "+Math.min(a,b));
    }
}

Output:

The smaller number between 0.0 and -0.0 is : -0.0

Example 4:

public class JavaMathMinExample4 {
    public static void main(String[] args) {
        Double a = 67.0d;
        Double b= 67.0d;
        //returns the result  of same value, if the arguments have same value
        System.out.println("The smaller number between "+a+" and "+ b+ "
is : "+Math.min(a,b));
    }
}

Output:

The smaller number between 67.0 and 67.0 is : 67.0

Related Topics

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

Java ArrayList

Java ArrayList Class A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the...

12 minutes read.

The Maximum Rectangular Area in a Histogram in Java

Continuous bars should be used to form the largest possible rectangle. We'll assume in the interest of convenience that each bar's width is 1. Naive Approach In this method, each bar will be...

6 minutes read.

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

Star Program in Java

By solving the patterns, we can develop our coding skills and logical thinking. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic....

3 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

Creating a Custom Generic Class in Java

To indicate parameter types when creating generic classes, we utilize the <> symbol. The syntax used to generate objects of a generic class is as follows. // To create an instance...

4 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Java Speech Recognition

The customer experience and convenience are improved with its utilization. Command and control interest in buying and voice synthesizers are supported by the cross-platform API defined by the API. For...

4 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Java Math sinh() Method

The sinh() method of Java Math class returns the hyperbolic sine of the specified double value. Syntax: public static double sinh(double x) Parameters: The parameter ‘a’ represents the number whose hyperbolic sine is to...

2 minutes read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

Java 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.