max()方法语法:
public static float max(float f1 , float f2); public static int max(int i1 , int i2); public static long max(long l1 , long l2); public static double max(double d1 , double d2);
max()方法在java.lang包中可用。
这些方法用于返回方法中给定参数的最大值之一,换句话说,此方法返回方法中给定两个参数的最大值。
这些方法不会引发异常。
这些是静态方法,可以使用类名进行访问,如果尝试使用类对象访问这些方法,则不会出现任何错误。
参数:
方法接受两个参数int / long / float / double表示值,以查找最大值/最大值。
返回值:
此方法的返回类型为int / long / float / double-它从给定的两个参数返回最大值/最大值。
注意:
如果我们将NaN作为任何参数传递,则method返回NaN。
示例
//Java程序演示示例
//max()StrictMath类的方法
public class Max {
public static void main(String[] args) {
//变量声明
float f1 = -0.0f;
float f2 = 0.0f;
float f3 = -0.6f;
float f4 = 124.58f;
int i1 = -0;
int i2 = 0;
long l2 = 0l;
long l3 = -2468l;
long l4 = 12458l;
double d1 = -0.0;
double d2 = 0.0;
//显示f1,f2,f3和f4的先前值
System.out.println("f1: " + f1);
System.out.println("f2: " + f2);
System.out.println("f3: " + f3);
System.out.println("f4: " + f4);
//显示i1,i2的先前值
System.out.println("i1: " + i1);
System.out.println("i2: " + i2);
//显示l1,l2的先前值
System.out.println("l3: " + l3);
System.out.println("l4: " + l4);
//显示d1,d2的先前值
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);
System.out.println();
System.out.println("max(float,float): ");
//在这里,我们得到(0.0),因为我们
//传递参数的值为(-0.0f,0.0f)
System.out.println("StrictMath.max(f1,f2): " + StrictMath.max(f1, f2));
//在这里,我们将得到(124.58)并且我们
//传递参数的值为(0.0f,124.58f)
System.out.println("StrictMath.max(f2,f4): " + StrictMath.max(f2, f4));
System.out.println();
System.out.println("max(int,int): ");
//在这里,我们将得到,因为我们
//传递参数,其值为(-0,0)
System.out.println("StrictMath.max(i1,i2): " + StrictMath.max(i1, i2));
System.out.println();
System.out.println("max(long,long): ");
//在这里,我们将得到(12458)并且我们
//传递参数的值为(0l,12458l)
System.out.println("StrictMath.max(l2,l4): " + StrictMath.max(l2, l4));
System.out.println();
System.out.println("max(double,double): ");
//在这里,我们得到(0.0),因为我们
//传递参数,其值为(-0.0,0.0)
System.out.println("StrictMath.max(d1,d2): " + StrictMath.max(d1, d2));
}
}输出结果
f1: -0.0 f2: 0.0 f3: -0.6 f4: 124.58 i1: 0 i2: 0 l3: -2468 l4: 12458 d1: -0.0 d2: 0.0 max(float,float): StrictMath.max(f1,f2): 0.0 StrictMath.max(f2,f4): 124.58 max(int,int): StrictMath.max(i1,i2): 0 max(long,long): StrictMath.max(l2,l4): 12458 max(double,double): StrictMath.max(d1,d2): 0.0