
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the Absolute Value of Float, Int, Double, and Long in Java
The java.lang.Math class has an abs() method which facilitates us to find the absolute values of different data types.
Absolute value of float
In order to compute the absolute value of a float value, we use the java.lang.Math.abs(float a) method. If the argument ‘a’ is negative, the negation of ‘a’ is returned. If the argument ‘a’ is non-negative, the argument itself is returned. When the argument is positive zero or negative zero, the result is positive zero. In case the argument is infinite, the result is positive infinity. If the argument is NaN, the result is NaN.
Declaration − The declaration of Math.abs(float a) function is as follows −
public static float abs(float a)
where a is the argument whose absolute value is to be returned.
Absolute value of int
In order to compute the absolute value of a int value, we use the java.lang.Math.abs(int a) method. If the argument ‘a’ is negative, the negation of ‘a’ is returned. If the argument ‘a’ is non-negative, the argument itself is returned. If the argument ‘a’ has the value Integer.MIN_VALUE, the negative value itself is returned. When the argument is positive zero or negative zero, the result is positive zero.
Declaration − The declaration of Math.abs(int a) function is as follows −
public static int abs(int a)
where a is the argument whose absolute value is to be returned.
Absolute value of double
In order to compute the absolute value of a double value, we are bound to use the java.lang.Math.abs(double a) method. If the argument ‘a’ is negative, the negation of ‘a’ is returned. If the argument ‘a’ is non-negative, the argument itself is returned. When the argument is positive zero or negative zero, the result is positive zero. In case the argument is infinite, the result is positive infinity. If the argument is NaN, the result is NaN.
Declaration − The declaration of Math.abs(double a) function is as follows −
public static double abs(double a)
where a is the argument whose absolute value is to be returned.
Absolute value of long
In order to compute the absolute value of a long value, we use the java.lang.Math.abs(long a) method. If the argument ‘a’ is negative, the negation of ‘a’ is returned. If the argument ‘a’ is non-negative, the argument itself is returned. If the argument ‘a’ has the value Long.MIN_VALUE, the negative value itself is returned. When the argument is positive zero or negative zero, the result is positive zero.
Declaration − The declaration of Math.abs(long a) function is as follows −
public static long abs(long a)
where a is the argument whose absolute value is to be returned.
Let us see a program where we find the absolute value of float, int, double and long data types.
Example
import java.lang.Math; public class Example { public static void main(String[] args) { // declaring and initialising some integer values int a = 10; int b = -9; // declaring and initialising some float values float c = 8.11f; float d = -9.32f; // declaring and initialising some double values double x = -100.01d; double y = 90.344d; // declaring and initialising some long values long r = 1234567891223l; long s = -4567891234554l; //printing their absolute values System.out.println("Absolute value of " + a + " = " + Math.abs(a)); System.out.println("Absolute value of " + b + " = " + Math.abs(b)); System.out.println("Absolute value of " + c + " = " + Math.abs(c)); System.out.println("Absolute value of " + d + " = " + Math.abs(d)); System.out.println("Absolute value of " + x + " = " + Math.abs(x)); System.out.println("Absolute value of " + y + " = " + Math.abs(y)); System.out.println("Absolute value of " + r + " = " + Math.abs(r)); System.out.println("Absolute value of " + s + " = " + Math.abs(s)); } }
Output
Absolute value of 10 = 10 Absolute value of -9 = 9 Absolute value of 8.11 = 8.11 Absolute value of -9.32 = 9.32 Absolute value of -100.01 = 100.01 Absolute value of 90.344 = 90.344 Absolute value of 1234567891223 = 1234567891223 Absolute value of -4567891234554 = 4567891234554