When we try to print any object using toString()
we get the HashCode (if toString()
is not overriden). But, If I want to print the Hashcode of the String Variable, what should I do.
This question it is with respect to Java.
Asked
Active
Viewed 3,023 times
1

Biswanath Chowdhury
- 257
- 1
- 3
- 15
-
What do you mean by "hash code of the String variable"? Why can't you simply call `hashCode()` on it? – Thomas Owens Aug 28 '12 at 13:57
-
`toString()` doesn't just print the hashCode() by default. You are better off calling `hashCode()` if that is what you want. Note: it might not be unique. – Peter Lawrey Aug 28 '12 at 13:57
-
you don't get the hashcode, you get the objectId. – Joeri Hendrickx Aug 28 '12 at 13:59
-
1@JoeriHendrickx And what would objectid be, this is not a standard Java term? – Marko Topolnik Aug 28 '12 at 14:00
-
@JoeriHendrickx: ok I meant the object id. thnx – Biswanath Chowdhury Aug 28 '12 at 14:00
-
@BiswanathChowdhury So, do you want to print the object id of your String, or the hashcode? :) – Joeri Hendrickx Aug 28 '12 at 14:01
-
@Peter Lawrey - That is a nice catch, that the default `hashCode()` implementation may not be unique for all objects. – Petar Minchev Aug 28 '12 at 14:03
-
2@PetarMinchev There is **no such thing** as a unique `hashCode` implementation, except for objects whose total number of possible states fits into an `int`. Such objects are **very rare**. – Marko Topolnik Aug 28 '12 at 14:04
-
There are many Strings with the same hashCode. ;) http://stackoverflow.com/questions/2310498/why-doesnt-strings-hashcode-cache-0 – Peter Lawrey Aug 28 '12 at 14:05
-
@JoeriHendrickx : what do you mean by "it is not a standard Java term" ? – Biswanath Chowdhury Aug 28 '12 at 14:05
-
@MarkoTopolnik `Integer` as a key isn't that rare. ;) – Peter Lawrey Aug 28 '12 at 14:05
-
@Marko Topolnik - Yep, it comes from the pigeonhole principle. – Petar Minchev Aug 28 '12 at 14:06
-
@BiswanathChowdhury I think he means, there is no globally unique id for an object you can print out. – Peter Lawrey Aug 28 '12 at 14:07
-
@BiswanathChowdhury The term "objectid" has no standard meaning in Java. The specification does not define it and it is not even in general use. Maybe there is some other programming language where that term is well-known, but Java is not such a language. – Marko Topolnik Aug 28 '12 at 14:07
-
@PeterLawrey : can you please elaborate me on this. " default hashCode() may not be unique". Thnx – Biswanath Chowdhury Aug 28 '12 at 14:10
-
I know that objectid is not a term in Java, I must have misleaded you. It was used because I honestly didn't knew the exact term. – Biswanath Chowdhury Aug 28 '12 at 14:13
-
@BiswanathChowdhury If you follow the link I gave, it lists multiple Strings with a hashCode() of 0. Empty string also has a hashCode of 0. – Peter Lawrey Aug 28 '12 at 14:13
-
@BiswanathChowdhury There is no exact term because its not something which exists in Java. – Peter Lawrey Aug 28 '12 at 14:14
-
@MarkoTopolnik: I think that by "objectId", Joeri Hendrickx means the value returned by `System.identityHashCode(___)`. (If so, (s)he's mistaken -- `Object.toString()` actually uses the hash-code from `hashCode()`, not the identity-hash-code -- but even so, that's what I think (s)he means.) – ruakh Aug 28 '12 at 14:26
-
@MarkoTopolnik Indeed, I was wrong; for some reason, I thought the default toString showed the object id (the one you see through jdb), but indeed, it's the hashcode. – Joeri Hendrickx Aug 28 '12 at 14:43
-
@BiswanathChowdhury Note additionally that even `identityHashCode` is not guaranteed to be unique on today's systems, since a `long` would be required to fit all the address space. – Marko Topolnik Aug 28 '12 at 14:47
-
possible duplicate of [what is an objects hashcode](http://stackoverflow.com/questions/2237720/what-is-an-objects-hashcode) – Oldskool Jan 01 '13 at 12:10
4 Answers
8
Just call hashCode()
on the String
object:
String s = "Hello World";
System.out.println(s.hashCode());
If you want it in the same format as Object.toString()
, try this:
System.out.println(Integer.toHexString(s.hashCode()));

Jesper
- 202,709
- 46
- 318
- 350
-
2More precisely: `s.getClass().getName() + "@" + Integer.toHexString(s.hashCode());` – assylias Aug 28 '12 at 13:58
-
@assylias Yes ofcourse: `"String@" + Integer.toHexString(s.hashCode()));` – Jesper Aug 28 '12 at 13:59
-
@Jesper: ok, hashCode() is the member of the object class. Thnx. – Biswanath Chowdhury Aug 28 '12 at 14:04
2
You can get the hash code of any Java object by invoking the hashCode()
method. The result will be an int
that you can then print or do anything else you want with it.
If you are interested in the implementation of Object.toString
, it is very easy to check at grepcode. It says:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

Marko Topolnik
- 195,646
- 29
- 319
- 436
1
Simply call the hashcode()
method. It comes from Object
.
String str = "mystring";
System.out.println(str.hashCode());

Petar Minchev
- 46,889
- 11
- 103
- 119