Java - StrictMath max(long x, long y) 方法



描述

Java StrictMath max(long a, long b) 方法返回兩個長整型值中較大的一個。也就是說,結果是更接近正無窮大的引數。如果引數值相同,則結果為該值。如果任一值為 NaN,則結果為 NaN。與數值比較運算子不同,此方法認為負零嚴格小於正零。如果一個引數為正零,另一個引數為負零,則結果為正零。

宣告

以下是java.lang.StrictMath.max() 方法的宣告

public static long max(long a, long b)

引數

  • a − 一個引數

  • b − 另一個引數

返回值

此方法返回 a 和 b 中較大的一個。

異常

獲取兩個正長整型值的最大值示例

以下示例顯示了兩個正值的 StrictMath max() 方法的用法。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two long numbers
      long x = 60984;
      long y = 497;
   
      // call max and print the result
      System.out.println("StrictMath.max(" + x + "," + y + ")=" + StrictMath.max(x, y));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

StrictMath.max(60984,497)=60984

獲取一個正長整型值和一個負長整型值的最大值示例

以下示例顯示了正值和負值的 StrictMath max() 方法的用法。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two long numbers
      long x = -60984;
      long y = 497;
   
      // call max and print the result
      System.out.println("StrictMath.max(" + x + "," + y + ")=" + StrictMath.max(x, y));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

StrictMath.max(-60984,497)=497

獲取兩個負長整型值的最大值示例

以下示例顯示了負值的 StrictMath max() 方法的用法。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two long numbers
      long x = -60984;
      long y = -497;
   
      // call max and print the result
      System.out.println("StrictMath.max(" + x + "," + y + ")=" + StrictMath.max(x, y));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

StrictMath.max(-60984,-497)=-497
java_lang_strictmath.htm
廣告