常用API(二)Math,System,Runtime

Math

036d6653c9cd47cc949f0896ace0368d.png

 

 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

c8b2206c2297433b89243893935e3641.png

 

终止当前虚拟机

System.exit(0);//人为的终止虚拟机

获取当前系统的时间

ecdf8e44bc3344eeb6bb5ed8ebdaf61d.png

 

返回的是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

ec30b40d3dd24ac798befad0a72bf89c.png

 

代表程序所在的运行环境

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();//销毁,关闭程序 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值