基本数据类型包装类、BigInteger、BigDecimal概述

一、基本数据类型对象包装类概述

*a 基本类型包装类的产生:
       在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的,需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类型,考试成绩需要转换成double类型等。
*b 八种基本类型对应的包装类:
          char    Character
          int     Integer
          byte    Byte
          short   Short
          long    Long
          float   Float
          double  Double
          boolean Boolean
*c 基本数据类型对象包装类特点:用于在基本数据和字符串之间进行转换。

以Integer类为例

​ 以下介绍的Integer方法,在另七种包装类中也有相应的方法。

1、Integer类parseInt方法

//字符串"123"转换为int类型的数字123,结果为125
System.out.println(Integer.parseInt("123")+ 2);
/*
* Integer类静态方法parseInt(String s, int radix)
* radix基数,进制
* "110",2 含义 前面的数字是二进制的,但是方法parseInt运行结果都是十进制
*  指定进制的字符串转换为十进制的整数
*/
public static void function_1(){
    int i = Integer.parseInt("110", 2);
    System.out.println(i);//6
}
2、Integer类int转成字符串
  *1: 使用+与字符串拼接
        int i = 3;
        String s = i+"";
        System.out.println(s+1);//"31"
  
  *2: toString(int i) 
          返回一个表示指定整数的 String 对象。
          
  *3: static String toString(int i, int radix) 
          返回用第二个参数指定基数表示的第一个参数的字符串表示形式。 
3、Integer类构造方法
 *A:Integer类构造方法
    /*
     *  Integer类构造方法
     *   Integer (String s)
     *   将数字格式的字符串,传递到Integer类的构造方法中
     *   创建Integer对象,包装的是一个字符串
     *   将构造方法中的字符串,转成基本数据类型,调用方法,非静态的, intValue()
     */
    public static void function_3(){
        Integer in = new Integer("100");
        int i = in.intValue();
        System.out.println(--i);//99
    }
4、Integer类其他方法
/*
* Integer类的3个静态方法
* 做进制的转换
* 十进制转成二进制  toBinarString(int)
* 十进制转成八进制  toOctalString(int)
* 十进制转成十六进制 toHexString(int)
* 三个方法,返回值都是以String形式出现
*/

a:十进制转二,,十六进制

public static void function_1(){

    System.out.println(Integer.toBinaryString(99));
    System.out.println(Integer.toOctalString(99));
    System.out.println(Integer.toHexString(999));

}

b:获取int的最大值和最小值

/*
*   Integer类的静态成员变量
*   MAX_VALUE
*   MIN_VALUE
*/

public static void function(){

    System.out.println(Integer.MAX_VALUE);
    System.out.println(Integer.MIN_VALUE);

}
5、自动装箱和自动拆箱

​ 优点: 基本类型和引用类直接运算

​ 自动装箱:基本数据类型直接变成对象 自动拆箱:对象中的数据变回基本数据类型

public static void function(){
        
    //引用类型 , 引用变量一定指向对象
    //自动装箱, 基本数据类型1, 直接变成了对象
    Integer in = 1; // Integer in = new Integer(1)
        
    //in 是引用类型,不能和基本类型运算, 自动拆箱,引用类型in,转换基本类型
    //in+1  ==> in.inValue()+1 = 2    
    //in = 2    自动装箱
    in = in + 1;
    System.out.println(in);
    
}

二、大数据运算

(一)、BigInteger类概述和构造方法

​ java中long型为最大整数类型,对于超过long型的整数已经不能被称为整数了,它们被封装成BigInteger对象。在BigInteger类中,实现四则运算都是方法来实现,并不是采用运算符。

2.1.1、BigInteger类的常用构造方法:传递字符串,要求数字格式,没有长度限制
public static void function(){
    BigInteger b = new      BigInteger("8465846668464684562385634168451684568645684564564");
    System.out.println(b);
    BigInteger b1 = new     BigInteger("5861694569514568465846668464684562385634168451684568645684564564");
    System.out.println(b1);
}
2.1.2、BigInteger类四则运算
/*
 * 调用方法计算,计算结果也只能是BigInteger对象
 */
public static void function_1(){

     BigInteger b1 = new BigInteger("5665464516451051581613661405146");
     BigInteger b2 = new BigInteger("965855861461465516451051581613661405146");
    
     //计算 b1+b2对象的和,调用方法 add
     BigInteger bigAdd = b1.add(b2);//965855867126930032902103163227322810292
     System.out.println(bigAdd);
     
     //计算b1-b2对象的差,调用方法subtract
     BigInteger bigSub = b1.subtract(b2);
     System.out.println(bigSub);
     
     //计算b1*b2对象的乘积,调用方法multiply
     BigInteger bigMul = b1.multiply(b2);
     System.out.println(bigMul);
     
     //计算b2/b1对象商,调用方法divied
     BigInteger bigDiv = b2.divide(b1);
     System.out.println(bigDiv);
}

(二)、精确的小数计算BigDecimal

double和float类型在运算中很容易丢失精度,造成数据的不准确性,Java提供了BigDecimal类可以实现浮点数据的高精度运算。

2.2.1BigDecimal类实现加法减法乘法
/*
 *BigDecimal实现加、减、乘
 */
public static void function(){
    BigDecimal b1 =  new BigDecimal("0.09");
    BigDecimal b2 =  new BigDecimal("0.01");
    //计算b1+b2的和,调用方法add
    BigDecimal bigAdd = b1.add(b2);
    System.out.println(bigAdd);

    BigDecimal b3 = new BigDecimal("1");
    BigDecimal b4 = new BigDecimal("0.32");
    //计算b3-b2的差,调用方法subtract
    BigDecimal bigSub = b3.subtract(b4);
    System.out.println(bigSub);

    BigDecimal b5 = new BigDecimal("1.015");
    BigDecimal b6 = new BigDecimal("100");
    //计算b5*b6的成绩,调用方法 multiply
    BigDecimal bigMul = b5.multiply(b6);
    System.out.println(bigMul);
}
2.2.2、BigDecimal类实现除法

​ 对于浮点数据的除法运算,和整数不同,可能出现无限不循环小数,因此需要对所需要的位数进行保留和选择舍入模式。

 /*
  * BigDecimal实现除法运算
  * divide(BigDecimal divisor, int scale, int roundingMode) 
  * int scale : 保留几位小数
  * int roundingMode : 保留模式
  * 保留模式:
  *     static int ROUND_UP  向上+1
  *     static int ROUND_DOWN 直接舍去
  *     static int ROUND_HALF_UP  >= 0.5 向上+1(四舍五入)
  *     static int ROUND_HALF_DOWN   > 0.5 向上+1 ,否则直接舍去
  */
 public static void function_1(){
    BigDecimal b1 = new BigDecimal("1.0301");
    BigDecimal b2 = new BigDecimal("100");
    //计算b1/b2的商,调用方法divied
    BigDecimal bigDiv = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//0.01301
    System.out.println(bigDiv);
 }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值