return hash-code of a string

  • Thread starter Flo 'Irian' Schaetz
  • Start date
F

Flo 'Irian' Schaetz

And thus spoke Eitan M...
Is there any hash function in Java, that convert a string value to a hash
code ?

String xyz = "Hello World";
int hash = xyz.hashcode();
 
E

Eitan M

Hello.
Is there any hash function in Java, that convert a string value to a hash
code ?

Thanks :)
 
E

Eric Sosman

Flo said:
And thus spoke Eitan M...


String xyz = "Hello World";
int hash = xyz.hashcode();

Should be hashCode(), with an upper-case C.

int hash = "Hello World".hashCode();

also works.
 
L

Lew

Eitan said:
Hash code returns an integer,

Please do not top-post (placement of your reply above the material to which
you're replying).
and due that it is limited (it is not CRC),

What is the limitation that concerns you, and how does it cause trouble?

Why is CRC needed?
so a string like : "zzzzz" may have an exception.

What sort of exception do you mean?


By definition, a hash reduces information from a source (document, byte
sequence, ...). That is, in fact, the point of a hash code in the first
place. The reduced information set is easier to manipulate than the source
domain set, and by dint of being int, faster, too. Not to put to fine a point
on it, hash codes are a hack to compensate for human impatience.

This aside from their cryptographic utility, but that's not essential to
Object.hashCode().

So if you want a hash code, you want to reduce information. There's no point
in asking for a hash code that doesn't reduce information.
 
L

Lew

Eitan said:
Hash code returns an integer,

Please do not top-post (placement of your reply above the material to which
you're replying).
and due that it is limited (it is not CRC),

What is the limitation that concerns you, and how does it cause trouble?

Why is CRC needed?
so a string like : "zzzzz" may have an exception.

What sort of exception do you mean?

And thus spoke Eric Sosman...
By definition, a hash reduces information from a source (document, byte
sequence, ...). That is, in fact, the point of a hash code in the first
place. The reduced information set is easier to manipulate than the source
domain set, and by dint of being int, faster, too. Not to put too fine a
point on it, hash codes are a hack to compensate for human impatience.

This aside from their cryptographic utility, but that's not essential to
Object.hashCode().

So if you want a hash code, you want to reduce information. There's no point
in asking for a hash code that doesn't reduce information.
 
R

rossum

On Sun, 12 Aug 2007 11:50:48 +0200, "Eitan M"

[Top posting changed]
Hash code returns an integer,
Of course hashCode() returns an integer, that is what it does. If you
do not want an integer then do not use hashCode().
and due that it is limited (it is not CRC),
If you want a CRC then use a CRC.
so a string like : "zzzzz" may have an exception.
I am not clear what you mean here. On my system

"zzzzz".hashCode() returns 116425210

rossum
 
E

Eitan M

Hash code returns an integer,
and due that it is limited (it is not CRC),
so a string like : "zzzzz" may have an exception.
 
J

Joshua Cranmer

Eitan said:
Hash code returns an integer,
and due that it is limited (it is not CRC),
so a string like : "zzzzz" may have an exception.

If I understand you correctly, what you are trying to say is that for
very long Strings, hashCode will fail with an exception. That is not
true: hashCode will only fail on a null object (for different reasons);
and the hash code will always be integral but not necessarily positive
(which is true for all objects). If you want a guaranteed positive hash
code, then use Math.abs(obj.hashCode()).

In short: hashCode will always return (except for null objects) an
integral value for any Java built-in object (negating bugs) and should
do so for any Java object.

I don't understand what you mean by "limited", though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top