public class Test2 {
public static void main(String[] args) {
System.out.println(Math.min(15l,17f));
System.out.println(Math.min(12.5f,14l));
}
}
结果:
15.0
12.5
Process finished with exit code 0
public class Test2 {
public static void main(String[] args) {
System.out.println(Math.min(15l,17f));
System.out.println(Math.min(12.5f,14l));
}
}
结果:
15.0
12.5
Process finished with exit code 0
这个自动类型转换是Java语言本身就有的功能,可以看下Math.max的源码:
@IntrinsicCandidate
public static double max(double a, double b) {
if (a != a)
return a; // a is NaN
if ((a == 0.0d) &&
(b == 0.0d) &&
(Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b;
}
return (a >= b) ? a : b;
}
方法接收的两个参数其实都是double类型,如果传入int型变量,int型会自动转换为double类型。