Home »
Java programming language
Java Math Class static long abs(long l) method with example
Java Math Class static long abs(long l) method: Here, we are going to learn about the static long abs(long l) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on August 19, 2019
Math Class static long abs(long l)
- 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 depends on given argument datatype and here we are passing long data type in the method that means the return type of this method is long.
- In this method, we pass only one parameter as an argument in the method of Math class.
- This method does not throw any exception.
- In this method, if the given argument is equal to the value of long.MIN_VALUE it returns the same negative value.
- This is an overridable method so various versions of this method are available but here we are looking long datatype argument so the following syntax is given below,
Syntax:
public static long abs(long l){
}
Parameter(s):
long l – a long value whose absolute value to be found.
Return value:
The return type of this method is long, returns absolute value as a long.
Note:
- If we pass a positive long integer, it returns the same value.
- If we pass a negative long integer, it returns the value without sign i.e. it returns the positive long 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(long l) method
// Java program to demonstrate the example of
// abs(long l) method of Math class
class AbsLongTypeMethod {
public static void main(String[] args) {
// We are declaring few variables
long a = 123121l;
long b = -123121l;
long c = 0l;
long d = -0l;
/*
long e = 74587l/0l;
long f = -7458l/0l;
*/
// By using abs(long l) method we will find 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 AbsLongTypeMethod.java
E:\Programs>java AbsLongTypeMethod
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