Home » Java programming language

Java StrictMath abs() method with example

StrictMath Class abs() method: Here, we are going to learn about the abs() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 25, 2019

StrictMath Class abs() method

Syntax:

    public static float abs(float f);
    public static int abs(int i);
    public static long abs(long l);
    public static double abs(double d);
  • abs() method is available in java.lang package.
  • These methods are used to return the absolute value of the given parameter in the method.
  • These methods don't throw an exception.
  • These are static methods, it is accessible with the class name and, if we try to access these methods with the class object then we will not get any error.

Parameter(s):

  • int / long / float / double – represents the value whose absolute value to be found.

Return value:

The return type of this method is int / long / float / double – It returns the absolute value of the given value.

Note:

  • If we pass a positive value, then the same value is returned.
  • If we pass a negative value, then the same value without sign is returned.
  • If we pass a zero, then the same value (zero) is returned.
  • If we pass a NaN, then NaN is returned.

Example:

// Java program to demonstrate the example 
// of abs() method of StrictMath class

public class Abs {
    public static void main(String[] args) {

        // variable declarations
        double a = 123.121d;
        double b = -123.121d;

        int c = 123121;
        int d = -123121;

        long e = 123121l;
        long f = -123121l;

        float g = 123.121f;
        float h = -123.121f;

        // Display previous value of a,b  
        System.out.println("a: " + a);
        System.out.println("b: " + b);

        // Display previous value of c,d
        System.out.println("c: " + c);
        System.out.println("d: " + d);

        // Display previous value of e,f 
        System.out.println("e: " + e);
        System.out.println("f:" + f);

        // Display previous value of g,h
        System.out.println("g: " + g);
        System.out.println("h: " + h);


        System.out.println();
        System.out.println("abs(double): ");

        // By using abs(double d) method we will calculate the 
        //absolute value of given parameter in the method

        System.out.println("StrictMath.abs(a): " + StrictMath.abs(a));
        System.out.println("StrictMath.abs(b): " + StrictMath.abs(b));

        System.out.println();
        System.out.println("abs(int): ");

        // By using abs(int i) method we will calculate the 
        // absolute value of given parameter in the method

        System.out.println("StrictMath.abs(c): " + StrictMath.abs(c));
        System.out.println("StrictMath.abs(d): " + StrictMath.abs(d));

        System.out.println();
        System.out.println("abs(long): ");

        // By using abs(long l) method we will calculate the 
        // absolute value of given parameter in the method

        System.out.println("StrictMath.abs(e): " + StrictMath.abs(e));
        System.out.println("StrictMath.abs(f): " + StrictMath.abs(f));

        System.out.println();
        System.out.println("abs(double): ");

        // By using abs(double d) method we will calculate the 
        // absolute value of given parameter in the method

        System.out.println("StrictMath.abs(g): " + StrictMath.abs(g));
        System.out.println("StrictMath.abs(h): " + StrictMath.abs(h));
    }
}

Output

a: 123.121
b: -123.121
c: 123121
d: -123121
e: 123121
f:-123121
g: 123.121
h: -123.121

abs(double): 
StrictMath.abs(a): 123.121
StrictMath.abs(b): 123.121

abs(int): 
StrictMath.abs(c): 123121
StrictMath.abs(d): 123121

abs(long): 
StrictMath.abs(e): 123121
StrictMath.abs(f): 123121

abs(double): 
StrictMath.abs(g): 123.121
StrictMath.abs(h): 123.121


Comments and Discussions!

Load comments ↻





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