How do you represent absolute value in Java?
In Java, you can use the abs() method from the Math class to obtain the absolute value of a number. The syntax of this method is as follows:
int abs(int a) // 返回一个整数的绝对值
long abs(long a) // 返回一个长整数的绝对值
float abs(float a) // 返回一个浮点数的绝对值
double abs(double a) // 返回一个双精度浮点数的绝对值
For example, to obtain the absolute value of an integer, you can use the following code:
int num = -5;
int absNum = Math.abs(num);
System.out.println("Absolute value of " + num + " is " + absNum);
The output result is:
Absolute value of -5 is 5