常用System、Runtime、Math、BigInteger、BigDecimal、Random类API

时间:2024-4-9    作者:老大夫    分类: JAVA


1. System类

属性:out、in、err

方法 作用
currentTimeMillis() 获取当前时间戳
exit() 退出,参数为0表示正常退出,非0表示异常退出
gc() 请求垃圾回收
getProperty 获取参数
public class SystemTest {
    public static void main(String[] args) {
        String home=System.getProperty("java.home");
        System.out.println("java的根目录"+home);

        String osName = System.getProperty("os.name");
        System.out.println("os的name:" + osName);

        String osVersion = System.getProperty("os.version");
        System.out.println("os的version:" + osVersion);

        String userName = System.getProperty("user.name");
        System.out.println("user的name:" + userName);

        String userHome = System.getProperty("user.home");
        System.out.println("user的home:" + userHome);

        String userDir = System.getProperty("user.dir");
        System.out.println("user的dir:" + userDir);
    }
}

2. Runtime类

对应着Java进程内存的使用环境,是单例的(饿汉式)

方法 作用
public static Runtime getRuntime() 返回与当前 Java 应用程序相关的运行时对象。应用程序不能创建自己的 Runtime 类实例。
public long totalMemory() 返回 Java 虚拟机中初始化时的内存总量。此方法返回的值可能随时间的推移而变化,这取决于主机环境。默认为物理电脑内存的1/64。
public long maxMemory() 返回 Java 虚拟机中最大程度能使用的内存总量。默认为物理电脑内存的1/4。
public long freeMemory() 返回 Java 虚拟机中的空闲内存量。调用 gc 方法可能导致 freeMemory 返回值的增加。
public class RuntimeTest {
    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        long initialMemory = runtime.totalMemory(); //获取虚拟机初始化时堆内存总量
        long maxMemory = runtime.maxMemory(); //获取虚拟机最大堆内存总量
        String str = "";
        //模拟占用内存
        for (int i = 0; i < 10000; i++) {
            str += i;
        }
        long freeMemory = runtime.freeMemory(); //获取空闲堆内存总量
        System.out.println("总内存:" + initialMemory / 1024 / 1024 * 64 + "MB");
        System.out.println("总内存:" + maxMemory / 1024 / 1024 * 4 + "MB");
        System.out.println("空闲内存:" + freeMemory / 1024 / 1024 + "MB") ;
        System.out.println("已用内存:" + (initialMemory-freeMemory) / 1024 / 1024 + "MB");
    }
}

3. Math类

数学相关的运算

方法 作用
public static double abs(double a) 返回 double 值的绝对值。
public static double ceil(double a) 返回大于等于参数的最小的整数。
public static double floor(double a) 返回小于等于参数最大的整数。
public static long round(double a) 返回最接近参数的 long。(相当于四舍五入方法)
public static double pow(double a,double b) 返回a的b幂次方法
public static double sqrt(double a) 返回a的平方根
public static double random() 返回[0,1)的随机值
public static final double PI 返回圆周率
public static double max(double x, double y) 返回x,y中的最大值
public static double min(double x, double y) 返回x,y中的最小值
acos,asin,atan,cos,sin,tan 三角函数
long d1 = Math.round(5.5); //d1的值为6
long d2 = Math.round(5.4); //d2的值为5
long d3 = Math.round(-3.3); //d3的值为-3
long d4 = Math.round(-3.8); //d4的值为-4
long d4 = Math.round(-12.5); //d4的值为-12

四舍五入方法,找离的近的,当是负数时 -12.5前进为-12而不是-13.
够0.5就往右进一个整数

4.BigInteger

Integer类作为int的包装类,能存储的最大整型值为2^31-1,Long类也是有限的,最大为2^63-1。
而BigInteger可以达到任意位数,并且提供运算方法

•构造器

–BigInteger(String val):根据字符串构建BigInteger对象
方法 作用
–public BigInteger abs() 返回此 BigInteger 的绝对值的 BigInteger。
–BigInteger add(BigInteger val) 返回其值为 (this + val) 的 BigInteger
–BigInteger subtract(BigInteger val) 返回其值为 (this - val) 的 BigInteger
–BigInteger multiply(BigInteger val) 返回其值为 (this * val) 的 BigInteger
–BigInteger divide(BigInteger val) 返回其值为 (this / val) 的 BigInteger。整数相除只保留整数部分。
–BigInteger remainder(BigInteger val) 返回其值为 (this % val) 的 BigInteger。
–BigInteger[] divideAndRemainder(BigInteger val) 返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
–BigInteger pow(int exponent) 返回其值为 (this^exponent) 的 BigInteger。

5.BigDecimal

•一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故用到java.math.BigDecimal类。
•BigDecimal类支持不可变的、任意精度的有符号十进制定点数。

•构造器

–public BigDecimal(double val)
–public BigDecimal(String val) --> 推荐
方法 作用
–public BigDecimal add(BigDecimal augend)
–public BigDecimal subtract(BigDecimal subtrahend)
–public BigDecimal multiply(BigDecimal multiplicand)
–public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) divisor是除数,scale指明保留几位小数,roundingMode指明舍入模式(ROUNDUP :向上加1、ROUNDDOWN :直接舍去、ROUNDHALFUP:四舍五入)

举例

@Test
public void test03(){
    BigInteger bi = new BigInteger("12433241123");
    BigDecimal bd = new BigDecimal("12435.351");
    BigDecimal bd2 = new BigDecimal("11");
    System.out.println(bi);
    // System.out.println(bd.divide(bd2));
    System.out.println(bd.divide(bd2, BigDecimal.ROUND_HALF_UP));  //除不尽的数需要BigDecimal.ROUND_HALF_UP
    System.out.println(bd.divide(bd2, 15, BigDecimal.ROUND_HALF_UP));
}

6. Random类

这个类用于产生一些随机数

方法 作用
boolean nextBoolean() 返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 boolean 值。
void nextBytes(byte[] bytes) 生成随机字节并将其置于用户提供的 byte 数组中。
double nextDouble() 返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 double 值。
float nextFloat() 返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 float 值。
double nextGaussian() 返回下一个伪随机数,它是取自此随机数生成器序列的、呈高斯(“正态”)分布的 double 值,其平均值是 0.0,标准差是 1.0。
int nextInt() 返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。
int nextInt(int n) 返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
long nextLong() 返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 long 值。

使用的时候需要先创建一个对象再调用方法

@Test
public void test04(){
    Random r = new Random();
    System.out.println("随机整数:" + r.nextInt());  //左闭右开区间 [0~10)
    System.out.println("随机小数:" + r.nextDouble());
    System.out.println("随机布尔值:" + r.nextBoolean());
}

版权所有:伸手党盘
文章标题:常用System、Runtime、Math、BigInteger、BigDecimal、Random类API
文章链接:https://ssdpan.cn/?post=378
本站文章来源于网络搜集和原创,仅供学习使用,未经授权请勿用于任何商业用途,如有侵权及时联系删除

推荐阅读:


扫描二维码,在手机上阅读