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表示一个正常的状态
        //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
        //如果有无限循环小数,就会保留分子的精度
        System.out.println(bigDecimal.divide(bigDecimal2,BigDecimal.ROUND_CEILING));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值