常用API(一):Math,Sysytem,Runtime

1.Math

 

package com.itheima.Math;

public class math {
    public static void main(String[] args) {
        //math
        System.out.println(Math.abs(-123));
        System.out.println(Math.abs(-0.23));//绝对值,可以是整数也可以是浮点数
        System.out.println(Math.ceil(4.0000001));//向上取整,5.0
        System.out.println(Math.ceil(4.000000));//向上取整,4.0
        System.out.println(Math.floor(4.999999999999));//向下取整
        System.out.println(Math.round(4.4999));//四舍五入
        System.out.println(Math.max(20, 30));//取较大值
        System.out.println(Math.min(20, 30));//取较小值
        System.out.println(Math.pow(2, 3));//2的3次方
        System.out.println(Math.random());//【0.0,1.0)包左不包右



        //System
//        System.exit(0);//终止当前运行的java虚拟机,人为终止虚拟机(不要使用)
//        System.out.println("=-=-=-");
        //Runtime
        System.out.println(System.currentTimeMillis());//获取系统时间毫秒值
        int n=0;
        for (int i = 0; i < 1000000; i++) {
            n+=1;
        }
        System.out.println(System.currentTimeMillis());//获取系统时间毫秒值,做程序性能分析

    }
}

 3.Runtime 运行时     (Runtime单例类)

 

package com.itheima.Math;

import java.io.IOException;

public class math {
    public static void main(String[] args) throws IOException, InterruptedException {
        Runtime r=Runtime.getRuntime();
        //r.exit(0);//终止当前运行的虚拟机程序,该参数用作状态代码,按照惯例,非零状态代码表示异常终止
        System.out.println(r.availableProcessors());//获取虚拟机能够使用的处理器数量
        System.out.println(r.totalMemory()/1024.0*1024.0+"MB");//获取JAVA虚拟机的内存数量 1024=1k
        System.out.println(r.freeMemory()/1024.0*1024.0+"MB");//获取JAVA虚拟机空闲的内存数量
        Process p=r.exec("\"C:\\网易云\\CloudMusic\\cloudmusic.exe\"");//添路径就可以运行程序
        Thread.sleep(5000);//暂停5秒
        p.destroy();//关闭程序
    }
}

 

posted @ 2024-04-06 18:10  小彭先森  阅读(3)  评论(0编辑  收藏  举报