【7】 Java基础 <七> —— 自动装箱 自动拆箱

封装类:

  包装类  将基本数据类型  包装成  类

  类  可以创建对象。(这样的设计符合面向对象的思想)

 

自动装箱  Integer b = 10 ;   等同于  Integer i = new Integer(20) ;  将基本数据类型封装成对象

自动拆箱  System.out.println( b + 20 ) ;   将对象转换为基本数据类型

 

创建 Integer 对象时 ,系统会自动判断 Integer 的值有没有超过127 ,如果超过127会在堆内存空间开辟新的地址 ,如果没超过默认的是同一个对象。

Integer a = 127;
Integer b = 127;
System.out.println(a.hashCode()+":::"+b.hashCode());
System.out.println("a==b?:::"+(a==b));       true 没超127同一个对象
Integer c = 128;
Integer d = 128;
System.out.println(c.hashCode()+":::"+d.hashCode());
System.out.println("c==d?:::"+(c==d));       false 超过127 不同对象

 

posted on 2018-03-11 21:06  就是菁可爱哦  阅读(97)  评论(0编辑  收藏  举报

导航