×

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

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Conditional operator in Java

In Java, there are around eight operators, and among them, three operators are used to evaluate the condition and decide the Result based on the Result of the evaluated condition. Below...

4 minutes read.

Types of Garbage Collector in Java

Garbage collection is a Java feature that offers automatic memory management. The JVM is in charge of it. The programmer does not have to handle object creation and deallocation. We...

3 minutes read.

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

Java Regular Expressions

Java Regular Expressions The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as...

8 minutes read.

Web Service Response Time Calculation in Java

Administration response time or reaction time is the typical measure of time it takes for a solicitation to be handled by a PC framework, like your organization switch. In the...

4 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Uses of Java

Java is used in many real-world Java applications, including technologies and tools. This Java programming language has become the backbone for developing many applications. In areas like embedded systems and...

3 minutes read.

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

Creating API Document Javadoc tool

The JavaDoc utility is a document generator tool written in Java that generates standard documentation in HTML format. It parses declarations and documentation in a source file collection that describes...

3 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Check if the given array is mirror inverse in Java

The primary objective is to check whether the given array is mirror inverse or not. The values and position of the specified array are switched, and a duplicate array is created....

3 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.