Java常用类之System类的使用指南

 更新时间:2022年07月20日 10:33:11   作者:世界尽头与你  
System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。该类位于java.lang包。本文将通过示例为大家详细讲讲System类的使用,需要的可以参考一下

1.System类

System系统类,主要用于获取系统的属性数据和其他操作,因其构造方法是私有的并且类中的成员方法都是静态的,所以在使用的时候不需要创建对象,可直接调用。

该类实现了一些关于系统功能的方法如下:

import java.util.Arrays;

/**
 * System类演示
 */
public class SystemTest {
    public static void main(String[] args) {
        int[] src = {1, 2, 3};
        int[] dest = new int[3];
        /**
         * 参数1:源数组
         * 参数2:从源数组的那个位置开始拷贝
         * 参数3:目标数组
         * 参数4:把源数组的数据拷贝到目标数组的那个索引
         * 参数5:从源数组拷贝多少的数到目标数组
         */
        System.arraycopy(src, 0, dest, 0, 3);
        System.out.println(Arrays.toString(dest));
        // 返回当前时间距离1970年1月1日的毫秒数
        System.out.println(System.currentTimeMillis());
        // 调用垃圾回收机制
        System.gc();
        // 退出当前程序
        // 0:程序正常退出
        System.exit(0);
    }
}

下面为大家补充一些System类的常用方法

1. arraycopy(…)方法

概述

arraycopy(…)方法将指定原数组中的数据从指定位置复制到目标数组的指定位置。

语法

static void arraycopy(Object src,  int srcPos, Object dest, int destPos, int length)

src – 原数组。

srcPos – 在原数组中开始复制的位置。

dest – 目标数组。

destPos – 在目标数组中开始粘贴的位置。

length – 复制的长度。

举例

package com.ibelifly.commonclass.system;

public class Test1 {
    public static void main(String[] args) {
        int[] arr={23,45,20,67,57,34,98,95};
        int[] dest=new int[8];
        System.arraycopy(arr,4,dest,4,4);
        for (int x:dest) {
            System.out.print(x+" ");
        }
    }
}

2. currentTimeMillis()方法

概述

currentTimeMillis()方法返回当前时间(以毫秒为单位)。

语法

static long currentTimeMillis()

举例

package com.ibelifly.commonclass.system;

public class Test2 {
    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis()); //打印现在的时间
        long start=System.currentTimeMillis(); //该方法可用来计时
        for (int i = -99999999; i < 99999999; i++) {
            for (int j = -99999999; j < 99999999; j++) {
                int result=i+j;
            }
        }
        long end=System.currentTimeMillis();
        System.out.println("用时:"+(end-start));
    }
}

3. gc()方法

概述

gc()方法用来运行垃圾回收器。(至于是否回收垃圾,有可能执行,有可能不执行,是否执行取决于JVM)

语法

static void gc();

举例

学生类:

package com.ibelifly.commonclass.system;

public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("回收了"+name+" "+age);
    }
}

测试类:

package com.ibelifly.commonclass.system;

public class Test3 {
    public static void main(String[] args) {
        new Student("小明",20);
        new Student("小红",28);
        new Student("小刚",22);
        System.gc();
    }
}

4. exit(int status)方法

概述

exit(int status)方法用于终止当前运行的Java虚拟机。如果参数是0,表示正常退出JVM;如果参数非0,表示异常退出JVM。

语法

static void exit(int status);

举例

package com.ibelifly.commonclass.system;

​​​​​​​public class Test4 {
    public static void main(String[] args) {
        System.out.println("程序开始了");
        System.exit(0); //因为此处已经终止当前运行的Java虚拟机,故不会执行之后的代码
        System.out.println("程序结束了");
    }
}

2.BigInteger

该类实现了大整数的运算:

// BigInteger
BigInteger bigInteger = new BigInteger("11111111111111111111111");
BigInteger bigInteger1 = new BigInteger("12345678910");
// +
BigInteger addRes = bigInteger.add(bigInteger1);
System.out.println(addRes);
// -
BigInteger subRes = bigInteger.subtract(bigInteger1);
System.out.println(subRes);
// *
BigInteger mulRes = bigInteger.multiply(bigInteger1);
System.out.println(mulRes);
// /
BigInteger divRes = bigInteger.divide(bigInteger1);
System.out.println(divRes);

3.BigDecimal

该类实现了超高精度的浮点数运算:

// BigDecimal
BigDecimal bigDecimal = new BigDecimal("1.1111111111111111111999999999999");
BigDecimal bigDecimal1 = new BigDecimal("1.234567");
System.out.println(bigDecimal);
// +
BigDecimal daddRes = bigDecimal.add(bigDecimal1);
System.out.println(daddRes);
// -
BigDecimal dsubRes = bigDecimal.subtract(bigDecimal1);
System.out.println(dsubRes);
// *
BigDecimal dmulRes = bigDecimal.multiply(bigDecimal1);
System.out.println(dmulRes);
// /,注意:如果不指定精度,结果是死循环小数,会抛出一个异常
BigDecimal ddivRes = bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING);
System.out.println(ddivRes);

到此这篇关于Java常用类之System类的使用指南的文章就介绍到这了,更多相关Java System类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解redis与spring的整合(使用缓存)

    详解redis与spring的整合(使用缓存)

    本篇文章主要介绍了redis与spring的整合(使用缓存),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • 使用Java实现Redis限流的方法

    使用Java实现Redis限流的方法

    限流的作用是防止某个段时间段内的请求数过多,造成模块因高并发而不可用。这篇文章给大家介绍使用Java实现Redis限流的相关知识,一起看看吧
    2021-09-09
  • 详解springboot中各个版本的redis配置问题

    详解springboot中各个版本的redis配置问题

    这篇文章主要介绍了详解springboot中各个版本的redis配置问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Java Redis Redisson配置教程详解

    Java Redis Redisson配置教程详解

    这篇文章主要介绍了Java Redis Redisson配置教程,包括Session共享配置及其他Redisson的Config配置方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • Java使用fill()数组填充的实现

    Java使用fill()数组填充的实现

    这篇文章主要介绍了Java使用fill()数组填充的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • 浅谈java中math类中三种取整函数的区别

    浅谈java中math类中三种取整函数的区别

    下面小编就为大家带来一篇浅谈java中math类中三种取整函数的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • idea的easyCode的 MybatisPlus模板的配置详解

    idea的easyCode的 MybatisPlus模板的配置详解

    这篇文章主要介绍了idea的easyCode的 MybatisPlus模板的配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Idea中maven项目实现登录验证码功能

    Idea中maven项目实现登录验证码功能

    这篇文章主要介绍了Idea中maven项目实现登录验证码功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • 如何使用Spring Boot设置上传文件大小限制

    如何使用Spring Boot设置上传文件大小限制

    上传文件是互联网中常应用的场景之一,最典型的情况就是上传头像等,下面这篇文章主要给大家介绍了关于如何使用Spring Boot设置上传文件大小限制的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • SpringBoot ApplicationListener事件监听接口使用问题探究

    SpringBoot ApplicationListener事件监听接口使用问题探究

    这篇文章主要介绍了SpringBoot ApplicationListener事件监听接口使用问题,自定义监听器需要实现ApplicationListener接口,实现对应的方法来完成自己的业务逻辑。SpringBoot Application共支持6种事件监听
    2023-04-04

最新评论