×

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

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

Java Future Example

Future is an interface in the Java language that is a part of  java.util.concurrent package. It serves as a symbol for the output of an asynchronous computation. The interface offers ways to determine whether a computation has finished,  wait for it to finish, and receive its result. Once the task or calculation is finished, it cannot be undone. A Future interface offers ways to determine whether the computation is finished, to wait for it to finish, and to receive the computation's results. When the computation...

3 minutes read.

How to get Day Name from Date in Java

We'll write a Java application to extract the day's name from the Date in this section. When dealing with Date and time in Java, the following classes are used. Class for Calendars:...

6 minutes read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

Blocking Queue in Java Example

Let's first briefly comprehend queue before moving on to the topic of "Blocking Queue." A queue seems to be an orderly list of items in which elements are added from...

8 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.