Home » Java programming language

Java Math Class static double min(double d1 , double d2) with example

Java Math Class static double min(double d1 , double d2) method: Here, we are going to learn about the static double min(double d1 , double d2) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on September 06, 2019

Math Class static double min(double d1 , double d2)

  • This method is available in java.lang package.
  • This method is used to return the minimum one of both the given arguments or in other words this method returns the smallest value of the given two arguments.
  • This is a static method so this method is accessible with the class name too.
  • The return type of this method is double, it returns the smallest element from the given two arguments.
  • This method accepts two arguments of double values.
  • This method does not throw any exception.

Syntax:

    public static double min(double d1, double d2){
    }

Parameter(s): double d1, double d2 – two double values, in which we have to find the smallest/minimum value.

Return value:

The return type of this method is double, it returns the smallest/minimum value.

Note:

  • If we pass "NaN" (Not a Number), it returns the same value i.e. "NaN".
  • If we pass zero (-0 or 0), it returns the 0.
  • If we pass the same values in both parameters, it returns the same value.

Java program to demonstrate example of min(double d1, double d2) method

// Java program to demonstrate the example of 
// min(double d1, double d2) method of Math Class.

public class MinDoubleTypeMethod {
    public static void main(String[] args) {
        // declaring variables
        double d1 = -0.0;
        double d2 = 0.0;
        double d3 = -0.6;
        double d4 = 124.68;

        // displaying the values
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);
        System.out.println("d4: " + d4);

        // Here , we will get (-0.0) because we are passing parameter 
        // whose value is (-0.0,0.0)
        System.out.println("Math.min(d1,d2): " + Math.min(d1, d2));

        // Here , we will get (0.0) and we are passing parameter 
        // whose value is (0.0,124.68)
        System.out.println("Math.min(d2,d4):" + Math.min(d2, d4));
    }
}

Output

E:\Programs>javac MinDoubleTypeMethod.java

E:\Programs>java MinDoubleTypeMethod
d1: -0.0
d2: 0.0
d3: -0.6
d4: 124.68
Math.min(d1,d2): -0.0
Math.min(d2,d4):0.0


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.