Which Math method is used to calculate the absolute value of a number

The abs() method is used to calculate absolute values.

In Core Java, the Math.abs() method is used to calculate the absolute value of a number. The abs() method returns the absolute value of its argument, which is the distance of the argument from zero. Here’s an example:

java
public class AbsoluteValueExample {
public static void main(String[] args) {
int number = -10;
double decimalNumber = -15.5;

int absoluteInt = Math.abs(number);
double absoluteDouble = Math.abs(decimalNumber);

System.out.println("Absolute value of " + number + " is: " + absoluteInt);
System.out.println("Absolute value of " + decimalNumber + " is: " + absoluteDouble);
}
}

In this example, Math.abs() is used for both integers and doubles to obtain their absolute values. The output will be:

csharp
Absolute value of -10 is: 10
Absolute value of -15.5 is: 15.5