为什么在这里我要导入java.lang.Math类?

来源:百度知道 编辑:UC知道 时间:2024/06/01 12:50:54
import java.lang.Math;
public class MathTest{
public static void main(String[] args){
int i=Math.max(5,6);
int f=(int)Math.max(5.0f,6.0f);
double d=Math.max(10,11.5);
long l=Math.max(2200000000l,2500000000l);
System.out.println(i);
System.out.println(f);
System.out.println(d);
System.out.println(l);
}
}

原则上要加的.
但为了方便,java.lang包里的东西JDK会自动加上.
所以你不加编译也可以通过.

因为你在这里用到了Math.max(5.0f,6.0f);这个函数,Math是一个类,这个类存在java.lang.Math;中,max是这个类中的静态方法。你引入了这个包java虚拟机才能找到这个类,也就能找到这个方法。不导入虚拟机就找不到这个类,也就找不到这个方法

因为你用到了这个包里的Math.max(int,int)方法,所以要导入这个类

回你一句话“用了Math类,肯定要导入才行...”