Java中的System類、BigInteger類和BigDecimal類詳解
System類常見方法
1)exit()方法,退出當(dāng)前程序;
2)arraycopy()方法,復(fù)制數(shù)組元素,比較適合底層調(diào)用,一般使用Arrays.copyOf()完成復(fù)制數(shù)組;
3)currentTimeMillis()方法,返回當(dāng)前時(shí)間距離1970-1-1的毫秒數(shù)
4)gc()方法,運(yùn)行垃圾回收機(jī)制System.gc();
package com.pero.system_; import java.util.Arrays; /** * @author Pero * @version 1.0 */ public class System_ { public static void main(String[] args) { //exit()方法,退出當(dāng)前程序; System.out.println("程序1"); //exit(0),表示程序正常退出 //0表示一個(gè)正常的狀態(tài) //System.exit(0); System.out.println("程序2"); //2)arraycopy()方法,復(fù)制數(shù)組元素,比較適合底層調(diào)用, // 一般使用Arrays.copyOf()完成復(fù)制數(shù)組; int[] src = {1,2,3}; int[] dest = new int[3]; //dest當(dāng)前是{0,0,0} System.arraycopy(src,0,dest,0,3); // src – the source array. 源數(shù)組(被拷貝的數(shù)組) // srcPos – starting position in the source array. 從源數(shù)組的指定索引位置開始拷貝(復(fù)制)信息 // dest – the destination array. 目標(biāo)數(shù)組(將源數(shù)組數(shù)據(jù)拷貝到該數(shù)組中) // destPos – starting position in the destination data. 從目標(biāo)數(shù)組的指定索引位置開始拷貝(粘貼)信息 // length – the number of array elements to be copied. 從源數(shù)組拷貝數(shù)據(jù)的個(gè)數(shù)(長(zhǎng)度) System.out.println("dest="+ Arrays.toString(dest)); //3)currentTimeMillis()方法,返回當(dāng)前時(shí)間距離1970-1-1的毫秒數(shù) System.out.println(System.currentTimeMillis()); } }
BigInteger類和BigDecimal類應(yīng)用場(chǎng)景
1)BigInteger適合保存比較大的整型;
2)BigDecimal適合保存精度更高的浮點(diǎn)型(小數(shù))。
BigInteger常用方法
package com.pero.bignum_; import java.math.BigInteger; /** * @author Pero * @version 1.0 */ public class BigInteger_ { public static void main(String[] args) { long longNumber = 1000000000000000001l; System.out.println(longNumber); //要以字符串形式傳進(jìn)BigInteger的對(duì)象中 BigInteger bigInteger = new BigInteger("99999999999999999999999"); System.out.println(bigInteger); //1.在對(duì)BigInteger進(jìn)行加減乘除的時(shí)候,必須使用對(duì)應(yīng)的方法,不能直接使用運(yùn)算符(+、-、*、/) //加法add() BigInteger bigInteger1 = BigInteger.valueOf(longNumber); System.out.println(bigInteger1); BigInteger bigInteger2 = bigInteger.add(bigInteger1); System.out.println(bigInteger2); //減法subtract() BigInteger subtract = bigInteger2.subtract(bigInteger); System.out.println(subtract); //乘法multiply() BigInteger multiply = bigInteger1.multiply(bigInteger2); System.out.println(multiply); //除法divide() BigInteger divide = multiply.divide(bigInteger2); System.out.println(divide); } }
BigDecimal常用方法
package com.pero.bignum_; import java.math.BigDecimal; /** * @author Pero * @version 1.0 */ public class BigDecimal_ { public static void main(String[] args) { double d = 19999999.199999999999999d; System.out.println(d); BigDecimal bigDecimal = new BigDecimal("19999999.19999999999999999999"); System.out.println(bigDecimal); //1.如果對(duì)BigDecimal進(jìn)行運(yùn)算,不能夠直接使用運(yùn)算符(+、-、*、/)進(jìn)行運(yùn)算, // 需要使用對(duì)應(yīng)方法 //加法add() BigDecimal bigDecimal1 = new BigDecimal("0.00000000000000000001"); System.out.println(bigDecimal.add(bigDecimal1)); //減法subtract() BigDecimal bigDecimal2 = new BigDecimal("19999999.09999999999999999999"); System.out.println(bigDecimal.subtract(bigDecimal2)); //乘法multiply() System.out.println(bigDecimal.multiply(bigDecimal2)); //除法divide() //System.out.println(bigDecimal.divide(bigDecimal2)); //注意:可能存在無法除盡的情況,拋出ArithmeticException異常 //在調(diào)用divide()方法時(shí),指定精度即可,BigDecimal.ROUND_CEILING //如果有無限循環(huán)小數(shù),就會(huì)保留分子的精度 System.out.println(bigDecimal.divide(bigDecimal2,BigDecimal.ROUND_CEILING)); } }
到此這篇關(guān)于Java中的System類、BigInteger類和BigDecimal類詳解的文章就介紹到這了,更多相關(guān)System類、BigInteger類和BigDecimal類詳解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java BigInteger類,BigDecimal類,Date類,DateFormat類及Calendar類用法示例
- JAVA基本類型包裝類 BigDecimal BigInteger 的使用
- Java你不了解的大數(shù)型BigInteger與BigDecimal類
- Java Big Number操作BigInteger及BigDecimal類詳解
- JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí)
- Java中BigInteger與BigDecimal類用法總結(jié)
- java數(shù)學(xué)類Math?BigInteger?BigDecimal使用介紹
- JavaAPI中BigInteger、BigDecimal的使用方法及應(yīng)用
相關(guān)文章
SpringBoot實(shí)現(xiàn)圖片識(shí)別文字的四種方式小結(jié)
本文主要介紹了SpringBoot實(shí)現(xiàn)圖片識(shí)別文字的四種方式,包括Tess4J,百度智能云,阿里云,騰訊云這四種,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02springMVC+velocity實(shí)現(xiàn)仿Datatables局部刷新分頁方法
下面小編就為大家分享一篇springMVC+velocity實(shí)現(xiàn)仿Datatables局部刷新分頁方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02提升java開發(fā)效率工具lombok使用爭(zhēng)議
這篇文章主要介紹了提升java開發(fā)效率工具lombok使用爭(zhēng)議到底該不該使用的分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07java如何實(shí)現(xiàn)遞歸刪除樹形數(shù)據(jù)的任一個(gè)節(jié)點(diǎn)
文章講述了在Java中實(shí)現(xiàn)遞歸刪除樹形數(shù)據(jù)的任一個(gè)節(jié)點(diǎn)時(shí)需要注意的三個(gè)點(diǎn),包括刪除的節(jié)點(diǎn)包含子節(jié)點(diǎn)、刪除子節(jié)點(diǎn)和其他子節(jié)點(diǎn)刪除的節(jié)點(diǎn)不包含子節(jié)點(diǎn)、以及該父節(jié)點(diǎn)變成葉子節(jié)點(diǎn),此外,文章還提到這兩件事包含在同一件事務(wù)中2024-12-12淺談Java中浮點(diǎn)型數(shù)據(jù)保留兩位小數(shù)的四種方法
今天在進(jìn)行開發(fā)的過程中遇到了一個(gè)小問題,是關(guān)于如何將double類型的數(shù)據(jù)保留兩位小數(shù)。具有一定的參考價(jià)值,本文就詳細(xì)的介紹一下2021-09-09Springboot文件上傳功能的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot文件上傳功能的實(shí)現(xiàn),文中通過代碼示例介紹的非常詳細(xì),具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們可以參考閱讀2023-04-04Jmeter自定義函數(shù)base64加密實(shí)現(xiàn)過程解析
這篇文章主要介紹了Jmeter自定義函數(shù)base64加密實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07擴(kuò)展tk.mybatis的流式查詢功能實(shí)現(xiàn)
mybatis查詢默認(rèn)是一次獲取全部,如果數(shù)據(jù)過于龐大,就會(huì)導(dǎo)致OOM問題,本文就介紹了tk.mybatis 流式查詢,具有一定的參考價(jià)值,感興趣的可以了解一下2021-12-12