×

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

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 minutes read.

Java String equalsIgnoreCase() method

equalsIgnoreCase() method compares two Strings based on the their content but ignore the case. Syntax public boolean equalsIgnoreCase(Objects anObject) Parameter anObject: Object to be compared with the current String without case consideration. Returns It returns true...

1 minute read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Java Integer lowestOneBit()

The lowestOneBit () method of Java Integer class returns an int value with at most a single one-bit, in the position of the lowest-order one-bit in the specified int value.  Syntax public...

2 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute 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 Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Java Print Writer

PrintWriter: It is used to write output data from the file. It is the class of Java.io package. It inherits the properties of the Writer class. Writer Class: It is a class of...

4 minutes read.

Converting Roman to Integer Numerals in java

In this article, you will be acknowledged about the process of conversion of roman numbers to integers in java. The most important approaches are discussed and also the implementation of...

3 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.