Java——泛型

Java——泛型

基本数据类型的包装类

  • 如果泛型都只能为引用数据类型…
  • 基本数据类型 包装类
  • byte Byte
  • short Short
  • int Integer
  • long Long
  • float Float
  • double Double
  • boolean Boolean
  • char Character
  • 自动装箱 : 讲 基本数据类型转为引用数据类型
  • 自动拆箱 : 将引用数据类型转换基本数据类型
public class DataDemo01 {
	public static void main(String[] args) {
		int i=1;
		//自动装箱		Integet in1 = Integer.valueof(1)
		Integer in1= i;
		//自动拆箱		in1.intValue()
		int i2 = in1;  
		
		test(1.1,2.2);
	}
	
	static double test(Double d1,Double d2) {//自动装箱
		return d1+d2; //自动拆箱
	}
}

1.无论何时Integer与newInteger之间都不会相等,因为不存在拆装箱过程,一个纸箱堆,一个指向常量池
2.两个newInteger不会相等 ,因为堆中地址不同
3.如果一个int与Integer或者newInteger,只要值相等就想等,因为会发生自动拆箱int再比较
4.两个Integer比较,再-128~127之间就相等,否则不相等
Integer对象比较问题

public class DataDemo02 {
	public static void main(String[] args) {
		int i1 = 127;
		Integer i2 = 127; //Integer.valueOf(127)
		Integer i3 = 127;
		Integer i4 = 128;
		Integer i5 = 128;
		Integer i6 = new Integer(127);
		Integer i7 = new Integer(127);
		
		System.out.println(i1==i2);  //true  自动拆箱
		System.out.println(i2==i3);  //true  缓冲区对象
		System.out.println(i4==i5);  //false new Integer,因为超过-128~127
		System.out.println(i3==i6);  //false  一个常量池,一个堆中不同
		System.out.println(i7==i6);  //false 因为都是通过new,在堆中的地址 肯定不相同
		System.out.println(i1==i6);  //true  自动拆箱
	}
}

下一篇,容器

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值