Java.lang.StrictMath.abs() 方法
描述
java.lang.StrictMath.abs(int a) 方法返回一个 int 值的绝对值。 如果参数不是负数,则返回参数。 如果参数是否定的,则返回参数的否定。
声明
以下是 java.lang.StrictMath.abs() 方法的声明。
public static int abs(int a)
参数
a − 这是要确定其绝对值的参数。
返回值
此方法返回一个 int 值的绝对值。
异常
NA
示例
下面的例子展示了 java.lang.StrictMath.abs() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class StrictMathDemo {
public static void main(String[] args) {
int i1 = 54 , i2 = -35;
// returns the absolute value of positive int value
int iAbsValue = StrictMath.abs(i1);
System.out.println("absolute value of " + i1 + " = " + iAbsValue);
// returns the absolute value of negative int value
iAbsValue = StrictMath.abs(i2);
System.out.println("absolute value of " + i2 + " = " + iAbsValue);
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
absolute value of 54 = 54 absolute value of -35 = 35

