欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中的System類、BigInteger類和BigDecimal類詳解

 更新時(shí)間:2023年09月21日 09:01:01   作者:Neo丶  
這篇文章主要介紹了Java中的System類、BigInteger類和BigDecimal類詳解,arraycopy()方法,復(fù)制數(shù)組元素,比較適合底層調(diào)用,一般使用Arrays.copyOf()完成復(fù)制數(shù)組,需要的朋友可以參考下

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論