Java包装类(Integer 详解 )

Java包装类

基本概述


  • 在某些场合要求所有的数据内容都必须是类类型的对象,而Java语言中有8种基本数据类型声明的变量都不是对象,为了使得这些数据在该场合种能够使用,则需要好进行对象化处理,此时就需要借助包装类将变量包装成对象再进行使用

  • 包装类是为了值类型数据和对象间能互相转换、提供装箱和拆箱机制的类

基本数据类型 包装类型
byte Byte
Boolean Boolean
short Short
char Character
int Integer
long Long
float Float
double Double

自动拆箱与装箱

  • 自动拆箱: 自动进行包装类基本数据类型的转换
  • 自动装箱: 自动进行基本数据类型包装类的转换

Integer类型


  • java.lang.Integer 类是Object类的间接子类,用于包装int类型的数据
  • 该类由final关键字修饰,表示该类不能被继承
  • 常用方法
    • 该类重写了Object类的equals()、hashCode()以及toString()方法
    • Integer(int value) - 根据参数指定的整数值来构造对象
    • Integer(String s) - 根据参数指定的字符串来构造对象
    • int intValue() - 用于获取调用对象种含有的int类型数据并返回
      • 用于实现将Integer类型拆包为int类型(自动拆箱)
    • static Integer valueOf(int i) - 根据参数指定的整数来得到对象
      • 用于实现将int类型包装成Integer类型(自动装箱)
    • static int parseInt(String s) - 用于将字符转类型的数据转换为int类型的整数
  • 案例
/*
 * 若尘
 */
package packing;

/**
 * 演示Integer类的使用
 * @author ruochen
 * @version 1.0
 */
public class TestInteger {
	public static void main(String[] args) {
		// 使用Integer类中的构造方法来构造对象,该类没有无参构造方法
		
		Integer it = new Integer(123);
		// 自动调用toString()方法, 得到字符串类型的十进制整数
		System.out.println(it);
		
		Integer it2 = new Integer("234");
		System.out.println(it2);
		System.out.println("----------------------");
		
		// 实现int类型和Integer类型之间的相互转换
		Integer it3 = Integer.valueOf(222);
		System.out.println(it3);  // String 类型
		
		int res = it3.intValue();
		System.out.println(res);  // int 类型

		System.out.println("----------------------");
		// 实现String类型向int类型的转换
		int res2 = Integer.parseInt("33333");
		System.out.println(res2);
		
		// java.lang.NumberFormatException
		// 要求字符串中每个字符都是十进制整数的字符,否则产生数字格式异常
		// int res3 = Integer.parseInt("1234a");
		// System.out.println(res3);
		
		System.out.println("----------------------");
		// 自动装箱和自动拆箱的机制
		Integer it4 = 100;  // int -> Integer 发生自动装箱,自动调用ValueOf()
		res = it4;  // Integer -> int  发生自动拆箱,自动调用intValue()
		
		
		System.out.println("----------------------");
		Integer it5 = 128;
		Integer it6 = 128;
		Integer it7 = new Integer(128);
		Integer it8 = new Integer(128);
		
		System.out.println(it5.equals(it6));  // true 比较内容
		System.out.println(it5 == it6);  // false 比较地址
		System.out.println(it7.equals(it8));  // true 比较内容
		System.out.println(it7 == it8);  // false 比较地址
		
		
		System.out.println("----------------------");
		// 源码993行
		// 耗时
		// -128~127 提前装箱完毕
		// 此处127已经提前装箱,不需要重新创建对象,两个指向同一个对象
		// 上面128不在范围内,需要创建对象
		Integer it9 = 127;
		Integer it10 = 127;
		// 下面是自己手动new的两个对象,地址不同
		Integer it11 = new Integer(128);
		Integer it12 = new Integer(128);
		
		System.out.println(it9.equals(it10));  // true 比较内容
		System.out.println(it9 == it10);  // false 比较地址
		System.out.println(it11.equals(it12));  // true 比较内容
		System.out.println(it11 == it12);  // false 比较地址
	}
}

123
234
----------------------
222
222
----------------------
33333
----------------------
----------------------
true
false
true
false
----------------------
true
true
true
false

自动装箱池(-128~127)

  • 为了提高性能在Integer类的内部提供了自动装箱池,也就是把-128 ~ 127 之间的整数提前装箱完毕,若程序中需要该范围内的数据则直接从装箱池中获取,无需创建新对象
发布了266 篇原创文章 · 获赞 335 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_29339467/article/details/105702842