更新时间:2022-05-01 01:01:20
@Test
public void test3(){
int i=1;
int j=i++;
if( i==(++j)&&( i++) == j){
i+=j;
}
System.out.println("i'"+ i);
}
定义
Integer i =1 ; 在编译中被执行: Integer i= Integer.valueOf(int i) ;
int j = i++; 在编译中执行 拆箱 , int j = Integer.inValue(i);
存储的范围值默认是 -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);
}
}
}
参考链接: https://blog.csdn.net/isscollege/article/details/78398968
欢迎关注公众号,查看更多内容 :