Is there an easy way to get the value of a string using its hashcode? Let me explain:
I have a string variable which has value Hello Guys
. It is saved here: String@dye63g
. Now, I want to get the string value Hello Guys
from String@dye63g
.
CodePudding user response:
Based on your comments on this question, your question is REALLY asking:
For an object whose class does not override the default toString()
from java.lang.Object
itself, can I derive the value again?
The answer is a resounding no. It's NOT A HASH. It's the fully qualified class name, an @
symbol, then in hexdigits (so 0-9a-f, not y and g like in your example), the value returned by System.identityHashCode(obj)
.
This code has no relationship at all with the value of it and isn't the hashCode of that object. It's, usually, its memory address or some modification of that principle.
You can trivially test this:
class Test {
public static void main(String[] args) {
System.out.println(System.identityHashCode("Hey, there!"));
}
}
Run that a few times. The same value might come out (it depends on your system and VM impl), but now run the same code on a different VM, or on a different computer, or reboot and run it again. Different values come out. That should make it rather obvious that you can't.
A second issue is the pigeonhole principle. That hash value is always exactly 32 bits long (why? Well, System.identityHashCode
returns an int
and those only have 32 bits. Each letter (0-9a-f) represents 4 bytes, hence why that thing after the @
is always 8 characters long: each character is 4 bits, 8*4 = 32.
You can test that too. Copy/paste the collected works of Shakespear into a single gigantic string, and now invoke identityHashCode
on that - it's still just a short number.
There are 4 billion different numbers that an int
can represent and that's it. However, there are more than 4 billion strings imaginable (there are an infinite amount of them, in fact). Hence, there must exist an infinite amount of different strings that all so happen to hash to, say, 1234abcd. Thus, given the hash 1234abcd, it is impossible to say which string produced it - after all, an infinite amount of strings exist that hash to that value. And that's presuming the hashing is 'constant' (same string results in the same hash, e.g. what string's own .hashCode()
method does). System.identityHashCode
isn't even that and in fact has no relationship at all to the actual value. The number you get depends on, effectively, the memory load of the system when you boot the JVM, more or less.