JAVA基础知识学习笔记——BigInteger、BigDecimal、Arrays、包装类、String

本文详细介绍了Java中的BigInteger类,包括概述、构造方法和成员方法,用于处理大整数计算。接着讲解了BigDecimal类,以及Arrays类在数组操作中的应用。此外,还探讨了包装类的装箱、拆箱、自动装箱和拆箱以及基本类型与字符串的转换。最后,总结了String类的重要构造方法和常用成员方法。
摘要由CSDN通过智能技术生成

一.BigInteger类

1.1 概述

java.math.BigInteger 类,不可变的任意精度的整数。如果运算中,数据的范围超过了long类型后,可以使用
BigInteger类实现,该类的计算整数是不限制长度的。

1.2 构造方法

BigInteger(String value) 将 BigInteger 的十进制字符串表示形式转换为 BigInteger。超过long类型的范围,已经不能称为数字了,因此构造方法中采用字符串的形式来表示超大整数,将超大整数封装成BigInteger对象。

1.3 成员方法

BigInteger类提供了对很大的整数进行加、减、乘、除的方法,注意:都是与另一个BigInteger对象进行运算。

方法声明 描述
add(BigInteger value) 返回其值为 (this + val) 的 BigInteger,超大整数加法运算
subtract(BigInteger value) 返回其值为 (this - val) 的 BigInteger,超大整数减法运算
multiply(BigInteger value) 返回其值为 (this * val) 的 BigInteger,超大整数乘法运算
divide(BigInteger value) 返回其值为 (this / val) 的 BigInteger,超大整数除法运算,除不尽取整数部分




/*
    java.math.BigInteger类
        不可变的任意精度的整数。可以操作超过long类型的整数
    构造方法:
        BigInteger(String val) 将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
        参数:
            String val:传递一个字符串的整数 "1111111111111111111111111111111111111111111" 不限制长度
    成员方法:
        BigInteger add(BigInteger val) 返回其值为 (this + val) 的 BigInteger。
        BigInteger subtract(BigInteger val) 返回其值为 (this - val) 的 BigInteger。
        BigInteger multiply(BigInteger val) 返回其值为 (this * val) 的 BigInteger。
        BigInteger divide(BigInteger val) 返回其值为 (this / val) 的 BigInteger。
 */
import java.math.BigInteger;

public class Demo01BigInteger {
   
    public static void main(String[] args) {
   
        //创建两个BigIteger对象
        BigInteger b1 = new BigInteger("14865435486468564564564315345");
        BigInteger b2 = new BigInteger("56418974564897987498648674489");

        //加法运算
        BigInteger sum = b1.add(b2);
        System.out.println("两个整数的和是:"+ sum);

        //减法运算
        BigInteger sub = b2.subtract(b1);
        System.out.println("两个整数的差是:" + sub);

        //乘法运算
        BigInteger multiply = b1.multiply(b2);
        System.out.println("两个整数相乘是:" + multiply);

        //除法运算
        BigInteger divide = b2.divide(b1);
        System.out.println("两个整数的商是:" + divide);

    }
}
两个整数的和是:71284410051366552063212989834
两个整数的差是:41553539078429422934084359144
两个整数相乘是:838692626607201885582562991108561916110221821574052733705
两个整数的商是:3

二.BigDecimal类



import java.math.BigDecimal;

/*
    java.math.BigDecimal类
        用于浮点数(小数)的精确计算
        以后想进行小数的精确计算,不要使用float和double,使用BigDecimal类
    构造方法:
        BigDecimal(String val) 将 BigDecimal 的字符串表示形式转换为 BigDecimal。
        参数:
            String val:传递一个字符类型的小数  "1.1"
    成员方法:
        加法:BigDecimal add(BigDecimal augend) 返回一个 BigDecimal,其值为 (this + augend)
        减法:BigDecimal subtract(BigDecimal subtrahend) 返回一个 BigDecimal,其值为 (this - subtrahend),
        乘法:BigDecimal multiply(BigDecimal multiplicand) 返回一个 BigDecimal,其值为 (this × multiplicand),
        除法:BigDecimal divide(BigDecimal divisor) 返回一个 BigDecimal,其值为 (this / divisor)
                如果无法表示准确的商值(除法除不尽 10/3=3.3333333333333),则抛出 ArithmeticException。
            BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
                返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。
                参数:
                    参数:
                        divisor - 此 BigDecimal 要除以的值。
                        scale - 保留的小数位数(2,3,4...)  3.33  3.333
                        roundingMode - 要应用的舍入模式。
                            BigDecimal.ROUND_HALF_UP:四舍五入模式  3.66666666==>3.67  3.333333==>3.3
 */

public class Demo02BigDecimal {
   
    public static void main(String[] args) {
   
        /*
add(BigDecimal augend)
返回 BigDecimal ,其值是 (this + augend) ,其标为 max(this.scale(), augend.scale()) 。
divide(BigDecimal divisor, int scale, int roundingMode)(第一个参数被除数,第二个参数保留)
返回一个 BigDecimal ,其值为 (this / divisor) ,其比例为指定。


         */
        System.out.println(0.09 + 0.01);//0.09999999999999999
        System.out.println(1.0 - 0.32);//0.6799999999999999
        BigDecimal b1 = new BigDecimal("0.09");
        BigDecimal b2 = new BigDecimal("0.01");

        //加法运算
        BigDecimal sum = b1.add(b2);
        System.out.println(sum);

        //减法运算
        BigDecimal sub = b1.subtract(b2);
        System.out.println(sub);

        //除法运算
        BigDecimal divide = b1.divide(b2);
        System.out.println(divide);

        BigDecimal b3 = new BigDecimal("10");
        BigDecimal b4 = new BigDecimal("3");
//        BigDecimal div2 = b3.divide(b4);//ArithmeticException数学运算异常
        //BigDecimal divide(BigDecimal divisor, int scale(保留小数位),int roundingMode(四舍五入模式))
        BigDecimal div2 = b3.divide(b4,2,BigDecimal.ROUND_HALF_UP);
        System.out.println(div2);

        System.out.println("-------------");

        BigDecimal b5 = new BigDecimal("11");
        BigDecimal b6 = new BigDecimal("3");
        BigDecimal div3 = b5.divide(b6,3,BigDecimal.ROUND_HALF_UP);
    }
}

0.09999999999999999
0.6799999999999999
0.10
0.08
9
3.33
-------------

三.Arrays类



import java.util.Arrays;

/*
    java.util.Arrays:操作数组的工具类
        此类包含用来操作数组(比如排序和搜索)的各种方法。
        Arrays类中的方法都是静态的,可以通过类名.方法名(参数)直接使用
    Arrays类中的成员方法:
         static String toString(Object[] a) 返回指定数组内容的字符串表示形式。
            对数组进行遍历,把数组中的元素组合成一个字符串返回
         static void sort(Object[] a)
            根据元素的自然顺序对指定对象数组按升序进行排序。
 */
public class Demo01Arrays {
   
    public static 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值