常用API
(一)Math
Math是静态工具类
public class Test5 {
public static void main(String[] args) {
// 1.abs()
int i = -5;
System.out.println(Math.abs(i));
// 2.ceil() double 向上取整
System.out.println(Math.ceil(0.95));
// 3.floor() double 向下取整
System.out.println(Math.floor(1.056));
// 4.round() int 四舍五入
System.out.println(Math.round(1.4));
// 5.max() min() int
System.out.println(Math.max(5, 6));
System.out.println(Math.min(5, 0));
// 6.random()包前不包后 double
System.out.println(Math.random());
// 7.pow(double a,double b) 返回a的b次幂
System.out.println(Math.pow(2.0, 3.0));
}
}
(二)System
System 代表程序所在的系统
//从1970年起的毫秒值
//相减算程序运行的时间
long time = System.currentTimeMillis();
//非零状态码代表非正常终止
//状态码为0表示人为终止系统
System.exit(0);
(三)Runtime
表示JAVA程序运行的环境
属于是一个单例类,对外只提供一个对象
public class Runtime {
// 提供一个 私有 静态 不可更改 的 Runtime成员变量
private static final Runtime currentRuntime = new Runtime();
private static Runtime.Version version;
// 提供一个静态的成员方法用来获得该类的单例对象
public static Runtime getRuntime() {
return currentRuntime;
}
// 私有的成员方法
private Runtime() {
}
public void exit(int status) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkExit(status);
}
Shutdown.exit(status);
}
Runtime类提供的常见方法
// 获得Runtime对象
Runtime rt = Runtime.getRuntime();
// 1.exit(int i)
// System提供的exit()方法内部就是调用了该方法
// rt.exit();
// 2.availableProcessors() 可用处理器数目
System.out.println(rt.availableProcessors());
// 3.totalMemory() 返回内存总量
System.out.println(rt.totalMemory());
// 4.freeMemory() 返回可用内存量
System.out.println(rt.freeMemory());
// 5.exec(String command)
Process P = rt.exec("Tim");
//程序睡眠五秒
Thread.sleep(5000);
P.destroy();