×

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

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.

The final Keyword in Java

The final keyword is employed in several instances. Firstly, the non-access modifier final only applies to variables, methods, and classes. The final can be used in the following situations. Final Variables When...

6 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

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.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

Array Slicing in Java

Array slicing is a method in Java for obtaining a subarray of a specified array. Assume a[] is an array. It contains eight items indexed from a[0] to a[7].a[] =...

3 minutes read.

Java Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

3 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.

Alice and Bob Problem Java

Alice and Bob were two friends who liked to play games. Both together found a game, which has the description below. The game begins with an integer num, used to create...

2 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.