软件编程
位置:首页>> 软件编程>> java编程>> Java常用类之System类的使用指南

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

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

标签:Java,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+" ");
       }
   }
}

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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com