Java中基本类型包装类(拆装箱)以及其中Integer的特性

为了对基本数据类型进行更多的操作,Java就针对每一种基本数据类型提供了对应的类类型。
常用操作之一:基本数据类型与字符串之间的转换。
基本类型与其对应的包装类类型。

基本类型对应的包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Integer 类在对象中包装了一个基本类型 int 的值。Integer 类型的对象包含一个 int 类型的字段。
此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。

构造方法摘要

  • Integer(int value) 构造一个新分配的 Integer 对象,它表示指定的 int 值。
  • Integer(String s) 构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。

public class Mytest3 {
    public static void main(String[] args) {
        int num = 100;
        Integer integer = new Integer(num);
        System.out.println(integer);
        Integer integer1 = new Integer("123");
        System.out.println(integer1+89);
    }
}


public class Mytest1 {
    public static void main(String[] args) {
        int num=100;
        //转换为二进制
        String string = Integer.toBinaryString(num);
        //转换为八进制
        String string1 = Integer.toOctalString(num);
        转换为十六进制
        String string2 = Integer.toHexString(num);
        //将返回的二进制字符串转换成其对应的数字
        System.out.println(Integer.parseInt(string)+45);
        System.out.println(string1);
        System.out.println(string2);
    }
}

String和int类型的相互转换

int – String

  • 和""进行拼接
  • public static String valueOf(int i)
  • int – Integer – String
  • public static String toString(int i)

String – int

  • String – Integer – intValue();
  • public static int parseInt(String s)
public class Mytest2 {
    public static void main(String[] args) {
        //int->String
        int num=100;
        String str=num+"";
        String.valueOf(num);
        String s = new Integer(num).toString();
        //String->int
        String str1 = "123";
        Integer integer = new Integer(str1);
        int i = integer.intValue();
        int i1 = Integer.parseInt(str1);
    }
}

JDK5的新特性,自动拆装箱。

  • 自动装箱:把基本类型转换为包装类类型
  • 自动拆箱:把包装类类型转换为基本类型
public class Mytest4 {
    public static void main(String[] args) {
        Integer num = 100;//自动装箱
        int num1 = 200;
        int i = num.intValue();//手动拆箱
        System.out.println(num + num1);
    }
}

public class Mytest5 {
    public static void main(String[] args) {
        Integer num = 10;//自动装箱
        num  += 100;//先自动拆箱,在自动装箱
        Integer integer = new Integer(200);
        int i = integer.intValue();//手动拆箱
        Integer integer1 = Integer.valueOf(100);//手动装箱
        Integer integer2 = Integer.valueOf("10000");//手动装箱

    }
}

Java 的Integer、int与new Integer到底怎么回事?

  1. int和Integer在进行比较的时候,Integer会进行拆箱,转为int值与int值进行比较。
  2. Integer与Integer比较的时候,由于直接赋值的时候会进行自动的装箱,那么这里就需要注意两个问题,一个是-128<= x<=127的整数,将会直接缓存在IntegerCache中,那么当赋值在这个区间的时候,不会创建新的Integer对象,而是从缓存中获取已经创建好的Integer对象。二:当大于这个范围的时候,直接new Integer来创建Integer对象。
  3. new Integer(1) 和Integer a = 1不同,前者会创建对象,存储在堆中,而后者因为在-128到127的范围内,不会创建新的对象,而是从IntegerCache中获取的。那么Integer a = 128, 大于该范围的话才会直接通过new Integer(128)创建对象,进行装箱。
package org.westos.demo2;

public class Mytest6 {
    public static void main(String[] args) {
        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1 == i2);//false
        System.out.println(i1.equals(i2));//true
        System.out.println("-----------");

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

        Integer i5 = 128;
        Integer i6 = 128;
        System.out.println(i5 == i6);//false  因为 超过了一个字节的范围 会new 一个Integer对象
        System.out.println(i5.equals(i6));//true
        System.out.println("-----------");

        Integer i7 = 127;
        Integer i8 = 127;
        System.out.println(i7 == i8);//true 没有超过一个字节的范围 因为在方法区中存在一个 字节常量池 范围-128---127
        System.out.println(i7.equals(i8));//true
    }
}

运行结果为:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值