【Java】System类、Math类、BigInteger类、BigDecimal类

1.System类

System类代表系统,提供很多有用的方法(标准输入、输出、错误输出等),该类在java.lang包。
System类用final修改,构造器用private修改,无法创建对象,内部成员变量和成员方法都是static的,可以直接通过类名调用。
常用方法

native long currentTimeMillis();
void exit(int status);
void gc();
String getProperty(String key);

查看信息

        System.out.println(System.getProperty("java.version"));
        System.out.println(System.getProperty("java.home"));
        System.out.println(System.getProperty("os.name"));
        System.out.println(System.getProperty("os.version"));
        System.out.println(System.getProperty("user.home"));
        System.out.println(System.getProperty("user.home"));
        System.out.println(System.getProperty("user.dir"));

2.Math

java.lang.Math类提供了一系列静态方法用于数学计算,方法的参数和返回值类型一般为double。

Math.PI;   //圆周率20位
Math.sin();
Math.random();
Math.min();
Math.max();

3.BigInteger类、BigDecimal类

在java.math包中,BigInteger可以表示任意精度的整数,可容纳超大整数,BigDecimal可以表示任意精度的小数,超大浮点数。

BigInteger bi = new BigInteger("123456789123456789123456789123456789123456789123456789");
BigDecimal bd = new BigDecimal("123456789123456789.123456789123456789123456789123456789123456789");
BigDecimal bd2 = new BigDecimal("11");
System.out.println(bi);
System.out.println(bd.divide(bd2)); //报错java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result

System.out.println(bd.divide(bd2,BigDecimal.ROUND_HALF_UP));
System.out.println(bd.divide(bd2,15,BigDecimal.ROUND_HALF_UP)); //保留15位

参考链接:
1.BigInteger 与 BigDecimal区别,及BigDecimal详解

posted @ 2022-11-24 11:00  植树chen  阅读(18)  评论(0编辑  收藏  举报