Javaexercise.com

Get absolute value of float, int, double and long in Java

An absolute value is a positive value that represents a non-negative value of the given value. For example, the absolute value of -3 will be 3 and the same for positive 3 as well. 

Input number can be positive or negative but the absolute value always be a positive number.

In Java, To get an absolute value of any number, the Math class provides abs() method. Although, we can make our own custom code as well to get the absolute value.

Let's understand with examples.

 

Example: Get Absolute Value in Java

In this example, we used our own custom code to get an absolute value. This is a simple approach and a single-line solution as well. 

/* 
 *  Code example to find absolute value in Java
 */
public class JExercise {
	public static void main(String[] args) {
		// absolute value of integer value
		int a = - 10;
		int result = (a<0) ? -a : a; 
		System.out.println(result);
	}
}

Output:

10

 

Example: Get Absolute Value in Java using Math.abs() Method

Java Math.abs() method is a built-in method of Math class that returns the absolute value of the given input. 

/* 
 *  Code example to find absolute value in Java
 */
public class JExercise {
	public static void main(String[] args) {
		int a = - 10;
		int b = 12;
		int result = Math.abs(a);
		System.out.println(result);
		result = Math.abs(b);
		System.out.println(result);
	}
}

Output:

10
12
 

 

Overloading Version of Math.abs() Method in Java

Java Math class has several built-in overloaded methods of abs() method to find the absolute value of other data types as well. See the below table.

Modifier and Type

Method and Description

static double abs(double a)

 

It takes a double type value as a parameter and returns the absolute value of a double value.

static float abs(float a)

 

It takes a float type parameter and returns the absolute value of a float value.

static int abs(int a)

 

It takes an int type parameter and returns the absolute value of an int value.

static long abs(long a)

 

It takes a long type parameter and Returns the absolute value of a long value.

 

Example: Get absolute value of float, int, double, and long using Math.abs() Method in Java

In this example, we used all the overloaded versions of the Math.abs() method to get the absolute value of int, long, float, and double type. See the example below.

/* 
 *  Code example to find absolute value of int, float, long, and double in Java
 */
public class JExercise {
	public static void main(String[] args) {
		// absolute value of integer value
		int a = - 10;
		int result = Math.abs(a);
		System.out.println(result);
		// absolute value of long value
		long b = - 10;
		long result2 = Math.abs(b);
		System.out.println(result2);
		// absolute value of float value
		float c = - 10;
		float result3 = Math.abs(c);
		System.out.println(result3);
		// absolute value of double value
		double d = - 10;
		double result4 = Math.abs(d);
		System.out.println(result4);
	}
}

Output:

10
10
10.0
10.0
 

Example: Absolute Value of Integer.Min_Value

This is a case, where the abs() method returns a negative value. If we get the absolute value of integer minimum value then this method returns the same value with a negative sign. See the example below.

/* 
 *  Code example to find absolute value in Java
 */
public class JExercise {
	public static void main(String[] args) {
		// absolute value of integer value
		int a = Integer.MIN_VALUE;
		int result = Math.abs(a);
		System.out.println(result);
	}
}

Output:

-2147483648
 

Example: Absolute Value of 0 in Java 

The absolute value of 0 and -0 will be positive zero. See the example below.

/* 
 *  Code example to find absolute value in Java
 */
public class JExercise {
	public static void main(String[] args) {
		// absolute value of integer value
		int result = Math.abs(-0);
		System.out.println(result);
		result = Math.abs(0);
		System.out.println(result);
	}
}

Output:

0
0
 

Example: Absolute Value of Negative Infinity and NaN in Java

The absolute value of negative infinity and positive infinity is an infinity 

/* 
 *  Code example to find absolute value in Java
 */
public class JExercise {
	public static void main(String[] args) {
		// absolute value of integer value
		double result = Math.abs(Double.NEGATIVE_INFINITY);
		System.out.println(result);
		result = Math.abs(Double.NaN);
		System.out.println(result);
	}
}

Output:

Infinity
NaN