Java包装类、装箱拆箱和大数类

本文围绕Java展开,介绍了8个基本类型对应的包装类,包括Integer对象创建、类型转换等。阐述自动装箱拆箱概念、享元模式及判断Integer相等的方法。还提及Math、BigInteger、BigDecimal类的用途,最后深入分析Integer和int的区别及自动拆装箱机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、8个基本类型及其对应的包装类:

Java中是可以直接处理 基本类型 的,
但Java是一门 面向对象 的语言,
有些时候,需要将其这些基本类型 看作对象来处理

Java中有8个基本类型,Java在 Java.lang 包下为其提供了 包装类
当基本类型变成对象后,就可以在其中定义方法,方便开发者操作,
比如:转换成字符串操作等。

基本数据类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

1. Integer对象的创建、自带方法和
   int、String、Integer三者的转换:

1.用构造方法创建Integer对象:

Integer i1 = new Integer(10); //int
Integer i2 =new Integer("10");//String

2.int 转换成Integer对象:

Integer i3 = 10; //自动装箱
Integer i4 =Integer.valueOf(10);//valueOf方法

3.String转换成Integer对象:

Integer i5 = Integer.valueOf("10");//valueOf方法

tips: valueOf() 接受字符串或数字,转换成调用者


4.Integer对象转换成int:

Integer i1 = new Integer(10);
int i2 = i1.intValue();//intValue()方法
int i3 = i1;//自动拆箱

tips: intValue() 以int的方式返回

5.Integer对象转换成String:

Integer i4 = new Integer(10);
String s5 = i4.toString();//toString()方法

6.int转换成String:

String s1 = Integer.toString(10);//toString()方法
String s2 = String.valueOf(10);//valueOf()方法
String s3 = 10 + "";

7.String转换成int:

int i4 = Integer.parseInt("10");

tips: valueOf(10 “10”),接受值,前面写的是谁就转换成谁(String、Integer);
Integer方法中:
intValue() 转换成数字
toString() 转换成字符串
toString(10) 数字转换成字符串
parseInt(“10”) 字符串转换成数字


8.获取int类型的最小值:

int maxint = Integer.MIN_VALUE

9.获取int类型的最大值:

int minint = Integer.MAX_VALUE

10.将int类型的十进制转换成2进制:

String s1 = Integer.toBinaryString(10);

11.将int类型的十进制转换成16进制:

String s2 = Integer.toHexString(10);

12.将int类型的十进制转换成8进制:

String s3 = Integer.toOctalString(10);

2. 其他包装类的构造方法:

1.Boolean:

Boolean b = new Boolean(true);
Boolean b = new Boolean("true");//其余字符串皆为false

2.Byte:

Byte myb = 45;
Byte b = new Byte(myb);
Byte b = new Byte("12");

3.Character:

Character c =new Character("s"); 

4.Double:

Double d1 =new Double(12.5); 
Double d2 =new Double("12.5"); 

ps:Number是BigDecimal、BigInteger、Byte、Double、Float、Integer、Long、Short的父类。


二、自动装箱拆箱:

1.相关概念:

  1. 基本类型 之间的转换:
    自动转换:小范围到大范围,自动填0补位;
    强制转换:大范围到小范围,损失精度;
      自动
 —————————————> 
 byte  short   int  long  float  double
       char
 <—————————————
     (强制)

注意:
① 如果整数没有超出byte、short、char的取值范围,可直接用整数赋值;
		
		byte b1 = 3;
		byte b2 = 4;
		
② byte、short、char做混合运算时,各自都先转换成int再运算;	
		
		byte b3 = (byte)(b1 + b2);;

其他例子:
short s1 = 1;//注意①
int s2 = s1 +1;//自动转换成int,之后相加
s1 = (short)(s1 + 1);//int强制转换为short
s1 += 1;//正确,等价于上面

  1. 引用类型 之间的转换:
    向上转型:父类引用子类对象;
    向下转型:子类引用父类对象(instanceof);
class Father {}
class Son extends Father {}


Father f = new Father();
Son s = new Son();


Father f1 = s;// 向上造型
if(f1 instanceof Son){  //判断是否可以转换
		Son s1 = (Son) f1; // 向下造型
}

Son s2 = f; // 错误
  1. 基本类型引用类型 之间的转换:
    基本类型 —> 引用类型:装箱;
    引用类型 —> 基本类型:拆箱;
自动装箱:把基本类型转换为包装类类型
Integer i1 = 10; //10会包装成Integer类的实例对象

自动拆箱:把包装类类型转换为基本类型
int i2 = i1; //i1实例对象会自动转换成int的10

案例:

//map中 key-value 都必须是对象
HashMap<String, Integer> hm = new HashMap<String,Integer>();

hm.put("a", 1); //1,自动装箱
int x = hm.get("a"); //1,自动拆箱

注意:自动装箱 ,或者 valueOf()方法
对于byte int long short -128到127之间,
对于char 0 到127之间,
存在 享元模式,见下文。

2.享元模式:

Integer n1 = 10;
Integer n2 = 10;
System.out.println(n9==n10);//true
		
Integer n3 = Integer.valueOf("11");
Integer n4 = Integer.valueOf("11");
System.out.println(n3==n4);//true
		
//上面用到了享元模式,第二个数也指向第一个数的内存空间
System.out.println("==================");
		

Integer n5 = 128;
Integer n6 = 128;
System.out.println((n5==n6));//false
		
Integer n7 = new Integer(11);
Integer n8 = new Integer(11);
System.out.println(n7==n8);//false
		
//上面没有用到享元模式,是两个不同的内存空间
System.out.println("==================");

3.判断两个Integer类型是否相等:

如果判断两个Integer类型是否相等时,
要使用equals()方法,不要使用"==",
原因和String类一样,Integer类也重写了Object中的equals()方法:

Integer i1 = 128;
Integer i2 = 128;
System.out.println(i1==i2);//false
System.out.println(i1.equals(i2));//true
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3==i4);//false
System.out.println(i3.equals(i4));//true

如果Integer数据是在(-128~127)之间,就会涉及到 常量池(享元模式) 的概念:

Integer i1 = 127; 
Integer i2 = 127;
System.out.println(i1==i2); //true
System.out.println(i1.equals(i2));//true
//上面程序不会在堆中创建对象,会直接从整型常量池中拿。
Integer i3 = new Integer(127);
Integer i4 = new Integer(127);
System.out.println(i3==i4);//false
System.out.println(i3.equals(i4));//true

可见,比较Integer的值是否相等时,equals()方法时稳定的。


三、关于数字:

1.Math类:

在java.lang包下,有个Math类,这个类包含用于执行基本数学运算的方法,如四舍五入,开方等等。

public class MathTest {

    public static void main(String[] args) {
        //圆周率
        System.out.println(Math.PI);
        //取绝对值
        System.out.println(Math.abs(-10));              

        //ceil天花板,会向上取值,结果是double
        System.out.println(Math.ceil(12.3));//13.0
        System.out.println(Math.ceil(12.99));//13.0

        System.out.println("-----------");
        //floor地板,会向下取整,结果是double
        System.out.println(Math.floor(12.3)); //12.0           
        System.out.println(Math.floor(12.99));//12.0

        //获取两个值中的最大值
        System.out.println(Math.max(20, 30));

        //前面的数是底数,后面的数是指数,即23次方
        System.out.println(Math.pow(2, 3));

        //生成0.01.0之间的随机小数,包括0.0,不包括1.0
        System.out.println(Math.random());

        //四舍五入
        System.out.println(Math.round(12.3f));//12
        System.out.println(Math.round(12.9f));//13

        //开平方
        System.out.println(Math.sqrt(16));
    }

}

2.BigInteger类:

BigInteger类可以让超过Integer范围的数据进行运算,通常在对数字计算比较大的行业中应用的多一些。

import java.math.BigInteger;

public class BigIntegerTest01 {

    public static void main(String[] args) {

        BigInteger bi1 = new BigInteger("100");
        BigInteger bi2 = new BigInteger("2");

        System.out.println(bi1.add(bi2));                 //+
        System.out.println(bi1.subtract(bi2));            //-
        System.out.println(bi1.multiply(bi2));             //*
        System.out.println(bi1.divide(bi2));            ///()

        BigInteger[] arr = bi1.divideAndRemainder(bi2);    //取商和余数

        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

}

3.BigDecimal类:

由于在运算的时候,float类型和double很容易丢失精度,
在金融、银行等对数值精度要求非常高的领域里面,
就不能使用float或double了,
为了能精确的表示、计算浮点数,
Java提供了BigDecimal类。
注意:如果对计算的数据要求高精度时,必须使用BigDecimal类

import java.math.BigDecimal;

public class BigDecimalTest01 {

    public static void main(String[] args) {
        System.out.println(2.0 - 1.1);//0.8999999999999999

        //这种方式在开发中不推荐,因为不够精确
        BigDecimal bd1 = new BigDecimal(2.0);        
        BigDecimal bd2 = new BigDecimal(1.1);
        System.out.println(bd1.subtract(bd2));
        //0.899999999999999911182158029987476766109466552734375

        //开发时推荐通过传入字符串的方式
        BigDecimal bd3 = new BigDecimal("2.0");        
        BigDecimal bd4 = new BigDecimal("1.1");
        System.out.println(bd3.subtract(bd4));//0.9

        //这种方式在开发中也是推荐的
        BigDecimal bd5 = BigDecimal.valueOf(2.0);    
        BigDecimal bd6 = BigDecimal.valueOf(1.1);
        System.out.println(bd5.subtract(bd6));//0.9
    }

}

四、转载:彻底让你明白 Integer 和 int 的区别:


本文来源:https://blog.youkuaiyun.com/teacher_lee_zzsxt/article/details/79230501
作者:郑州尚学堂李老师
来源:优快云


1.引入:

突然发现自己对Integer i = 10;这种语法不太明白,于是乎有了这篇文章,
那么在讲解 Integer 之前,我们先看下面这段代码:

    public static void main(String[] args) {
        Integer i = 10;
        Integer j = 10;
        System.out.println(i == j);
        
        Integer a = 128;
        Integer b = 128;
        System.out.println(a == b);
         
        int k = 10;
        System.out.println(k == i);
        int kk = 128;
        System.out.println(kk == a);
          
        Integer m = new Integer(10);
        Integer n = new Integer(10);
        System.out.println(m == n);
    }

大家可以先思考一下结果是什么?

答案是:
在这里插入图片描述
至于为什么是这个结果,下面我们来逐个进行介绍。


2.Integer 类简介:

首先我们大致看一下Integer是什么,Integer 类在JDK1.0的时候就有了,它是一个类,是 int 基本数据类型的封装类。
  
  在这里插入图片描述

基本API如下:
  
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


3.Integer 类和 int 的区别:

①、Integer 是 int 包装类,int 是八大基本数据类型之一(byte,char,short,int,long,float,double,boolean)

②、Integer 是类,默认值为null,int是基本数据类型,默认值为0;

③、Integer 表示的是对象,用一个引用指向这个对象,而int是基本数据类型,直接存储数值。


4.Integer 的自动拆箱和装箱:

自动拆箱和自动装箱是 JDK1.5 以后才有的功能,也就是java当中众多的语法糖之一,它的执行是在编译期,会根据代码的语法,在生成class文件的时候,决定是否进行拆箱和装箱动作。

①、自动装箱
一般我们创建一个类的时候是通过new关键字,比如:

Object obj = new Object();

但是对于 Integer 类,我们却可以这样:

Integer a = 128;

为什么可以这样,通过反编译工具,我们可以看到,生成的class文件是:

Integer a = Integer.valueOf(128);

这就是基本数据类型的自动装箱,128是基本数据类型,然后被解析成Integer类。
②、自动拆箱

我们将 Integer 类表示的数据赋值给基本数据类型int,就执行了自动拆箱。

Integer a = new Integer(128);

int m = a;

反编译生成的class文件:

Integer a = new Integer(128);

int m = a.intValue();

简单来讲:自动装箱就是Integer.valueOf(int i);自动拆箱就是 i.intValue();


5.回顾开头的问题:

public static void main(String[] args) {
    Integer i = 10;
    Integer j = 10;
    System.out.println(i == j);
      
    Integer a = 128;
    Integer b = 128;
    System.out.println(a == b);
     
    int k = 10;
    System.out.println(k == i);
    int kk = 128;
    System.out.println(kk == a);
      
    Integer m = new Integer(10);
    Integer n = new Integer(10);
    System.out.println(m == n);
}

我们使用反编译工具Jad,得到的代码如下:

public static void main(String args[])
{
    Integer i = Integer.valueOf(10);
    Integer j = Integer.valueOf(10);
    System.out.println(i== j);
    Integer a = Integer.valueOf(128);
    Integer b = Integer.valueOf(128);
    System.out.println(a== b);
    int k = 10;
    System.out.println(k== i.intValue());
    int kk = 128;
    System.out.println(kk== a.intValue());
    Integer m = new Integer(10);
    Integer n = new Integer(10);
    System.out.println(m== n);
}

打印结果为:
  首先,直接声明Integer i = 10,会自动装箱变为Integer i = Integer.valueOf(10);Integer i 会自动拆箱为 i.intValue()。

①、第一个打印结果为 true

对于 i== j ,我们知道这是两个Integer类,他们比较应该是用equals,这里用==比较的是地址,那么结果肯定为false,但是实际上结果为true,这是为什么?

我们进入到Integer 类的valueOf()方法:
  在这里插入图片描述
分析源码我们可以知道在 i >= -128 并且 i <= 127 的时候,第一次声明会将 i 的值放入缓存中,
第二次直接取缓存里面的数据,而不是重新创建一个Ingeter 对象。
那么第一个打印结果因为 i = 10 在缓存表示范围内,所以为 true。

②、第二个打印结果为 false

从上面的分析我们知道,128是不在-128到127之间的,所以第一次创建对象的时候没有缓存,
  第二次创建了一个新的Integer对象。故打印结果为false

③、第三个打印结果为 true

Integer 的自动拆箱功能,也就是比较两个基本数据类型,结果当然为true

④、第四个打印结果为 true

解释和第三个一样。int和integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比较。

⑤、第五个打印结果为 false

因为这个虽然值为10,但是我们都是通过 new 关键字来创建的两个对象,是不存在缓存的概念的。两个用new关键字创建的对象用 == 进行比较,结果当然为 false。


6.测试:

Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
 
Integer e = 321;
Integer f = 321;
 
Long g = 3L;
Long h = 2L;
 
System.out.println(c== d);
System.out.println(e== f);
System.out.println(c== (a + b));
System.out.println(c.equals((a+b)));
System.out.println(g == (a+b));
System.out.println(g.equals(a+b));
System.out.println(g.equals(a+h));

反编译结果:

在这里插入图片描述
  打印结果为:

true
false
true
true
true
false
true

分析:

第一个和第二个结果没什么疑问,Integer类在-128到127的缓存问题;

第三个由于 a+b包含了算术运算,因此会触发自动拆箱过程(会调用intValue方法),
==比较符又将左边的自动拆箱,因此它们比较的是数值是否相等

第四个对于c.equals(a+b)会先触发自动拆箱过程,再触发自动装箱过程,
也就是说a+b,会先各自调用intValue方法,得到了加法运算后的数值之后,
便调用Integer.valueOf方法,再进行equals比较。

第五个对于 g== (a+b),首先计算 a+b,也是先调用各自的intValue方法,
得到数值之后,由于前面的g是Long类型的,也会自动拆箱为long,
==运算符能将隐含的将小范围的数据类型转换为大范围的数据类型,
也就是int会被转换成long类型,两个long类型的数值进行比较。

第六个对于 g.equals(a+b),同理a+b会先自动拆箱,然后将结果自动装箱,
需要说明的是 equals 运算符不会进行类型转换。所以是Long.equals(Integer),
结果当然是false

第七个对于g.equals(a+h),运算符+会进行类型转换,a+h各自拆箱之后是int+long,
结果是long,然后long进行自动装箱为Long,两个Long进行equals判断。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值