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
广告
© . All rights reserved.