jvm imgui中,我正在使用

System.identityHashCode(i++)

哪里

var i = 0

为每个帧始终为给定对象生成一个常数id(因此
能够跟踪)

但是,一个user case只是告诉我,这仅对[0, 125]中的值有效

尝试调试并查找错误,我结束了对以下简短代码段的测试:

    var i = 0
    val A = Array(256, { System.identityHashCode(i++) })
    i = 0
    val B = Array(256, { System.identityHashCode(i++) })
    repeat(256) {
        if (A[it] != B[it])
            println("[$it] different, A ${A[it]}, B ${B[it]}")
    }


还有:


字节(完全工作,对于所有256个值,A == B)
短裤(不能从128开始工作)
整数(不能从128开始工作)
多头(不能从128开始工作)
浮动(完全不起作用)
加倍(根本不起作用)


这是为什么?

我是否可以安全地假设此行为在其他平台上也能保持一致?

最佳答案

但是,一个用户案例只是告诉我,这仅对[0,125]中的值有效


System.identityHashCode(Object)使用Object而不是原语,这意味着您的i++被自动装箱为Integer(或Long或...)。对象具有相同身份哈希码的唯一时间是(但并非总是)当它们是同一对象时。碰巧的是,JVM出于优化目的缓存了少量的Integer,这意味着0到127值的标识哈希码是相同的。

自动装箱时,编译器generates a call to Integer.valueOf(int)。如果我们看一下`valueOf(...)的代码,我们会看到:

if (i >= IntegerCache.low && i <= IntegerCache.high)
    return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);


因此,存在一个低缓存范围和一个高缓存范围,您将从中获得一个在JVM启动时生成的缓存常量对象。有JVM参数会影响数字高速缓存的大小。因此127必须在您的JVM的缓存范围内,而128以上的版本不在。

// both of the 100 values gets auto-boxed to the same Integer object
System.identityHashCode(100) == System.identityHashCode(100)


这基本上等效于比较同一对象的哈希码:

Integer i = new Integer(100);
System.identityHashCode(i) == System.identityHashCode(i)


一旦超过了初始的Integer值缓存集并指定了更大的int,新的Integer对象将在自动装箱后创建,因此(很可能)标识哈希码不再相等。

// by default, each of these 1000000 values gets auto-boxed to a different object
System.identityHashCode(1000000) != System.identityHashCode(1000000)


这基本上等效于实例化2个不同的整数,并期望它们的哈希码相同:

Integer i1 = new Integer(1000000);
Integer i2 = new Integer(1000000);
System.identityHashCode(i1) != System.identityHashCode(i2)


请注意,System.identityHashCode(...)不会返回唯一值,因此,当查看不同的对象时,它们有可能(尽管不太可能)生成相同的值。

关于java - System.identityHashCode()对原语的行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47677305/

10-14 11:57
查看更多