进阶JAVA篇- Math 类和 System 类、 Runtime 类的常用API(三)

目录

API      

        1.0 Math 类的说明

        1.1 Math 类中的 abs() 方法

        1.2 Math 类中的 ceil () 方法

        1.3 Math 类中的 floor () 方法

        1.4 Math 类中的 round () 方法

        1.5 Math 类中的 max() 和 min() 方法

        1.6 Math 类中的 pow(double a , double b) 方法

        1.7 Math 类中的 random() 方法

        2.0 System 类的说明

        2.1 System 类中的 exit() 方法

        2.2 System 类中的 currentTimeMillis() 方法

        3.0 Runtime 类的说明

        3.1 Runtime 类中的 getRuntime() 方法

        3.2 Runtime 类中的 exit() 方法

        3.3 Runtime 类中的 availableProcessors() 方法

        3.4 Runtime 类中的 totalMemory() 方法

        3.5 Runtime 类中的 freeMemory() 方法

        3.6 Runtime 类中的 exec(String command) 方法


API      

        1.0 Math 类的说明

         代表数学,是一个工具类,里面提供的都是对数据进行操作的一些静态方法,属于类方法,直接用类名去调用方法。

        1.1 Math 类中的 abs() 方法

        获取传入的数据的绝对值,传入的数据类型可以是 int ,double ,long,float 类型。

代码如下:

public class MathMethod {
    public static void main(String[] args) {
        int a = Math.abs(-12);
        double b = Math.abs(-12.32);
        long c = Math.abs(-123333);
        float d = Math.abs(-12.4f);

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}

运行结果如下:

         1.2 Math 类中的 ceil () 方法

        向上取整,返回的是浮点型。

代码如下:

public class MathMethod {
    public static void main(String[] args) {

        //向上取整
        double a = Math.ceil(2.0000001);
        System.out.println(a);

    }

运行结果如下:

         1.3 Math 类中的 floor () 方法

        向下取整,返回的是浮点数类型。

代码如下:

public class MathMethod {
    public static void main(String[] args) {

        //向下取整
        double a = Math.floor(2.9999);
        System.out.println(a);
    }

运行结果如下:

        1.4 Math 类中的 round () 方法

        四舍五入,传进去的是浮点型,返回出来的是整型或者长整型。

代码如下:

public class MathMethod {
    public static void main(String[] args) {
        //四舍五入
        int a = Math.round(1.455f);
        long b = Math.round(1.5000);

        System.out.println(a);
        System.out.println(b);
    }

运行结果如下:

        1.5 Math 类中的 max()min() 方法

        比较两个数的大小,max() 方法返回大的值,min() 方法返回小的值。

代码如下:

public class MathMethod {
    public static void main(String[] args) {
        double a = 1.2;
        double b = 1.3;
        int c = 1;
        long d = 12222;

        System.out.println(Math.max(a,b));
        System.out.println(Math.max(a,c));
        System.out.println(Math.max(c,d));

        System.out.println(Math.min(a,d));
        System.out.println(Math.min(a,c));
        System.out.println(Math.min(a,d));

    }

运行结果如下:

        1.6 Math 类中的 pow(double a , double b) 方法

        返回a的b次幂的浮点类型的值。

代码如下:

public class MathMethod {
    public static void main(String[] args) {

        int a = 2;
        int b = 3;
        double c = Math.pow(a,b);//输入整型,会在方法内部转化成浮点型的
        System.out.println(c);

    }

运行结果如下:

        1.7 Math 类中的 random() 方法

        随机返回 double 类型的数值,范围为 [0,1)

代码如下:

public class MathMethod {
    public static void main(String[] args) {

        double a = Math.random();
        System.out.println(a);

    }

运行结果如下:

        2.0 System 类的说明

        System 代表程序所在的系统,也是一个工具类,提供了一些静态方法,直接用类名去调用方法。

        2.1 System 类中的 exit() 方法

        终止运行当前的java虚拟机。

代码如下:

public class SystemMethod {
    public static void main(String[] args) {
        System.exit(0);
        System.out.println("运行这行代码");
    }
}

运行结果:

        2.2 System 类中的 currentTimeMillis() 方法

        返回当前系统的时间毫秒值形式,用 long 类型来接收。时间毫秒值是指从某个特定的起始时间点(通常是1970年1月1日00:00:00 UTC)到当前时间的总毫秒数。它用于表示时间的精确度,比如在计算机编程中常用于记录和比较时间。时间毫秒值可以用于计算时间间隔、时间戳转换以及其他与时间相关的操作。

代码如下:

public class SystemMethod {
    public static void main(String[] args) {

        long a = System.currentTimeMillis();
        System.out.println(a);

        for (int i = 0; i < 100000; i++) {
            System.out.println(1);
        }
        long b = System.currentTimeMillis();

        System.out.println("该程序运行了 " + (b-a)/1000.0 + "s");
    }

运行结果为:

        3.0 Runtime 类的说明

        代表程序所在的运行环境,Runtime 是一个单例类。

分析 Runtime 类:

        

public class Runtime {
    private static Runtime currentRuntime = new Runtime();
    //先是在类中创建一个Runtime的对象,被静态变量引用且这个变量是不可以通过类名在类外访问的
    //因为这个变量被private修饰,只能在类中获取
    //所以这个变量只有一个,被称为单例类


    public static Runtime getRuntime() {
        return currentRuntime;
    }
    //在类外通过调用这个静态方法,返回该类的对象

    private Runtime() {}
    //构造方法被private修饰了,只能在类中使用,在类外是不能创建对象了。

}

        3.1 Runtime 类中的 getRuntime() 方法

        获取 Runtime 类的对象。

代码如下:

public class SystemMethod {
    public static void main(String[] args) {

        Runtime runtime = Runtime.getRuntime();
        System.out.println(runtime);
    }

运行结果:

        3.2 Runtime 类中的 exit() 方法

        终止当前运行的虚拟机。

代码如下:

public class SystemMethod {
    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        runtime.exit(0);
        System.out.println("执行当前代码");
    }

运行结果:

        3.3 Runtime 类中的 availableProcessors() 方法

        返回 java 虚拟机可用的处理器数。

代码如下:

public class SystemMethod {
    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        int a = runtime.availableProcessors();
        System.out.println(a);
        //我自己这台有12个处理器,所以输出12
    }

运行结果:

        3.4 Runtime 类中的 totalMemory() 方法

        返回java虚拟机中内存总量,返回来的是字节数,用 long 类型来接收。

代码如下:

public class SystemMethod {
    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        long i = runtime.totalMemory();
        System.out.println(i/1024/1024.0+"mb");
    }

运行结果如下:

         3.5 Runtime 类中的 freeMemory() 方法

        返回java虚拟机可用内存,返回来的是字节数,用 long 类型来接收。

代码如下:

public class SystemMethod {
    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        long i = runtime.totalMemory();
        System.out.println(i/1024/1024.0+"MB");

        long a = runtime.freeMemory();
        System.out.println(a / 1024 / 1024.0 + "MB");

    }

运行结果如下:

        3.6 Runtime 类中的 exec(String command) 方法

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

代码如下:

import java.io.IOException;

public class SystemMethod {
    public static void main(String[] args) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("D:\\WeChat\\WeChat.exe");
    }

        小结:本篇都是比较简单的API,看懂且多用就达到目标了。


猜你喜欢

转载自blog.csdn.net/Tingfeng__/article/details/133803368