Home » Java programming language

Java Math Class static int abs(int i) method with example

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

Math Class static int abs(int i)

  • 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 int datatype in the method that means the return type of this method is int.
  • 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 int datatype argument so the following syntax is given below,

Syntax:

    public static int abs(int i){
    }

Parameter(s):

int i – an integer value whose absolute value to be found.

Return value:

The return type of this method is int, returns absolute value as an integer.

Note:

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

Java program to demonstrate example of abs(int i) method

// Java program to demonstrate the example of 
// abs(int i) method of Math class

class AbsIntTypeMethod {
    public static void main(String[] args) {
        // We are declaring few variables
        int a = 123121;
        int b = -123121;
        int c = 0;
        int d = -0;
        /*
        int e = 7/0;
        int f = -7/0;
        */

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

        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));

        /* 
        // In the below code exception will be thrown 
        // because we are passing infinity
        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 AbsIntTypeMethod.java

E:\Programs>java AbsIntTypeMethod
The absolute value of a is : 123121
The absolute value of b is : 123121
The absolute value of c is : 0
The absolute value of d is : 0


Comments and Discussions!

Load comments ↻





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