`
niwtsew
  • 浏览: 71945 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

The trap of System.identityHashCode()

阅读更多

Why a trap ?

First ,let's take a look at the comments of this API provided by Sun:

    /**
     * Returns the same hash code for the given object as
     * would be returned by the default method hashCode() ,
     * whether or not the given object's class overrides
     * hashCode().
     * The hash code for the null reference is zero.
     *
     * @param x object for which the hashCode is to be calculated
     * @return  the hashCode
     * @since   JDK1.1
     */
    public static native int identityHashCode(Object x);

 

 

Pls,pay more and more attention to the default method hashCode() , do you know what is the default method hashCode()??   It is the hashCode() defined in Object class !!!

i.e. the one     public native int hashCode();  The most confusing comment is “whether or not the given object's class overrides hashCode()" ,  this is definitely bullshit bullshit.....

 

That means if your class overrides hashCode(), System.identityHashCode() != your.hashCode()! Checkout below testcase:

 

import junit.framework.TestCase;

public class TestIdentityHashCode extends TestCase{
	
	public void testIdentityHahshCodeAlwaysReturnTheSameAsDefaultHashCode()
	{
		Object arbitaryObj = new Object();
		assertEquals(arbitaryObj.hashCode(),System.identityHashCode(arbitaryObj));
		
		ObjectWhichDoesNotOverrideHashCode objUsingDefaultHashCode = new ObjectWhichDoesNotOverrideHashCode();
		assertEquals(objUsingDefaultHashCode.hashCode(),System.identityHashCode(objUsingDefaultHashCode));
		
		ObjectWhichOverrideHashCode objUsingOverridedHashCode = new ObjectWhichOverrideHashCode();
		assertEquals(objUsingOverridedHashCode.hashCode(),System.identityHashCode(objUsingOverridedHashCode));			
	}
	
	public void testIdentityHashCodeOfNullIsZero()
	{
		assertEquals(0, System.identityHashCode(null));
	}
	
	private static class ObjectWhichDoesNotOverrideHashCode
	{		
	}
	private static class ObjectWhichOverrideHashCode
	{		
		public int hashCode()
		{
			return 1000;
		}
	}
}


Above testcase always fails as expected.. so pls pay attention the trap .

BTW: the default hashCode() of Sun's JVM always returns the memory address of the specific Object 
which guarantee it's unique. So System.identityHashCode() could be used to generate a unique key 
in a same JVM.
 

 

2
0
分享到:
评论

相关推荐

    Trap+Instrumental.lnk

    Trap+Instrumental.lnk

    Use-SnmpTrap-Tools.rar_SNMPtrap

    SnmpTrap 是 SNMP 协议的一部分,它允许网络设备在发生特定事件时发送报警或陷阱消息到预先配置的管理站。这篇文章将详细介绍如何使用 SnmpTrap 工具,并探讨其相关知识点。 首先,我们需要理解 SnmpTrap 的基本...

    RFC1215_为使用SNMP定义Trap的惯例 .doc

    《为使用SNMP定义Trap的惯例 (RFC1215)》是一份关于网络管理的文档,由M. Rose编辑,旨在提供一种定义SNMP(简单网络管理协议)陷阱的标准方法。SNMP是一种广泛用于监控网络设备状态和性能的协议。陷阱(Trap)是...

    LEON 2 SystemC model on ReSP.pdf

    LEON 2 SystemC model on ReSP, 21页,目录如下: 1 Leon 2 5 1.1 Introduction . . . . . . . ....1.2 Architecture Description ....2.5 Modication of RTEMS to enable interrupts trap handler . . . . 20

    snmp-master.zip_SNMP_SNMPtrap_org.araqne_snmp 管理_snmp开发

    在SNMP中,SNMPtrap是一个关键功能,它允许网络设备在发生特定事件时主动向网络管理员或管理系统报告,而无需等待轮询。SNMPtrap消息是设备向管理站发送的异步通知,这些通知通常与设备故障、状态变化或其他重要事件...

    snmptrap.zip

    "snmptrap.zip"这个压缩包显然包含了用于在Windows环境下接收和调试SNMP TRAP信息的工具。 **SNMP TRAP详解:** SNMP TRAP是一种异步通信机制,当网络设备遇到异常情况或者发生了预定义的事件(如接口故障、内存...

    SNMP实例大全--snmp4j(get ,trap,set,取mib)

    本文将详细介绍SNMP实例,特别是通过Java库snmp4j实现的GET、GETNEXT、SET操作以及TRAP发送和MIB获取。 1. **SNMP基本概念** SNMP由三个主要组件构成:管理站(Manager)、代理(Agent)和管理信息库(MIB)。管理...

    选修六Unit 5 The power of nature课件及练习题7份3精选.doc

    So, while dedication and hard work are commendable qualities, it's vital to strike a balance and avoid falling into the trap of workaholism. After all, life is not just about work; it's about living, ...

    英文原版-Bioinformatics Challenges at the Interface of Biology and Computer Science 1st Edition

    It approaches bioinformatics from a unique perspective, highlighting interdisciplinary gaps that often trap the unwary.The book considers how the need for biological databases drove the evolution of ...

    3D_Flash_Memories.pdf

    1 The Business of NAND. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Rahul N. Advani 2 Reliability of 3D NAND Flash Memories. . . . . . . . . . . . . . . . . . . . 29 A. Grossi, C...

    Snmp_trap的配置与使用.doc

    - **程序方式发送Trap**:对于某些特定需求,可以编写C语言程序,使用SNMP库(如`send_easy_trap`或`send_v2trap`)发送Trap。 2. **Manager 端配置**: - **配置 snmptrapd.conf**:设置访问权限,添加Trap...

    snmp_trap 协议接收工具

    `snmp_trap`协议接收工具是针对SNMP Trap消息设计的应用,允许用户在Windows、Linux和Unix系统上接收并处理这些 traps。 SNMP Trap工作原理: 1. 设备触发事件:当网络设备(如路由器、交换机或服务器)遇到预定义...

    利用SNMP#NET做trap接收器

    Console.WriteLine($"Trap received from {trap.SrcAddress}"); foreach (var variable in trap.Vars) { Console.WriteLine($"OID: {variable.Oid}, Value: {variable.Value}"); } } } ``` 6. **配置网络设备...

    Matrix Differential Calculus with Applications in Statistics and Econometrics

    6Existence and uniqueness of the MP inverse . . . . . . . . . . . 37viContents 7Some properties of the MP inverse . . . . . . . . . . . . . . . . 38 8Further properties . . . . . . . . . . . . . . . ....

    snmp trap命令接收软件

    "receive_trap.exe"文件是一个可执行文件,双击运行后,该软件会在后台监听SNMP Trap消息。当你在其他设备上触发一个Trap命令时,这个软件将捕获并显示相关信息,从而帮助你验证Trap消息是否成功发送和接收。 在...

    Android代码-ColdStart – Instant SNMP Traps

    Most SNMP traps sent to trap.coldstart.io will arrive on your phone within 500ms, with a crowd sourced human readable version of the OIDs in a manner similar to SNMPTT. Read more here: ...

    游戏编程--设计思想

    "When you have learned to snatch the error code from the trap frame, it will be time for you to leave." “当你从我手中夺走水晶球时,就是你离开的时候了。” 1.1 Something mysterious is formed, born ...

    java_conn_SNMPtrap_send_accept.rar_SNMP_SNMPtrap_snmp java

    SNMPTrap是SNMP的一部分,用于设备向管理系统发送通知,通常在设备状态发生变化时,如接口状态改变或硬件故障等。在Java中实现SNMP通信,SNMP4J是一个常用的库。 《java_conn_SNMPtrap_send_accept.rar》这个压缩包...

    net-snmp-trap发送(c语言)

    SNMP陷阱是一种主动的通知机制,当网络设备发生异常或需要报告特定事件时,设备会向管理站发送trap,从而让管理员能够及时响应。 SNMP协议定义了五种版本:SNMPv1、SNMPv2c、SNMPv3、SNMPv2u和SNMPv2t。在这个场景...

    mpi_trap1.rar_并行计算

    本项目"mpi_trap1.rar"聚焦于使用MPI(Message Passing Interface)来实现梯形积分法的并行化,从而加速计算过程。 MPI,全称Message Passing Interface,是一种标准的库,用于在分布式内存系统(如多核处理器、...

Global site tag (gtag.js) - Google Analytics