您现在的位置是:首页 > 文章详情

java 语法糖之 -----------自动装箱、拆箱

日期:2019-08-05点击:360

一、 示例代码

 @Test public void test3(){ int i=1; int j=i++; if( i==(++j)&&( i++) == j){ i+=j; } System.out.println("i'"+ i); }
  • 结果如下: 5

    java_autobox

  • 结果为什么会是这样呢? 仔细看来

二、 自动装箱、拆箱

  • 定义

    • 自动装箱,在编译之后被转化成了对应的包装方法,Integer.valueOf();

    Integer i =1 ; 在编译中被执行: Integer i= Integer.valueOf(int i) ;

    • 拆箱 ,在编译之后被转化成了对应的还原方法, Integer.intValue()方法。

    int j = i++; 在编译中执行 拆箱 , int j = Integer.inValue(i);

  • 示例,调用了拆箱,java虚拟机中常量池包括有Integer型常量池,用一个表存储,具体信息如下:
    33

存储的范围值默认是 -128~127, 故如果Interger指向的是这个范围内的数字在编译的时候会直接指向常量池中的对象;

三、 通过两种方法,比较地址是否相同:

  • 第一种

    System.out.println(System.identityHashCode(i)); System.out.println(System.identityHashCode(i++)); System.out.println(System.identityHashCode(++j)); 
  • 第二种

    public class javaAuto { static final Unsafe unsafe = getUnsafe(); static final boolean is64bit = true; public static void main(String... args) { int i=1; int j=i++; System.out.println("-------------------------"); System.out.println("----"); printAddresses("i", i); System.gc(); System.out.println("----"); printAddresses("j", ++j); System.gc(); System.out.println("----"); printAddresses("i++", i++); } public static void printAddresses(String label, Object... objects) { System.out.print(label + ": 0x"); long last = 0; int offset = unsafe.arrayBaseOffset(objects.getClass()); int scale = unsafe.arrayIndexScale(objects.getClass()); switch (scale) { case 4: long factor = is64bit ? 8 : 1; final long i1 = (unsafe.getInt(objects, offset) & 0xFFFFFFFFL) * factor; System.out.print(Long.toHexString(i1)); last = i1; for (int i = 1; i < objects.length; i++) { final long i2 = (unsafe.getInt(objects, offset + i * 4) & 0xFFFFFFFFL) * factor; if (i2 > last) System.out.print(", +" + Long.toHexString(i2 - last)); else System.out.print(", -" + Long.toHexString( last - i2)); last = i2; } break; case 8: throw new AssertionError("Not supported"); } System.out.println(); } private static Unsafe getUnsafe() { try { Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); theUnsafe.setAccessible(true); return (Unsafe) theUnsafe.get(null); } catch (Exception e) { throw new AssertionError(e); } } } 
  • 通过这两种方式打印出的,i ,i++,++j 的地址都是一样的;他们的值呢?

四、值

  • i 和 ++j 的值都是2,这个毋庸置疑;
  • (i++)==j ,这个比较,楼主怀疑是先执行的比较,再执行的++操作,可是貌似又不符合运算符的运算规则。
  • 待查...希望有人能告知答案,不胜感激

参考链接: https://blog.csdn.net/isscollege/article/details/78398968

欢迎关注公众号,查看更多内容 :
XG54_9_WXMH_5X_HB_H_7V

原文链接:https://yq.aliyun.com/articles/712942
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章