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

作者:世界尽头与你 时间:2021-09-20 07:54:46 

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+" ");
       }
   }
}

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

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));
   }
}

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

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();
   }
}

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

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("程序结束了");
   }
}

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

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);

来源:https://blog.csdn.net/Gherbirthday0916/article/details/125882743

标签:Java,System,类
0
投稿

猜你喜欢

  • mybatis中resultMap 标签的使用教程

    2022-01-15 11:19:42
  • SpringBoot集成Tomcat服务架构配置

    2022-06-30 09:10:11
  • 浅析java中next与nextLine用法对比

    2022-01-11 01:02:53
  • springboot项目配置多个kafka的示例代码

    2023-11-23 23:15:29
  • Spring Boot 实现图片上传并回显功能

    2021-10-11 17:45:20
  • SpringBoot选择自有bean优先加载实现方法

    2023-05-21 06:22:39
  • Springboot整合redis实现发布订阅功能介绍步骤

    2021-11-23 16:42:26
  • Java使用Scanner类进行控制台输入实现方法

    2021-08-08 18:34:26
  • Android开发之ListView列表刷新和加载更多实现方法

    2021-06-20 06:28:30
  • 解析C#拼接Json串的几种方法

    2021-12-28 20:22:48
  • Java实现在Word指定位置插入分页符

    2021-06-29 03:24:44
  • C#使用iTextSharp封装的PDF文件操作类实例

    2023-05-16 22:26:52
  • C#语音识别用法实例

    2021-09-11 01:16:47
  • mybatis自定义类型处理器TypehHandler示例详解

    2023-10-11 04:30:40
  • RabbitMQ延迟队列及消息延迟推送实现详解

    2023-11-15 15:31:31
  • C#生成Word文件(图片、文字)

    2023-03-28 04:07:58
  • Java BufferWriter写文件写不进去或缺失数据的解决

    2023-07-20 14:57:02
  • android 获取上一个activity返回值的方法

    2023-06-15 13:22:22
  • Java中匿名类的两种实现方式

    2022-06-17 23:57:05
  • VsCode使用EmmyLua插件调试Unity工程Lua代码的详细步骤

    2022-12-25 14:13:25
  • asp之家 软件编程 m.aspxhome.com