×

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 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.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

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.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

Zig Zag star and Number Pattern in Java

We covered many Java pattern applications in the preceding part. We will write Java applications for zigzag star and number patterns in this part. Printing Zig Zag Number Pattern Steps Print one...

3 minutes read.

Cosmic Superclass in Java

The parent class of all Java classes is the Object class. The Java Object class is the parent of all Java classes, whether directly or indirectly. The Object class is...

6 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

How to Convert Binary to Decimal in Java

How to Convert Binary to Decimal in Java There are two methods to convert binary to decimal. Using Integer.parseInt() method Using user-defined logic Using Integer.parseInt() method The parseInt() is a static method of...

2 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Why we use static in Java

Many reserved keywords in Java cannot be used as variable names or identifiers. Java uses static keywords frequently because of its effective memory management system. In most cases, you must...

3 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 minutes read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

How to Concatenate Two Strings in Java

How to Concatenate Two Strings in Java Concatenation of two strings means adding the beginning of one string to the end of the other string. Some of the ways to concatenate...

3 minutes read.

Java 16

Java 16 is the most recent short-term incremental release, based on Java 15, and it was released on March 16, 2021. Records and sealed classes are just two of the...

11 minutes read.