Java中的System類、BigInteger類和BigDecimal類詳解
System類常見方法
1)exit()方法,退出當前程序;
2)arraycopy()方法,復制數組元素,比較適合底層調用,一般使用Arrays.copyOf()完成復制數組;
3)currentTimeMillis()方法,返回當前時間距離1970-1-1的毫秒數
4)gc()方法,運行垃圾回收機制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()方法,退出當前程序;
System.out.println("程序1");
//exit(0),表示程序正常退出
//0表示一個正常的狀態(tài)
//System.exit(0);
System.out.println("程序2");
//2)arraycopy()方法,復制數組元素,比較適合底層調用,
// 一般使用Arrays.copyOf()完成復制數組;
int[] src = {1,2,3};
int[] dest = new int[3]; //dest當前是{0,0,0}
System.arraycopy(src,0,dest,0,3);
// src – the source array. 源數組(被拷貝的數組)
// srcPos – starting position in the source array. 從源數組的指定索引位置開始拷貝(復制)信息
// dest – the destination array. 目標數組(將源數組數據拷貝到該數組中)
// destPos – starting position in the destination data. 從目標數組的指定索引位置開始拷貝(粘貼)信息
// length – the number of array elements to be copied. 從源數組拷貝數據的個數(長度)
System.out.println("dest="+ Arrays.toString(dest));
//3)currentTimeMillis()方法,返回當前時間距離1970-1-1的毫秒數
System.out.println(System.currentTimeMillis());
}
}BigInteger類和BigDecimal類應用場景
1)BigInteger適合保存比較大的整型;
2)BigDecimal適合保存精度更高的浮點型(小數)。
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);
//要以字符串形式傳進BigInteger的對象中
BigInteger bigInteger = new BigInteger("99999999999999999999999");
System.out.println(bigInteger);
//1.在對BigInteger進行加減乘除的時候,必須使用對應的方法,不能直接使用運算符(+、-、*、/)
//加法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進行運算,不能夠直接使用運算符(+、-、*、/)進行運算,
// 需要使用對應方法
//加法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異常
//在調用divide()方法時,指定精度即可,BigDecimal.ROUND_CEILING
//如果有無限循環(huán)小數,就會保留分子的精度
System.out.println(bigDecimal.divide(bigDecimal2,BigDecimal.ROUND_CEILING));
}
}到此這篇關于Java中的System類、BigInteger類和BigDecimal類詳解的文章就介紹到這了,更多相關System類、BigInteger類和BigDecimal類詳解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Java BigInteger類,BigDecimal類,Date類,DateFormat類及Calendar類用法示例
- JAVA基本類型包裝類 BigDecimal BigInteger 的使用
- Java你不了解的大數型BigInteger與BigDecimal類
- Java Big Number操作BigInteger及BigDecimal類詳解
- JAVA?biginteger類bigdecimal類的使用示例學習
- Java中BigInteger與BigDecimal類用法總結
- java數學類Math?BigInteger?BigDecimal使用介紹
- JavaAPI中BigInteger、BigDecimal的使用方法及應用
相關文章
springMVC+velocity實現仿Datatables局部刷新分頁方法
下面小編就為大家分享一篇springMVC+velocity實現仿Datatables局部刷新分頁方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02

