常用API-Math、System、Runtime

1.Math

  • 是一个帮助我们用于数学计算的工具类
  • 私有化构造方法,所有的方法都是静态的

Math类的常用方法

方法名说明

public  static  int

abs(int  a)获取参数绝对值
public  static  doubleceil(double  a)向上取整
public  static  doublefloor(double  a)向下取整
public  static  intround(float  a)四舍五入
public  static  intmax(int  a,int  b)获取两个int中的较大值
public  static  doublepow(double  a,double  b)返回a的b次幂的值
public  static  doublerandom()返回值为double的随机值,范围[0.0 , 1.0)
public static void main(String[] args) {
        //abs 获取参数绝对值
        System.out.println(Math.abs(88));//88
        System.out.println(Math.abs(-88));//88
        //bug:
        //以int为例,取值范围-2147483648 ~ 2147483647
        System.out.println(Math.abs(-2147483648));//-2147483648

        //ceil  向上取整
        System.out.println(Math.ceil(12.34));//13.0
        System.out.println(Math.ceil(12.54));//13.0
        System.out.println(Math.ceil(-12.54));//-12.0
        System.out.println(Math.ceil(-12.54));//-12.0

        //floor  向下取整
        System.out.println(Math.floor(12.44));//12.0
        System.out.println(Math.floor(12.55));//12.0
        System.out.println(Math.floor(-12.44));//-13.0
        System.out.println(Math.floor(-12.55));//-13.0

        //round  四舍五入
        System.out.println(Math.round(12.45));//12
        System.out.println(Math.round(12.54));//13

        //max  获取较大值
        System.out.println(Math.max(12.3, 13.2));//13.2
        System.out.println(Math.max(10, 20));//20

        //min  获取两数中较小值
        System.out.println(Math.min(20, 30));//20

        //pow  获取a的b次幂
        System.out.println(Math.pow(2, 3));//8.0
        System.out.println(Math.pow(4,0.5));//2.0
        System.out.println(Math.pow(2, -1));//0.5

        //sqrt  获取a的平方根
        System.out.println(Math.sqrt(4));//2.0

        //cbrt  获取a的立方根
        System.out.println(Math.cbrt(8));//2.0

        //random  获取[0.0 , 1.0)之间的随机数
        for (int i = 0; i < 10; i++) {
            System.out.println(Math.random());
        }

        //获取1~100之间的随机数
        for (int i = 0; i < 10; i++) {
            System.out.println(Math.floor(Math.random() * 100) + 1);
        }
}

2.System

System也是一个工具类,提供了一些与系统相关的方法

System类的常用方法

方法名说明
public  static  void  exit(int  status)终止当前运行的Java虚拟机
public  static  long  currentTimeMillis()返回从时间原点开始到代码运行这个时间点在这个过程中一共过了多少毫秒
public  static  void  arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数)数组拷贝
public class SystemDemo2 {
    public static void main(String[] args) {
        //exit  停止虚拟机运行
        //状态码 0:虚拟机正常停止 非0:虚拟机异常停止
        //System.exit(0);
        //System.out.println("看看我执行了吗");
        //应用:当我们需要把整个程序结束的时候,就可以调用这个方法

        //currentTimeMillis  获取时间毫秒值
        long l = System.currentTimeMillis();
        System.out.println(l);
        //应用:计算程序执行需要的时间

        //arraycopy  拷贝数组
        int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
        int[] arr2 = new int[10];
        System.arraycopy(arr1,0,arr2,0,10);
        for (int i = 0; i < arr2.length; i++) {
            int number = arr2[i];
            System.out.print(number + " ");
        }

        //arr2: 0 0 0 0 1 2 3 0 0 0
        System.arraycopy(arr1,0,arr2,4,3);

        //细节:
        //1.如果数据源数组和目的地数组都是基本数据类型,那么两者的类型必须保持一致,否则会报错
        //2.在拷贝的时候需要考虑数组的长度,如果超出范围也会报错
        //3.如果数据源数组和目的地数组都是引用数据类型,那么子类类型可以赋值给父类类型
    }
}

3.Runtime

Runtime表示当前虚拟机的运行环境

Runtime类的常用方法

方法名说明
public  static  Runtime  getRuntime()当前系统的运行环境

public  void  exit(int  status)

停止虚拟机
public  int  availableProcessors()获取CPU线程数
public  long  maxMemory()JVM能从系统中获取总内存大小(单位byte)
public  long  totalMemory()JVM已经从系统中获取总内存大小(单位byte)
public  long  freeMemory()JVM剩余内存大小(单位byte)
public  Process  exec(String  command)运行cmd命令
import java.io.IOException;

public class RuntimeDemo1 {
    public static void main(String[] args) throws IOException {
        //获取Runtime对象
        //Runtime对象表示当前系统的运行环境,所有Runtime对象都表示同一个地址值
        Runtime r1 = Runtime.getRuntime();
        Runtime r2 = Runtime.getRuntime();
        System.out.println(r1 == r2);//true

        //exit  停止虚拟机
        //Runtime.getRuntime().exit(0);
        //System.out.println("看看我执行了吗");

        //获取CPU线程数
        System.out.println(Runtime.getRuntime().availableProcessors());//8

        //获取总内存大小,单位byte
        System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024);//1804

        //已经获取的总内存大小
        System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024);//123

        //剩余内存大小
        System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024);//119

        //运行cmd命令
        Runtime.getRuntime().exec("notepad");//打开记事本
        //shutdown :关机  加上参数才能执行
        //-s :默认在一分钟后关机
        //-s -t 指定时间 :指定关机时间
        //-a :取消关机操作
        //-r :关机并重启
        Runtime.getRuntime().exec("shutdown -s -t 3600");//1小时后关机
        Runtime.getRuntime().exec("shutdown -a");//取消关机操作
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值