Java Math abs() method with Examples Last Updated : 09 Nov, 2021 Comments Improve Suggest changes Like Article Like Report Absolute value refers to the positive value corresponding to the number passed as in arguments. Now geek you must be wondering what exactly it means so by this it is referred no matter what be it positive or negative number been passed for computation, the computation will occur over the positive corresponding number in both cases. So in order to compute the absolute value for any number we do have a specified method in Java referred to as abs() present inside Math class present inside java.lang package. The java.lang.Math.abs() returns the absolute value of a given argument. If the argument is not negative, the argument is returned.If the argument is negative, the negation of the argument is returned. Syntax : public static DataType abs(DataType a) Parameters: Int, long, float, or double value whose absolute value is to be determined Returns Type: This method returns the absolute value of the argument. Exceptions Thrown: ArithmeticException Tip: One must be aware of generic return type as follows: If the argument is of double or float type: 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.If the argument is of int or long type: If the argument is equal to the value of Integer.MIN_VALUE or Long.MIN_VALUE, the most negative representable int or long value, the result is that same value, which is negative. Example 1: Java // Java Program to Illustrate Absolute Method // of Math Class // Importing all Math classes // from java.lang package import java.lang.Math; // Main class class GFG { // Main driver method public static void main(String[] args) { // Custom integer input received from user int n = -7; // Printing value before applying absolute function System.out.println( "Without applying Math.abs() method : " + n); // Applying absolute math function and // storing it in integer variable int value = Math.abs(n); // Printing value after applying absolute function System.out.println( "With applying Math.abs() method : " + value); } } OutputWithout applying Math.abs() method : -7 With applying Math.abs() method : 7 Example 2: Java // Java Program to Demonstrate Working of abs() method // of Math class inside java.lang package // Importing Math class // from java.lang package import java.lang.Math; // Main class class GFG { // Main driver method public static void main(String args[]) { // Customly declaring and initializing all // arguments that ans() function takes // Float float a = 123.0f; float b = -34.2323f; // Double double c = -0.0; double d = -999.3456; // Integer int e = -123; int f = -0; // Long long g = -12345678; long h = 98765433; // abs() method taking float type as input System.out.println(Math.abs(a)); System.out.println(Math.abs(b)); // abs() method taking double type as input System.out.println(Math.abs(1.0 / 0)); System.out.println(Math.abs(c)); System.out.println(Math.abs(d)); // abs() method taking int type as input System.out.println(Math.abs(e)); System.out.println(Math.abs(f)); System.out.println(Math.abs(Integer.MIN_VALUE)); // abs() method taking long type as input System.out.println(Math.abs(g)); System.out.println(Math.abs(h)); System.out.println(Math.abs(Long.MIN_VALUE)); } } Output123.0 34.2323 Infinity 0.0 999.3456 123 0 -2147483648 12345678 98765433 -9223372036854775808 Comment More infoAdvertise with us Next Article Java Math abs() method with Examples N Niraj_Pandey Follow Improve Article Tags : Java Java-lang package java-math Practice Tags : Java Similar Reads Java Math acos() method with Examples The Math.acos() method returns the arc cosine of an angle between 0.0 and pi (0.0, pi). Arc cosine is also called as inverse of a cosine. If the argument is NaN or its absolute value is greater than 1, then the result is NaN.Syntax of Math acos() Methodpublic static double acos(double a)Parameter: a 2 min read Java Math asin() method with Example The asin() method of java.lang.Math class, computes the arc sine also called as an inverse of a sine of a given value. This method is useful when we have the sine of an angle and we need to find the angle. This method returns the arc sine of an angle in between -pi/2 and pi/2.If the argument is NaN 2 min read Java Math ulp() method with Examples In Java, the Math.ulp() method returns the size of the unit of least precision (ulp) of a given number. This method is used to find the smallest difference between a given floating-point number and the next larger number it can represent. It works with both float and double types. In this article, w 3 min read Java Math copySign() method with Examples The copySign() method in Java is a part of java.lang.Math class. This method copies the sign of one number to the magnitude of another. We can also say that it takes two numbers and returns a value with the absolute value of the first and the sign of the second.This method is very useful in mathemat 2 min read Java Math sin() method with Examples The java.lang.Math.sin() returns the trigonometry sine of an angle in between 0.0 and pi. If the argument is NaN or infinity, then the result is NaN. If the argument is zero, then the result is a zero with the same sign as the argument. The value returned will be between -1 and 1. Syntax : public st 2 min read Java cbrt() method with Examples The Math.cbrt() is a part of java.lang.Math package. This method is used to calculate the cube root of a given number. In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.Note: A number, when multiplied by itself three time 3 min read Java Math hypot() method with Example The Math.hypot() function is an in-built Math function in Java. Before going deep dive to the topic, first understand the reason behind to use this function.Suppose we have a right-angled triangle and we know the lengths of the two shorter sides i.e., x and y. To calculate the hypotenuse, we use sqr 2 min read Java atan() method with Examples The Math.atan() method in Java is used to calculate the arc tangent of a given value. This method is part of the java.lang.Math package. In this article, we are going to discuss the working of this method with regular values and for special cases such as infinity and NaN. This method is very useful 3 min read Math class methods in Java with Examples | Set 2 java.math class and its methods | Set 1 java.math class methods discussed in this article : abs() : java.math.abs() method returns the absolute value of any type of argument passed. This method can handle all the data types. Special Case : Result is positive zero, if the argument is positive zero or 6 min read Java sqrt() method with Examples The Math.sqrt() method is a part of java.lang.Math package. This method is used to calculate the square root of a number. This method returns the square root of a given value of type double. In this article, we are going to discuss how this method works for regular values and for special cases such 2 min read Like