java.lang.Math.abs()
method is used to find the absolute value of a float value in Java for the given input (x
– parameter).
If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.
abs()
method exists in Math
class of java.lang
package.Since: JDK1.0
Declaration of abs()
Method
The declaration of
abs()
method is:
public static float abs(float a)
Syntax of abs()
Method
The syntax of
abs()
method in Java is:
java.lang.Math.abs(float x)
Example
/* *Find the absolute value of given input(float) without importing the java.lang Package. */ public class JavaMathExample { public static void main(String[] args) { //Declaring variables float x = -4876.1874f; float result; //Assign absolute value of x to result variable result = java.lang.Math.abs(x); //Printing result to the console System.out.println(result); } }
Output:
4876.1875
Example
By importing java.lang
package the syntax of abs()
method can be used as:
/* *Find the absolute value of given input(float) by importing the java.lang Package. */ import java.lang.*; //importing package public class JavaMathExample { public static void main(String[] args) { //Declaring variables float x = -68.97F; float result; //Assign absolute value of x to result variable result = Math.abs(x); //Printing result to the console System.out.println(result); } }
Output:
68.97
Special Cases of abs()
Method in Java
If the argument is positive zero or negative zero, the result is positive zero.
If the argument is infinite, the result is positive infinity.
If the argument is NaN, the result is NaN.
Example
If the argument is infinite, the result is positive infinity.
If the argument is NaN, the result is NaN.
Example
/* *This example shows some special cases of abs() method in Java */ import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { //Declaring variables float positiveZero = 0; float negativeZero = -0; float positiveInfinity = Float.POSITIVE_INFINITY; float negativeInfinity = Float.NEGATIVE_INFINITY; float nan = Float.NaN; float result; //Here argument is positive zero, output will also be positive zero result = Math.abs(positiveZero); System.out.println(result); //Here argument is negative zero, output will be positive zero result = Math.abs(negativeZero); System.out.println(result); //Here argument is positive infinity, output will also be positive infinity result = Math.abs(positiveInfinity); System.out.println(result); //Here argument is negative infinity, output will be negative infinity result = Math.abs(negativeInfinity); System.out.println(result); //Here argument is NaN, output will be NaN result = Math.abs(nan); System.out.println(result); } }
Output:
0.0
0.0
Infinity
Infinity
NaN
Parameters of abs()
Method in Java
x
– Where x
is any valid float input value. This parameter is required.
Error Handling
If the
If there is no argument (
Example
x
parameter is not a number abs()
method returns an error
.If there is no argument (
x
– input value) passes to the method, the compiler will produce an error
.Example
/* *This example returns a compile time error */ import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { //Declaring variables String x = "Not a number"; float result; //argument is not a number result = Math.abs(x); System.out.println(result); //Here no argument is used result = Math.abs(); System.out.println(result); } }
Output:
Error
Return Value of abs()
Method in Java
abs()
method returns the absolute value of a float value for the given input (x
– parameter).
Java abs()
Method Example 1
import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { float negativeValue = -18.869f; float positiveValue = 27.78639f; float zero = 0; System.out.println("absolute value of a Negative Value: "+Math.abs(negativeValue)); System.out.println("absolute value of a Positive Value: "+Math.abs(positiveValue)); System.out.println("absolute value of Zero: "+Math.abs(zero)); } }
Output:
absolute value of a Negative Value: 18.869
absolute value of a Positive Value: 27.78639
absolute value of Zero: 0.0
Java abs()
Method Example 2
import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { //Declare and initialize an array containing float values float test [] = { Float.MAX_VALUE, Float.MIN_VALUE, Float.NaN, Float.MIN_EXPONENT, 42.867e-12f, 16.852f, 0, -2.58f, -78.8569e7f }; //Use for loop to print absolute value of each element of the array for (int i=0; i < test.length; i++) { System.out.println( Math.abs(test[i]) ); } } }
Output:
3.4028235E38
1.4E-45
NaN
126.0
4.2867E-11
16.852
0.0
2.58
7.8856902E8