Java常用類之System類的使用指南
1.System類
System系統(tǒng)類,主要用于獲取系統(tǒng)的屬性數(shù)據(jù)和其他操作,因其構造方法是私有的并且類中的成員方法都是靜態(tài)的,所以在使用的時候不需要創(chuàng)建對象,可直接調(diào)用。
該類實現(xiàn)了一些關于系統(tǒng)功能的方法如下:
import java.util.Arrays;
/**
* System類演示
*/
public class SystemTest {
public static void main(String[] args) {
int[] src = {1, 2, 3};
int[] dest = new int[3];
/**
* 參數(shù)1:源數(shù)組
* 參數(shù)2:從源數(shù)組的那個位置開始拷貝
* 參數(shù)3:目標數(shù)組
* 參數(shù)4:把源數(shù)組的數(shù)據(jù)拷貝到目標數(shù)組的那個索引
* 參數(shù)5:從源數(shù)組拷貝多少的數(shù)到目標數(shù)組
*/
System.arraycopy(src, 0, dest, 0, 3);
System.out.println(Arrays.toString(dest));
// 返回當前時間距離1970年1月1日的毫秒數(shù)
System.out.println(System.currentTimeMillis());
// 調(diào)用垃圾回收機制
System.gc();
// 退出當前程序
// 0:程序正常退出
System.exit(0);
}
}
下面為大家補充一些System類的常用方法
1. arraycopy(…)方法
概述
arraycopy(…)方法將指定原數(shù)組中的數(shù)據(jù)從指定位置復制到目標數(shù)組的指定位置。
語法
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
src – 原數(shù)組。
srcPos – 在原數(shù)組中開始復制的位置。
dest – 目標數(shù)組。
destPos – 在目標數(shù)組中開始粘貼的位置。
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()); //打印現(xiàn)在的時間
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()方法用來運行垃圾回收器。(至于是否回收垃圾,有可能執(zhí)行,有可能不執(zhí)行,是否執(zhí)行取決于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虛擬機。如果參數(shù)是0,表示正常退出JVM;如果參數(shù)非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); //因為此處已經(jīng)終止當前運行的Java虛擬機,故不會執(zhí)行之后的代碼
System.out.println("程序結束了");
}
}
2.BigInteger
該類實現(xiàn)了大整數(shù)的運算:
// 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
該類實現(xiàn)了超高精度的浮點數(shù)運算:
// 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);
// /,注意:如果不指定精度,結果是死循環(huán)小數(shù),會拋出一個異常
BigDecimal ddivRes = bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING);
System.out.println(ddivRes);到此這篇關于Java常用類之System類的使用指南的文章就介紹到這了,更多相關Java System類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java使用fill()數(shù)組填充的實現(xiàn)
這篇文章主要介紹了Java使用fill()數(shù)組填充的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
淺談java中math類中三種取整函數(shù)的區(qū)別
下面小編就為大家?guī)硪黄獪\談java中math類中三種取整函數(shù)的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
idea的easyCode的 MybatisPlus模板的配置詳解
這篇文章主要介紹了idea的easyCode的 MybatisPlus模板的配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
SpringBoot ApplicationListener事件監(jiān)聽接口使用問題探究
這篇文章主要介紹了SpringBoot ApplicationListener事件監(jiān)聽接口使用問題,自定義監(jiān)聽器需要實現(xiàn)ApplicationListener接口,實現(xiàn)對應的方法來完成自己的業(yè)務邏輯。SpringBoot Application共支持6種事件監(jiān)聽2023-04-04

