Absolute Value

Math Class

Absolute Value

🙋 Need help? Ask an expert now!

The absolute value method in the Math class returns the absolute value of an int or double. Java offers more than two absolute value methods with different parameters depending on the output necessary. The absolute value methods are written in this manner: Math.abs(parameter). Take a look at the absolute value method below.

AbsoluteValueMethodExample.java
package exlcode;

public class AbsoluteValueMethodExample {

  public static int exampleVariableOne = Math.abs(10);
  public static int exampleVariableTwo = Math.abs(-10);

  public static void main(String[] args) {
    System.out.println(exampleVariableOne);
    System.out.println(exampleVariableTwo);
  }
}

The absolute value method works the same way as it does in a regular algebra class. The input for this method could be an int, float, double or a byte value and will return the same data type as the input. Keep in mind that none of the outputs are rounded when producing the result of the method. Inputting a positive value would simply return the same value while inputting a negative value would return the negative value without the minus sign.

Edit Me on GitHub!

Application Question

Math.abs(-3) returns