Math
Math.abs(double a) 获取参数绝对值(拿到的结果一定是正数)
System.out.println(Math.abs(-12));//12
System.out.println(Math.abs(-3.14));//3.14
Math.ceil(double a)向上取整
System.out.println(Math.ceil(4.000001));//5.0
System.out.println(Math.ceil(4.0));//4.0
Math.floor(double a)向下取整
System.out.println(Math.floor(4.999999));//4.0
System.out.println(Math.floor(4.0));//4.0
Math.round(douuble a)四舍五入
System.out.println(Math.round(3.499));//3
System.out.println(Math.round(3.50001));//4
Math.max(int a,int b)取较大值
Math.min(int a,int b)取较小值
System.out.println(Math.max(10,20));//20
System.out.println(Math.min(10,20));//10
Math.pow(double a,double b)取次方
System.out.println(Math.pow(2,3));//2的3次方 8.0
System.out.println(Math.pow(3,2));//3的2次方 9.0
Math.random() 取随机数,[0.0 , 1.0)(包前不包后)
System.out.println(Math.random());
System
终止当前虚拟机
System.exit(0);//人为的终止虚拟机
获取当前系统的时间
返回的是long类型的时间毫秒值,指的是从1970年1月1日0点0分0秒开始走到此刻的毫秒值,1s=100ms
long time = System.currentTimeMillis();//返回的是long类型的变量
System.out.println(time);
应用:计算程序执行了多长时间,做程序的性能分析
long time = System.currentTimeMillis();
for(int i =0;i<1000000;i++){
System.out.println("执行了"+i);
}
long time2 = System.currentTimeMillis();
System.out.println((time2-time)/1000.0 +"s");//转化成秒
Runtime
代表程序所在的运行环境
Runtime是一个单例类,通过调用 getRuntime() 方法去得到对象
1, public static Runtime getRuntime()返回与当前Java应用程序关联的运行时对象
Runtime r = Runtime.getRuntime();
2,exit(int status)终止当前运行的虚拟机(不要使用)
r.exit(0);
3,public int availableProcessors() 获取虚拟机能够使用的处理器数
System.out.println(r.availableProcessors());//20
4,public long totalMemmory() 返回Java虚拟机中的内存总量
System.out.println(r.totalMemory()/1024.0 +"KB");// 1024 = 1k 1024*1024 = 1M
5,public long freeMemory() 返回Java虚拟机中的可用内存量(还有多少空余内存)
System.out.println(r.freeMemory()/1024.0/1024.0 +"MB");
6,public Process exec(String command) 启动某个程序,并且返回代表该程序的对象
r.exec("填入程序的启动路径");
r.exec("QQ");// 先把QQ的启动命令配置到环境变量
Process p = r.exec("QQ");
Thread.sleep(5000);//程序暂停5秒后继续往下走 看到销毁的效果
p.destory();//销毁,关闭程序