java的Math类,System类,Runtime类和BigDemical类常用的API

本文详细介绍了JavaMath类中的各种数学函数,如绝对值、取整、四舍五入等,以及System类和Runtime类提供的系统资源管理和操作功能,如时间获取、处理器数量、内存管理等。同时,还提到了BigDecimal类用于处理浮点型运算精度问题。
摘要由CSDN通过智能技术生成

Math类:
代表数学,是一个工具类;

public static int abs(int a)

Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

取绝对值

public class test {
    public static void main(String[] args) {
        System.out.println(Math.abs(-23));//23
        System.out.println(Math.abs(-4.3));//4.3
        System.out.println(Math.abs(2));//2
    }
}

 public static double ceil(double a)

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer。

向上取整

         System.out.println(Math.ceil(4.0001));//5.0
         System.out.println(Math.ceil(3.0));//3.0

public static double floor(double a)

Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. 

向下取整

System.out.println(Math.floor(3.999));//3.0

 public static long round(double a)

Returns the closest long to the argument, with ties rounding to positive infinity.

四舍五入 

System.out.println(Math.round(3.56));//4

 public static int max(int a, int b)

Returns the greater of two int values.

取最大值

public static int min(int a, int b) 

 Returns the smaller of two int values.

取最小值

public static double pow(double a, double b) 

幂运算

  System.out.println(Math.pow(2,3));//8.0

 public static double random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0

返回0.0到1.0的数,不包括1.0 

 

System.out.println(Math.random());//0.8425849812213428

System类:代表程序所在的系统,也是一个工具类

public static void exit(int status)

Initiates the shutdown sequence of the Java Virtual Machine. 

终止当前运行的JVM虚拟机

status非零表示异常终止

public static long currentTimeMillis()

Returns the current time in milliseconds. 

获取当前系统的时间,返回的是毫秒值,返回的是从1970-1-1 0:0:0开始到现在的毫秒值。1s=1000ms 

Runtime类:代表程序所在的运行环境

Runtime是一个单例类 

public void exit(int status) 

Initiates the shutdown sequence of the Java Virtual Machine. 

public int availableProcessors() 

Returns the number of processors available to the Java virtual machine.

获取虚拟机能够使用的处理器数 

 

public class test {
    public static void main(String[] args) {
        Runtime r=Runtime.getRuntime();
        System.out.println(r.availableProcessors());//16
    }
}

public long totalMemory()

Returns the total amount of memory in the Java virtual machine.

返回虚拟机的内存总量

System.out.println(r.totalMemory()/1024.0+"KB");//1024=1KB
        System.out.println(r.totalMemory()/1024.0/1024.0+"MB");//252.0MB
//freeMemory()返回虚拟机的可用内存量
        System.out.println(r.freeMemory()/1024.0/1024.0+"MB");//246.67790985107422MB

 public Process exec(String command) throws IOException

启动某个程序,并返回代表程序的对象

 

 

 Process p=r.exec("QQ");
        Thread.sleep(4000);//让当前程序运行4s
        p.destroy();//销毁程序

 BigDecimal类:用于解决浮点型运算时,出现结果失真的问题

 public BigDecimal(String val)

Translates the string representation of a BigDecimal into a BigDecimal.

把字符串变成BigDecimal类型 

 

public class test {
    public static void main(String[] args) {
        double a=0.1;
        double b=0.2;

        //1:把他们变成字符串再变成Bigdecimal类型
        /*BigDecimal a1=new BigDecimal(Double.toString(a));
        BigDecimal b1=new BigDecimal(Double.toString(b));*/
        BigDecimal a1=BigDecimal.valueOf(a);//在底层调用了上面的构造器,更简洁
        BigDecimal b1=BigDecimal.valueOf(b);

        BigDecimal c1=a1.add(b1);
        System.out.println(c1.toString());//0.3
        BigDecimal c2=a1.subtract(b1);//减法
        System.out.println(c2);//-0.1
        BigDecimal c3=a1.multiply(b1);//乘法
        System.out.println(c3);//0.02//
        BigDecimal c4=a1.divide(b1);//除法
        System.out.println(c4);//0.5
        

        BigDecimal i=BigDecimal.valueOf(0.1);
        BigDecimal j=BigDecimal.valueOf(0.3);
//        BigDecimal k=i.divide(j);//error,无限循环小数
        BigDecimal k=i.divide(j,2,RoundingMode.HALF_UP);//两位小数,以四舍五入的方式
        System.out.println(k);//0.33

        double x=k.doubleValue();//将BigDecimal类型变成double类型
        System.out.println(k);
    }
}

  • 30
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落落落sss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值