星空网 > 软件开发 > Java

java中打印变量地址

在java中打印变量的地址

这个代码是在startoverflow上看到的,跟大家分享一下。

import sun.misc.Unsafe;import java.lang.reflect.Field;import java.util.Arrays;import java.util.Collections;public class OrderOfObjectsAfterGCMain {  static final Unsafe unsafe = getUnsafe();  static final boolean is64bit = true; // auto detect if possible.  public static void main(String... args) {    Double[] ascending = new Double[16];    for(int i=0;i<ascending.length;i++)      ascending[i] = (double) i;    Double[] descending = new Double[16];    for(int i=descending.length-1; i>=0; i--)      descending[i] = (double) i;    Double[] shuffled=new Double[16];    for(int i=0;i<shuffled.length;i++)      shuffled[i] = (double) i;    Collections.shuffle(Arrays.asList(shuffled));    System.out.println("Before GC");    printAddresses("ascending", ascending);    printAddresses("descending", descending);    printAddresses("shuffled", shuffled);    System.gc();    System.out.println("\nAfter GC");    printAddresses("ascending", ascending);    printAddresses("descending", descending);    printAddresses("shuffled", shuffled);    System.gc();    System.out.println("\nAfter GC 2");    printAddresses("ascending", ascending);    printAddresses("descending", descending);    printAddresses("shuffled", shuffled);  }  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);    }  }}

设置因为导入import sun.misc.Unsafe;引起的错误

java中打印变量地址




原标题:java中打印变量地址

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

2022春节亚马逊备货的tips!你掌握了吗?:https://www.kjdsnews.com/a/776446.html
Lazada首次推出中越直飞专线;Shopee推出自助取货:https://www.kjdsnews.com/a/776447.html
大量卖家收到亚马逊封店警告,清库存、搞促销也中招!:https://www.kjdsnews.com/a/776448.html
Discogs官网网址,Discogs是什么电商平台:https://www.kjdsnews.com/a/776449.html
除了内卷,2022年电商人面临的7大挑战:https://www.kjdsnews.com/a/776450.html
什么是社交媒体营销?海外品牌营销指南:https://www.kjdsnews.com/a/776451.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流