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ù)(長度)
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)用場景
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的對象中
BigInteger bigInteger = new BigInteger("99999999999999999999999");
System.out.println(bigInteger);
//1.在對BigInteger進(jìn)行加減乘除的時(shí)候,必須使用對應(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.如果對BigDecimal進(jìn)行運(yùn)算,不能夠直接使用運(yùn)算符(+、-、*、/)進(jìn)行運(yùn)算,
// 需要使用對應(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)容請搜索腳本之家以前的文章或繼續(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)文章
Java 覆蓋equals時(shí)總要覆蓋hashcode
這篇文章主要介紹了Java 覆蓋equals時(shí)總要覆蓋hashcode的相關(guān)資料,這里附有實(shí)例代碼,具有參考價(jià)值,需要的朋友可以參考下2016-12-12
Java線程協(xié)調(diào)運(yùn)行操作實(shí)例詳解
這篇文章主要介紹了Java線程協(xié)調(diào)運(yùn)行操作,結(jié)合具體實(shí)例形式詳細(xì)分析了Java線程協(xié)調(diào)運(yùn)行原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
MyBatis-Plus實(shí)現(xiàn)對查詢結(jié)果進(jìn)行分頁的基本步驟
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生,MyBatis-Plus 支持多種數(shù)據(jù)庫的分頁查詢,其分頁功能是通過 Page 類實(shí)現(xiàn)的,本文介紹了使用 MyBatis-Plus 實(shí)現(xiàn)分頁查詢的基本步驟,需要的朋友可以參考下2024-08-08
解決springboot報(bào)錯(cuò)找不到自動(dòng)注入的service問題
這篇文章主要介紹了解決springboot報(bào)錯(cuò)找不到自動(dòng)注入的service問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
scala+redis實(shí)現(xiàn)分布式鎖的示例代碼
這篇文章主要介紹了scala+redis實(shí)現(xiàn)分布式鎖的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
MyBatisPlus PaginationInterceptor分頁插件的使用詳解
這篇文章主要介紹了MyBatisPlus PaginationInterceptor分頁插件的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

