开发者

Why does the default Object.toString() return a hex representation of the hashCode?

开发者 https://www.devze.com 2023-01-16 19:27 出处:网络
I\'m curious why Object.toString() returns this: return getClass().getName() + \"@\" + Integer.toHexString(hashCode());

I'm curious why Object.toString() returns this:

return getClass().getName() + "@" + Integer.toHexString(hashCode());

as opposed to this:

return getClass().getName() + "@" + hashCode();

Wha开发者_StackOverflow中文版t benefits does displaying the hash code as a hex rather than a decimal buy you?


The Short Answer:

Hash Codes are usually displayed in hexadecimal because this way it is easier for us to retain them in our short-term memory, since hexadecimal numbers are shorter and have a larger character variety than the same numbers expressed in decimal.

Also, (as supercat states in a comment,) hexadecimal representation tends to prevent folks from trying to assign some meaning to the numbers, because they don't have any. (To use supercat's example, Fnord@194 is absolutely not the 194th Fnord; it is just Fnord with some unique number next to it.)

The Long Answer:

Decimal is convenient for two things:

  • Doing arithmetic
  • Estimating magnitude

However, these operations are inapplicable to hashcodes. You are certainly not going to be adding hashcodes together in your head, nor would you ever care how big a hashcode is compared to another hashcode.

What you are likely to be doing with hashcodes is the one and only thing that they were intended for: to tell whether two hash codes possibly refer to the same object, or definitely refer to different objects.

In other words, you will be using them as unique identifiers or mnemonics for objects. Thus, the fact that a hashcode is a number is in fact entirely irrelevant; you might as well think of it as a hash string.

Well, it just so happens that our brains find it a lot easier to retain in short-term memory (for the purpose of comparison) short strings consisting of 16 different characters, than longer strings consisting of only 10 different characters.

To further illustrate the analogy by taking it to absurdity, imagine if hash codes were represented in binary, where each number is far longer than in decimal, and has a much smaller character variety. If you saw the hash code 010001011011100010100100101011 now, and again 10 seconds later, would you stand the slightest chance of being able to tell that you are looking at the same hash code? (I can't, even if I am looking at the two numbers simultaneously. I have to compare them digit by digit.)

On the opposite end lies the tetrasexagesimal numbering system, which means base 64. Numbers in this system consist of:

  • the digits 0-9, plus:
  • the uppercase letters A-Z, plus:
  • the lowercase letters a-z, plus:
  • a couple of symbols like '+' and '/' to reach 64.

Tetrasexagesimal obviously has a much greater character variety than lower-base systems, and it should come as no surprise that numbers expressed in it are admirably terse. (I am not sure why the JVM is not using this system for hashcodes; perhaps some prude feared that chance might lead to certain inconvenient four-letter words being formed?)

So, on a hypothetical JVM with 32-bit object hash codes, the hash code of your "Foo" object could look like any of the following:

Binary:           com.acme.Foo@11000001110101010110101100100011
Decimal:          com.acme.Foo@3251989283
Hexadecimal:      com.acme.Foo@C1D56B23
Tetrasexagesimal: com.acme.Foo@31rMiZ

Which one would you prefer?

I would definitely prefer the tetrasexagesimal, and in lack of that, I would settle for the hexadecimal one. Most people would agree.

One web site where you can play with conversions is here: https://www.mobilefish.com/services/big_number/big_number.php


Object.hashCode used to be computed based on a memory location where the object is located. Memory locations are almost universally displayed as hexadecimal.

The default return value of toString isn’t so much interested in the hash code but rather in a way to uniquely identify the object for the purpose of debugging, and the hash code serve well for the purpose of identification (in fact, the combination of class name + memory address is truly unique; and while a hash code isn’t guaranteed to be unique, it often comes close).

0

精彩评论

暂无评论...
验证码 换一张
取 消