Java中StrictMath subtractExact()方法及示例


在Java中,`subtractExact()`是`StrictMath`類的靜態方法,位於`java.lang`包中。

本文將討論`StrictMath`類及其一些內建方法。我們將瞭解`subtractExact()`方法的實現,以及它與該類其他方法的不同之處。

Java中的StrictMath類

`StrictMath`是一個最終類,繼承自`Object`類。由於該類所有方法都是靜態的,因此無需建立例項即可使用其方法,可以直接呼叫靜態方法。

呼叫靜態方法

Class_name.static_method_name

匯入StrictMath類

import java.lang.StrictMath;

讓我們先討論`StrictMath`類的一些方法,然後在下一節中討論其`subtractExact()`方法。

  • abs(value) - 返回給定引數的正值。它只接受一個引數。

  • ceil(value) - 以double值為引數,返回大於給定引數的舍入值。

  • floor(value) - 以double值為引數,返回小於給定引數的舍入值。

  • log(value) - 以double值為引數,返回以e為底的對數值。

  • max(value1, value2) - 返回給定兩個引數中的最大值。

  • min(value1, value2) - 返回給定兩個引數中的最小值。

  • random() - 生成0到1之間的隨機數。(原文缺少引數,此處修正)

  • pow(value1, value2) - 接受兩個引數,返回value1的value2次冪。

  • round(value) - 返回給定引數最接近的整數。

示例

在這個例子中,我們將實現上面討論的方法,以便更好地理解。我們使用類名來呼叫所有這些方法。

import java.lang.StrictMath;
public class Methods {
   public static void main(String[] args) {
      int n1 = 45;
      int n2 = 9;
      double d1 = 46.992;
      double d2 = 34.27;
      System.out.println("Printing a random value between 0 and 1: " + StrictMath.random());
      System.out.println("Ceil value of d2: " + StrictMath.ceil(d2));
      System.out.println("Absolute value of d1: " + StrictMath.abs(d1));
      System.out.println("Floor value of d2: " + StrictMath.floor(d2));
      System.out.println("Floor modulus value of n1 and n2: " + StrictMath.floorMod(n1, n2));
      System.out.println("Logarithmic value of d2: " + StrictMath.log(d2));
      System.out.println("Maximum value between n1 and n2: " + StrictMath.max(n1, n2));
      System.out.println("Minimum value between n1 and n2: " + StrictMath.min(n1, n2));
      System.out.println(" 9 to power 2 is: " + StrictMath.pow(n2, 2));
      System.out.println("Rounded value of d1: " + StrictMath.round(d1));
   }
}

輸出

Printing a random value between 0 and 1: 0.5155915867224573
Ceil value of d2: 35.0
Absolute value of d1: 46.992
Floor value of d2: 34.0
Floor modulus value of n1 and n2: 0
Logarithmic value of d2: 3.5342703358865175
Maximum value between n1 and n2: 45
Minimum value between n1 and n2: 9
9 to power 2 is: 81.0
Rounded value of d1: 47

subtractExact()方法

`subtractExact()`方法計算兩個給定引數之間的差並返回結果。它適用於整數和長整型基本資料型別。

到目前為止,我們討論的所有方法都不會丟擲任何異常。但是,當結果超出其引數型別的範圍時,它會丟擲`ArithmeticException`異常。

語法

StrictMath.strictExact(val1, val2);

它將從`val1`中減去`val2`。

示例1

下面的例子說明了使用整數型別實現`subtractExact()`方法。

import java.lang.StrictMath;
public class Methods {
   public static void main(String[] args) {
      int i1 = 45;
      int i2 = 9;
      System.out.println("Difference between i1 and i2: " + StrictMath.subtractExact(i1, i2));
   }
}

輸出

Difference between i1 and i2: 36

示例2

在這個例子中,我們將看到它如何與長整型資料型別一起工作。

import java.lang.StrictMath;
public class Methods {
   public static void main(String[] args) {
      long l1 = 459653499;
      long l2 = 287933475;
      System.out.println("Difference between l1 and l2: " + StrictMath.subtractExact(l1, l2));
   }
}

輸出

Difference between l1 and l2: 171720024

結論

`StrictMath`類在需要進行數學計算時非常有用。它提供各種內建方法來對數值資料型別進行運算。在這篇文章中,我們瞭解了`StrictMath`類及其內建方法`subtractExact()`。

更新於:2023年5月5日

102 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告