03_使用API_系统与数学工具

数学工具

Math
  • 代表数学,是一个工具类,里面提供的都是对数据进行操作的一些静态方法
public class Test {
    public static void main(String[] args) {
        // 1. Math.abs() : 取绝对值
        System.out.println(Math.abs(-3.14));  // >>> 3.14

        // 2. Math.ceil() : 向上取整
        System.out.println(Math.ceil(4.000001));  // >>> 5.0
        System.out.println(Math.ceil(4.0));  // >>> 4.0

        // 3. Math.floor() : 向下取整
        System.out.println(Math.floor(4.999999));  // >>> 4.0
        System.out.println(Math.ceil(4.0));  // >>> 4.0

        // 4. Math.round() : 四舍五入
        System.out.println(Math.round(3.4999));  // >>> 3
        System.out.println(Math.round(3.5001));  // >>> 4

        // 5. Math.max() : 取较大
        //    Math.min() : 取较小
        System.out.println(Math.max(10, 20));  // >>> 20
        System.out.println(Math.min(10, 20));  // >>> 10

        // 6. Math.pow() : 次方运算
        System.out.println(Math.pow(2, 3));  // 2的3次方 >>> 8.0
        System.out.println(Math.pow(3, 2));  // 3的2次方 >>> 9.0

        // 7. Math.random() : 从0~1中取随机数,[ 0.0 , 1.0 )
        System.out.println(Math.random());  // >>> 0.6582009378991727

    }
}
BigDecimal
  • 用于解决浮点型运算时,出现结果失真的问题
import java.math.BigDecimal;
import java.math.RoundingMode;

public class Test {
    public static void main(String[] args) {
        // double型运算时候,可能会出现失真的问题,如下
        double a = 0.1;
        double b = 0.2;
        System.out.println(a + b);  // >>> 0.30000000000000004

        // 解决办法
        // 1.1 先把它们变成字符串,然后交给 BigDecimal 封装成对象进行运算
//      BigDecimal objA =  new BigDecimal(Double.toString(a));
//      BigDecimal objB =  new BigDecimal(Double.toString(b));

        // 1.2 快速方法 (推荐以下方式)
        BigDecimal objA = BigDecimal.valueOf(a);
        BigDecimal objB = BigDecimal.valueOf(b);

        // 2. 加法
        BigDecimal objC = objA.add(objB);
        System.out.println(objC);  // >>> 0.3

        // 3. 减法
        BigDecimal objD = objA.subtract(objB);
        System.out.println(objD);  // >>> -0.1

        // 4. 乘法
        BigDecimal objE = objA.multiply(objB);
        System.out.println(objE);  // >>> 0.02

        // 5. 除法(注意!除法比较特殊,例如1/3这些数是除不尽的,所以必须要指定精度2,以及舍入模式)
        BigDecimal objF = objA.divide(objB, 2, RoundingMode.HALF_DOWN);  // 精度2位,模式为"四舍五入"
        System.out.println(objF);  // >>> 0.50

        // 6. 由于在开发中更常用的是 double 类型,所以还要将 BigDecimal 转回 double 类型
        double res = objF.doubleValue();
        System.out.println(res);  // >>> 0.50
    }
}

系统工具

System
  • System 代表程序所在的系统,也是一个工具类
public class Test {
    public static void main(String[] args) {
        // 1. 终止当前运行的 Java 虚拟机 : System.exit()
        // 该参数用作状态码; 按照管理,非零状态码表示异常终止
//      System.exit(0);  // 人为的终止就输入一个 0  (此代码尽量不要使用)

        // 2. 获取系统当前时间 : System.currentTimeMillis()
        // 返回的是long类型的时间毫秒值,指的是从1970-1-1,0:0:0开始到此刻的总的毫秒值,1s = 1000ms
        long time = System.currentTimeMillis();
        System.out.println(time);
    }
}
Runtime
  • 代表程序所在的运行环境
  • Runtime 是一个单例类
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException, InterruptedException {
        // 1. 返回与当前 Java 应用程序关联的运行时对象
        Runtime r = Runtime.getRuntime();

        // 2. 终止当前运行的虚拟机,该参数用作状态码;按照惯例,非零状态码表示异常终止
//      r.exit(0);

        // 3. 获取虚拟机能够使用的处理器数
        System.out.println(r.availableProcessors());  // >>> 16

        // 4. 返回 Java 虚拟机中的可用内存量
        System.out.println(r.totalMemory() / 1024.0 / 1024.0 + "MB");  // 1024 = 1K >>> 252.0MB

        // 5. 返回 Java 虚拟机中的可用内存量
        System.out.println(r.freeMemory() / 1024.0 / 1024.0 + "MB");  // >>> 248.23760986328125MB

        // 6. 启动某个程序,并返回代表该程序的对象
        Process obj = r.exec("E:\\mine\\run.exe");  // 输入"程序"的绝对路径
        Thread.sleep(5000);  // 让程序在此暂停5秒
        obj.destroy();  // 关闭程序
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值