×

Java Math abs() Method

The abs() method of Math class returns the absolute value of the argument where the argument can be int, double, float, long.

Syntax public static int abs(int a) public static float abs(float a) public static double abs(double a) public static long(long a) Parameters The argument whose absolute value is to be determined Return Value

The abs() method returns the absolute value of the argument.

  • If the argument passed is positive, it will return positive value.
  • If the argument passed is Infinity, it will return positive infinity.
  • If the argument passed is NaN, it will return NaN.
Example 1:
public class JavaMathAbsExample1 {
    public static void main(String[] args) {
        int x=-109;
        int y=109;
        //returns an absolute value for int type
        System.out.println("Absolute value of "+x +" : "+Math.abs(x));
        System.out.println("Absolute value of "+y +" : "+Math.abs(y));
    }
}
Example 2:
public class JavaMathAbsExample2 {
    public static void main(String[] args) {
        double val1=1234545465644555553333.78;
        double val2=-233.3;
        //returns an absolute value for double type
        System.out.println("Absolute value of "+val1+" : "+Math.abs(val1));
        System.out.println("Absolute value of "+val2+" : "+Math.abs(val2));
        System.out.println("Absolute value of Min value : "+Math.abs(Double.
MIN_VALUE));
    }
}
Output:
Absolute value of 1.2345454656445554E21 : 1.2345454656445554E21
Absolute value of -233.3 : 233.3
Absolute value of Min value : 4.9E-324
Example 3:
public class JavaMathAbsExample3 {
    public static void main(String[] args) {
        Float x =82726f;
        Float y = -287372356f;
        //returns an absolute value for float type
        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 Min value : "+Math.abs(Float
.MIN_VALUE));
        System.out.println("Absolute value of 0/0 : "+Math.abs(0f/0f));
    }
}
Output:
Absolute value of 82726.0 : 82726.0
Absolute value of -2.87372352E8 : 2.87372352E8
Absolute value of Min value : 1.4E-45
Absolute value of 0/0 : NaN
Example 4:
public class JavaMathAbsExample4 {
    public static void main(String[] args) {
        Long x=223l;
        Long y=-9l;
        //returns an absolute value for long type
        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 Min value : "+Math.abs(Long.
MIN_VALUE));
    }
}
Output:
Absolute value of 223 : 223
Absolute value of -9 : 9
Absolute value of Min value : -9223372036854775808

Related Topics

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

Java Break Keyword

Break: The word Break is a keyword or a statement in a java programming language.This Keyword break is used to stop the execution of the loop or switch statements etc.The loop...

3 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

How to Convert int to char in Java

To convert a higher data type to lower data type, we need to do typecasting. Casting is also required when we want to convert ASCII value into character. It is...

3 minutes read.

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal: Using toOcatlString() method Using user-defined logic Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer...

2 minutes read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

How to override toString() method in Java?

Java is an object-oriented language. It only works with classes and objects. Thus, whenever we need to calculate, we need an object or objects that belong to the class. The Java method...

2 minutes read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

4 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

3 minutes read.

Selection Sort in Java

Selection Sort in Java Selection sort is also a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion of the input array, and placing it...

5 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.