3.基本类型包装类

一. 装箱和拆箱

image

  1. 装箱:把基本类型数据转换成对应的包装类对象。
  2. 拆箱:把包装类对象转化成对应的基本数据类型数据。
  3. Sun公司从Java5开始提供自动装箱(AutoBoxing)和自动拆箱(AutoUnboxing)功能

image

  1. 自动装箱:可把一个基本类型变量直接赋值给对应的包装类变量。
  2. 自动拆箱:运行把包装类对象直接赋给对相应的基本数据类型变量。

注意:自动装箱和自动拆箱,是一个编译器级别的新特性。

  1. 在底层依然是手动装箱和拆箱操作,
    但是:装箱操作使用的是integer.valueOf的方式,而是不是直接new Integer。

  2. switch支持的数据类型:byte,short,char,int,也支持对应的包装类,
    在底层,switch会对包装类型进行拆箱。

新建IntegerDemo.java

//Integer装箱和拆箱
class IntegerDemo 
{
	public static void main(String[] args) 
	{
		//装箱
		//方式1
		Integer num1 = new Integer(12);
		//方式2
		Integer num2 = Integer.valueOf(13);
		System.out.println(num1);
		System.out.println(num2);
		//拆箱
		int num3 = num1.intValue();
		System.out.println(num3);

		//自动装箱
		Integer num4 = 14;
		System.out.println(num4);
		//自动拆箱
		int num5 = num4;
		System.out.println(num5);

		switch (num1)
		{
		    case 12:  System.out.println("底层进行拆箱。");
			break;
		default :		
		}
	}
}
  1. Object可以接受一切数据类型的值。
    1. Object obj = 17;

    2. 引用的自动类型转换,把子类对象赋给父类变量:
      Object obj = i;

    3. Object数组:Object[]该数组可以装一切数据类型。
      Object[] arr = {“A”,12,3.15,fals};

二. 基本使用

  1. 包装类中的常量:MAX_VALUE/MIN_VALUE/SIZE(内存中存储占用的位数)/TYPE(对应的基本类型)

  2. 包装类的构造器,XXX类型的包装类型xxx(xx表示8大数据类型)。
    xxx(xxx value):接收自己的基本类型值,如Integer(int val)//Boolean(boolean val)
    xxx(String value):接收String类型,但是Character除外。

  3. 基本类型和包装类型的转换(装箱和拆箱)

    1. 装箱:

      Integer i1 = new Integer(123);

      Integer i2 = Integer.valueOf(123);//推荐,带有缓存。

    2. 拆箱:

      int val = i2.intValue();

  4. String和基本类型/包装类型之间的转换操作:

    1. Sting和int/Integer之间的转换操作:转换方法必须在String或Integer中
      把String转换为包装类类型:
      1. 方式1:static Xxx valueOf(String str) :Xxx表示包装类型
        Integer i1 = Integer.valueOf(“123”);
      2. 方式2:new Xxx(String str);
        Integer i2 = new Integer(“123”);
    2. 把包装类对象转换为String
      String str = 任何对象.toString();
    3. 把基本类型转换为String类型
      String str = 17 + “”;
    4. 把String转换为基本数据类型:
      static Xxx parseXxx(String s) :Xxx表示8大基本数据类型
      String input = “123456”;
      int num = Integer.parseInt(input);
  5. Boolean b = new Boolean(“SB”);//fales
    只认可true或TRUE,其他都是false

image

image

新建WrapperDemo.java

//包装类型,基本类,String类型的转换
class WrapperDemo
{
	public static void main(String[] args) 
	{
		//包装类型的常量
		System.out.println(Integer.MAX_VALUE);
		System.out.println(Integer.MIN_VALUE);
		System.out.println(Integer.SIZE);
		System.out.println(Integer.TYPE);
		
		//包装类型和基本类型的转换
		Integer i1 = new Integer(12);
		Integer i2 = Integer.valueOf(14);
		int num = i2.intValue();
		System.out.println(i1);
		System.out.println(i2);
		System.out.println(num);

		//String和包装类型的转换
		String s = i1.toString();
		Integer i3 = Integer.valueOf(s);
		System.out.println(i3);

		//String和基本类型的转换
		String s1 = 123 +"";
		String s2 = "234";
		int num2 = Integer.parseInt(s1);
		System.out.println(num2);
	}
}

三. 享元模式

  1. 包装类中的缓存设计(享元模式):
  2. Byte,Short,Integer,Long:缓存[-128,127]区间的数据;
  3. Character:缓存[0,127]区间的数据;

新建IntegerCache.java

//Integer的缓存
class IntegerCacheDemo
{
	public static void main(String[] args) 
	{
		Integer i1 = new Integer(123);
		Integer i2 = new Integer(123);
		System.out.println(i1 == i2);

		Integer i3 = Integer.valueOf(127);
		Integer i4 = Integer.valueOf(127);
		System.out.println(i3 == i4);//true:在[-128,127]范围,就获取缓存中的数据

		Integer i5 = 123;//自动装箱,底层:Integer.valuOf(123)
		Integer i6 = 123;
		System.out.println(i5 == i6);

		System.out.println("---------------------------");
		Integer i11 = new Integer(255);
		Integer i21 = new Integer(255);
		System.out.println(i11 == i21);

		Integer i31 = Integer.valueOf(255);//false:255不在[-128,127]之间,就得new Integer()
		Integer i41 = Integer.valueOf(255);
		System.out.println(i31 == i41);

		Integer i51 = 255;
		Integer i61 = 255;
		System.out.println(i51 == i61);
	}
}

image

  1. 包装类型对象之间的值的比较使用equals进行比较。

image

  1. 把包装类型拆箱为基本类型,再比较值。
    System.out.println(i51.equals(i61));//true

四.Integer与int的区别

  1. 默认值:
    int 的默认值是0;
    Integer的默认值为null;
    推论:Integer既可以表示null,又可以表示0;

  2. 包装类中提供了该类型相关的很多算法操作。
    static String toBinaryString(int i):把十进制转化为二进制
    static String toOctalString(int i):把十进制转化为八进制
    static String toHexString(int i):把十进制转换为十六进制

  3. 在集合框架中,只能存储内存对象类型,不能存储基本数据类型值。

  4. Integer和int不是相同数据类型
    可以共存
    void getInt(int num){}
    void getInt(Integer num){}

  5. 方法中,基本类型变量主要存储在栈中,包装类型存储在堆中。

发布了58 篇原创文章 · 获赞 0 · 访问量 734

猜你喜欢

转载自blog.csdn.net/huang_kuh/article/details/104862731