Java 中的 abs() 方法
    
    Mehvish Ashiq
    2023年10月12日
    
    Java
    Java Method
    
 
我们将学习 Java 中的 abs() 方法来查找指定数字的绝对值。我们将通过编写和练习各种代码示例来学习。
Java 中的绝对值是什么
绝对值是指指定数字的非负值。例如,-4 的绝对值为 4。
我们使用 java.lang.Math 包的 abs() 方法。它只接受一个类型为 int、double、float 或 long 的参数并返回其非负(绝对)值。
以下是一些经验法则,你必须记住要成为专家,同时找到给定数字的绝对值。
- 如果参数的数据类型是 float或double:
 1.1abs()返回正值;传递的参数是正数还是负数都没有关系。
 1.2 如果我们将Infinity作为参数传递,它的结果是POSITIVE_INFINITY。
 1.3 如果它的参数是NaN,它返回NaN。
 1.4 如果abs()方法得到一个负零或正零,它返回一个正零。
- 如果参数的数据类型是 long或int:
 2.1 如果Long.MIN_VALUE或Integer.MIN_VALUE的值等于abs()方法的参数,输出将是相同的,一个负值。
在 Java 中使用 abs() 方法查找数字的绝对值
在本节中,我们将编写不同的代码示例来练习上面给出的所有规则。
示例代码(当将负数作为参数传递时):
import java.lang.Math;
public class findAbsoluteValue {
  public static void main(String[] args) {
    int intNumber = -8;
    System.out.println("Before applying the Math.abs() function: " + intNumber);
    int absoluteValue = Math.abs(intNumber);
    System.out.println("After applying the Math.abs() function: " + absoluteValue);
  }
}
输出:
Before applying the Math.abs() function: -8
After applying the Math.abs() function: 8
在 main 方法中,我们声明并初始化一个变量以保存一个值,该值进一步传递给 Math.abs() 函数以计算给定数字的绝对值(正值)。
我们在应用 Math.abs() 函数之前和之后打印一个数字的值。在接下来的示例中将遵循相同的过程,但变量名称和类型将被更改。
示例代码(当一个正数作为参数传递时):
import java.lang.Math;
public class findAbsoluteValue {
  public static void main(String[] args) {
    int intNumber = 8;
    System.out.println("Before applying the Math.abs() function: " + intNumber);
    int absoluteValue = Math.abs(intNumber);
    System.out.println("After applying the Math.abs() function: " + absoluteValue);
  }
}
输出:
Before applying the Math.abs() function: 8
After applying the Math.abs() function: 8
示例代码(当 infinity 作为参数传递时):
import java.lang.Math;
public class findAbsoluteValue {
  public static void main(String[] args) {
    double doubleNumber = Double.POSITIVE_INFINITY;
    System.out.println("Before applying the Math.abs() function: " + doubleNumber);
    double absoluteValue = Math.abs(doubleNumber);
    System.out.println("After applying the Math.abs() function: " + absoluteValue);
  }
}
输出:
Before applying the Math.abs() function: Infinity
After applying the Math.abs() function: Infinity
示例代码(当 NaN 作为参数传递时):
import java.lang.Math;
public class findAbsoluteValue {
  public static void main(String[] args) {
    double doubleNumber = Double.NaN;
    System.out.println("Before applying the Math.abs() function: " + doubleNumber);
    double absoluteValue = Math.abs(doubleNumber);
    System.out.println("After applying the Math.abs() function: " + absoluteValue);
  }
}
输出:
Before applying the Math.abs() function: NaN
After applying the Math.abs() function: NaN
示例代码(当正零或负零作为参数传递时):
import java.lang.Math;
public class findAbsoluteValue {
  public static void main(String[] args) {
    int number, absoluteValue;
    number = -0;
    System.out.println("Before applying the Math.abs() function: " + number);
    absoluteValue = Math.abs(number);
    System.out.println("After applying the Math.abs() function: " + absoluteValue);
    number = 0;
    System.out.println("Before applying the Math.abs() function: " + number);
    absoluteValue = Math.abs(number);
    System.out.println("After applying the Math.abs() function: " + absoluteValue);
  }
}
输出:
Before applying the Math.abs() function: 0
After applying the Math.abs() function: 0
Before applying the Math.abs() function: 0
After applying the Math.abs() function: 0
示例代码(当 Long.MIN_VALUE 或 Integer.MIN_VALUE 作为参数传递时):
import java.lang.Math;
public class findAbsoluteValue {
  public static void main(String[] args) {
    long longNumber = Long.MIN_VALUE;
    System.out.println("Before applying the Math.abs() function: " + longNumber);
    long longAbsVal = Math.abs(longNumber);
    System.out.println("After applying the Math.abs() function: " + longAbsVal);
    int intNumber = Integer.MIN_VALUE;
    System.out.println("Before applying the Math.abs() function: " + intNumber);
    int intAbsVal = Math.abs(intNumber);
    System.out.println("After applying the Math.abs() function: " + intAbsVal);
  }
}
输出:
Before applying the Math.abs() function: -9223372036854775808
After applying the Math.abs() function: -9223372036854775808
Before applying the Math.abs() function: -2147483648
After applying the Math.abs() function: -2147483648
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
作者: Mehvish Ashiq
    
