hello
Iam wondering if it's ok to get negative values from hash code?
it seems strange to me, since hashcode should reffer to place in memory,
or have I got it wrong?
public class Test {
public static void main(String[] args) {
String z = "string";
System.out.println(z);
System.out.println("1: "+z.hashCode());
}
public int hashCode() {
int hash = 1;
hash = hash*17+this.hashCode();
return hash;
}//@-->---
}//@-->---
Output:
string
1: -891985903

Signature
Thanx in advance
________________________
Torkel Franzen - 21 Jan 2006 10:42 GMT
> Iam wondering if it's ok to get negative values from hash code?
Sure. Before using the hash code we just make sure to set the sign bit
to 0.
Carramba - 21 Jan 2006 10:48 GMT
But by doing so, it results in pitfall? since you can have to diffetern
hashcodes with differens in sign?
would't it make more sence to genere hachcode witch would by nonnegative
directly? instead of shifting bits?
>> Iam wondering if it's ok to get negative values from hash code?
>
> Sure. Before using the hash code we just make sure to set the sign bit
> to 0.

Signature
Thanx in advance
________________________
Roedy Green - 21 Jan 2006 10:56 GMT
>But by doing so, it results in pitfall? since you can have to diffetern
>hashcodes with differens in sign?
>would't it make more sence to genere hachcode witch would by nonnegative
>directly? instead of shifting bits?
no. You don't use the hashcode directly to address anything anyway.
you always collapse it further. Why waste a bit. Sometimes you might
use it as an unsigned quantity.
All the techniques for generating them will produce essentially random
numbers which will include negatives. There is no point on insisting
they be negative. The produces would have to trim the sign and the
collection would have to check it was trimmed. Better to just let the
collection trim the sign it if needs to.
What you often want in the remainder after division by some prime or
power of two. See http://mindprod.com/jgloss/modulus.html
The sign trimming comes out in the wash.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 21 Jan 2006 10:53 GMT
>Iam wondering if it's ok to get negative values from hash code?
>it seems strange to me, since hashcode should reffer to place in memory,
>or have I got it wrong?
negative is fine. It is just a digest, not a slot in ram. See
http://mindprod.com/jgloss/hashcode.html
and http://mindprod.com/hashtable.html
for how it works.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
IchBin - 21 Jan 2006 21:33 GMT
>> Iam wondering if it's ok to get negative values from hash code?
>> it seems strange to me, since hashcode should reffer to place in memory,
[quoted text clipped - 4 lines]
> and http://mindprod.com/hashtable.html
> for how it works.
http://mindprod.com/jgloss/hashtable.html

Signature
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Carramba - 21 Jan 2006 11:17 GMT
who complex should one writte hashcode alghorit? is it enought like in my
exemple?
Hendrik Maryns - 22 Jan 2006 22:15 GMT
Carramba uitte de volgende tekst op 01/21/2006 12:17 PM:
> who complex should one writte hashcode alghorit? is it enought like in
> my exemple?
You hashcode function loops. What do you think it will output?
H.
- --
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org
Thomas Hawtin - 21 Jan 2006 11:50 GMT
> it seems strange to me, since hashcode should reffer to place in memory,
> or have I got it wrong?
Not true.
For one thing modern JVMs move objects around in memory, but do not
change the identity hash code. Recent Sun JVMs default to an algorithm
involving the object address at the time the hashCode is first
requested. However the address is not used directly as the lower bits
are not very random (the least significant three will be zero, for
instance). IIRC, the IBM JVM uses a less predictable technique.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/