Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Understand System.identityHashCode in Java

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Tips and Traps

  1. You cannot get the address of an object in Java (without hacking). System.identityHashCode(obj) is the next best thing you can have. This trick (combined with the timestamp) is used to generate default seed for random number generators in Java.

import org.apache.commons.math3.random.RandomDataGenerator
import collection.JavaConverters._
val rng1 = new RandomDataGenerator()
val rng2 = new RandomDataGenerator()
import org.apache.commons.math3.random.RandomDataGenerator import collection.JavaConverters._ rng1: RandomDataGenerator = org.apache.commons.math3.random.RandomDataGenerator@254a5d85 rng2: RandomDataGenerator = org.apache.commons.math3.random.RandomDataGenerator@e18d0ae
System.identityHashCode(rng1)
res4: Int = 625630597
System.identityHashCode(rng2)
res5: Int = 236507310

References