Home » Java programming language

Java Math Class static double abs(double d) method with example

Java Math Class static double abs(double d) method: Here, we are going to learn about the static double abs(double d) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on August 19, 2019

Math Class static double abs(double d)

  • This method is available in java.lang package.
  • This method is used to return the absolute value of the given parameter in the method.
  • This is a static method so this method is accessible with classname too.
  • The return type of this method is depend given argument datatype and here we are passing double datatype in the method that means the return type of this method is double.
  • In this method, we pass only one parameter as an argument in the method of Math class.
  • This method does not throw any exception.
  • This is an overridable method so various versions of this method are available but here we are looking double datatype argument so the following syntax is given below,

Syntax:

    public static double abs(double d){
    }

Parameter(s):

double d – a double value whose absolute value to be found.

Return value:

The return type of this method is double, returns absolute value as a double.

Note:

  • If we pass a positive double, it returns the same value.
  • If we pass a negative double, it returns the value without sign i.e. it returns the positive double.
  • If we pass positive zero (0) or negative zero (-0), it returns zero without sign (0).
  • If we pass an infinity parameter, it returns the same infinity value without the sign.
  • If we pass "NaN" (Not A Number), it returns the same i.e. a "NaN".

Java program to demonstrate example of abs(double d) method

// Java program to demonstrate the example of abs(double d) 
// method of Math class

class AbsDoubleTypeMethod {
    public static void main(String[] args) {
        // We are declaring few variables
        double a = 123.121d;
        double b = -123.121d;
        double c = 0.0d;
        double d = -0.0d;
        double e = 7.70d / 0.0d;
        double f = -7.70d / 0.0d;

        // By using abs(double d) method we will find the 
        // absolute value of given parameter
        System.out.println("The absolute value of a is : " + Math.abs(a));
        System.out.println("The absolute value of b is : " + Math.abs(b));
        System.out.println("The absolute value of c is : " + Math.abs(c));
        System.out.println("The absolute value of d is : " + Math.abs(d));
        System.out.println("The absolute value of e is : " + Math.abs(e));
        System.out.println("The absolute value of f is : " + Math.abs(f));
    }
}

Output

E:\Programs>javac AbsDoubleTypeMethod.java

E:\Programs>java AbsDoubleTypeMethod
The absolute value of a is : 123.121
The absolute value of b is : 123.121
The absolute value of c is : 0.0
The absolute value of d is : 0.0
The absolute value of e is : Infinity
The absolute value of f is : Infinity


Comments and Discussions!

Load comments ↻





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