Java常用類之System類的使用指南
1.System類
System系統(tǒng)類,主要用于獲取系統(tǒng)的屬性數(shù)據(jù)和其他操作,因其構(gòu)造方法是私有的并且類中的成員方法都是靜態(tài)的,所以在使用的時(shí)候不需要?jiǎng)?chuàng)建對(duì)象,可直接調(diào)用。
該類實(shí)現(xiàn)了一些關(guā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ù)組的那個(gè)位置開(kāi)始拷貝 * 參數(shù)3:目標(biāo)數(shù)組 * 參數(shù)4:把源數(shù)組的數(shù)據(jù)拷貝到目標(biāo)數(shù)組的那個(gè)索引 * 參數(shù)5:從源數(shù)組拷貝多少的數(shù)到目標(biāo)數(shù)組 */ System.arraycopy(src, 0, dest, 0, 3); System.out.println(Arrays.toString(dest)); // 返回當(dāng)前時(shí)間距離1970年1月1日的毫秒數(shù) System.out.println(System.currentTimeMillis()); // 調(diào)用垃圾回收機(jī)制 System.gc(); // 退出當(dāng)前程序 // 0:程序正常退出 System.exit(0); } }
下面為大家補(bǔ)充一些System類的常用方法
1. arraycopy(…)方法
概述
arraycopy(…)方法將指定原數(shù)組中的數(shù)據(jù)從指定位置復(fù)制到目標(biāo)數(shù)組的指定位置。
語(yǔ)法
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
src – 原數(shù)組。
srcPos – 在原數(shù)組中開(kāi)始復(fù)制的位置。
dest – 目標(biāo)數(shù)組。
destPos – 在目標(biāo)數(shù)組中開(kāi)始粘貼的位置。
length – 復(fù)制的長(zhǎng)度。
舉例
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()方法返回當(dāng)前時(shí)間(以毫秒為單位)。
語(yǔ)法
static long currentTimeMillis()
舉例
package com.ibelifly.commonclass.system; public class Test2 { public static void main(String[] args) { System.out.println(System.currentTimeMillis()); //打印現(xiàn)在的時(shí)間 long start=System.currentTimeMillis(); //該方法可用來(lái)計(jì)時(shí) 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("用時(shí):"+(end-start)); } }
3. gc()方法
概述
gc()方法用來(lái)運(yùn)行垃圾回收器。(至于是否回收垃圾,有可能執(zhí)行,有可能不執(zhí)行,是否執(zhí)行取決于JVM)
語(yǔ)法
static void gc();
舉例
學(xué)生類:
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); } }
測(cè)試類:
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)方法用于終止當(dāng)前運(yùn)行的Java虛擬機(jī)。如果參數(shù)是0,表示正常退出JVM;如果參數(shù)非0,表示異常退出JVM。
語(yǔ)法
static void exit(int status);
舉例
package com.ibelifly.commonclass.system; ???????public class Test4 { public static void main(String[] args) { System.out.println("程序開(kāi)始了"); System.exit(0); //因?yàn)榇颂幰呀?jīng)終止當(dāng)前運(yùn)行的Java虛擬機(jī),故不會(huì)執(zhí)行之后的代碼 System.out.println("程序結(jié)束了"); } }
2.BigInteger
該類實(shí)現(xiàn)了大整數(shù)的運(yùn)算:
// 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
該類實(shí)現(xiàn)了超高精度的浮點(diǎn)數(shù)運(yùn)算:
// 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); // /,注意:如果不指定精度,結(jié)果是死循環(huán)小數(shù),會(huì)拋出一個(gè)異常 BigDecimal ddivRes = bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING); System.out.println(ddivRes);
到此這篇關(guān)于Java常用類之System類的使用指南的文章就介紹到這了,更多相關(guān)Java System類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解springboot中各個(gè)版本的redis配置問(wèn)題
這篇文章主要介紹了詳解springboot中各個(gè)版本的redis配置問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Java使用fill()數(shù)組填充的實(shí)現(xiàn)
這篇文章主要介紹了Java使用fill()數(shù)組填充的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01淺談java中math類中三種取整函數(shù)的區(qū)別
下面小編就為大家?guī)?lái)一篇淺談java中math類中三種取整函數(shù)的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11idea的easyCode的 MybatisPlus模板的配置詳解
這篇文章主要介紹了idea的easyCode的 MybatisPlus模板的配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Idea中maven項(xiàng)目實(shí)現(xiàn)登錄驗(yàn)證碼功能
這篇文章主要介紹了Idea中maven項(xiàng)目實(shí)現(xiàn)登錄驗(yàn)證碼功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12如何使用Spring?Boot設(shè)置上傳文件大小限制
上傳文件是互聯(lián)網(wǎng)中常應(yīng)用的場(chǎng)景之一,最典型的情況就是上傳頭像等,下面這篇文章主要給大家介紹了關(guān)于如何使用Spring?Boot設(shè)置上傳文件大小限制的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01SpringBoot ApplicationListener事件監(jiān)聽(tīng)接口使用問(wèn)題探究
這篇文章主要介紹了SpringBoot ApplicationListener事件監(jiān)聽(tīng)接口使用問(wèn)題,自定義監(jiān)聽(tīng)器需要實(shí)現(xiàn)ApplicationListener接口,實(shí)現(xiàn)對(duì)應(yīng)的方法來(lái)完成自己的業(yè)務(wù)邏輯。SpringBoot Application共支持6種事件監(jiān)聽(tīng)2023-04-04