-2

I am so sure this is not a duplicate although heading may seems used.Please take a look

On what basis hashcode is calculated in JAVA???

On the basis of current state??? I don't think so.

Here is the code:

    initTest inttt2 = new initTest();// this initTest has params: int x and String str
    inttt2.str = "i am object1";
    inttt2.x = 374892762;
    System.out.println(inttt2.hashCode()); // print say 12345
    inttt2.x = 938745;
    inttt2.str = "i am object22222222";
    System.out.println(inttt2.hashCode()); // still print 12345

So here is the question: Do hashcode depends on location in memory if it does not depends on current state???

Do hashcode remains same no matter how much you alter the object??

I am not an expert in data structures but one question is bothering me very much.

This hashcode(particularly Object's native) function have to return an hash value within range of integer so what options do it have other than calculation by its attributes.

Sachin Verma
  • 3,712
  • 10
  • 41
  • 74
  • 3
    `I am so sure this is not a duplicate` Um, _why?_ – SLaks Jun 11 '13 at 17:02
  • Please first at least read the Java docs or enhance your search skills on the net. – Luiggi Mendoza Jun 11 '13 at 17:03
  • hey in this object is altered, not in any other question. i am sorry if again i asked the duplicate question. uhhhhhhhhhhhhhh........ what the hell is wrong with me????? – Sachin Verma Jun 11 '13 at 17:04
  • Seems you are asking all the questions without reading anything. All valid questions but google will help you more. – Juned Ahsan Jun 11 '13 at 17:04
  • guyz mind taking a look on this???? see the accepted answer it says hashcode returns value by current state. IS he wrong??? [link]http://stackoverflow.com/questions/2427631/how-is-hashcode-calculated-in-java – Sachin Verma Jun 11 '13 at 17:11

1 Answers1

3

This is what the documentation says for Object#hashCode().

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

String#hashCode() is more deterministic.

Returns a hash code for this string. The hash code for a String object is computed as

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • hey tell me one thing???? there may be possibility of two object having same hashcode. but how could two different object having different memory location have same hashcode if object's location is within 32 bit.(i guess that Object#hashcode takes address as 32 bit address and returns a 32 bit hashcode then why bother to use hashing) – Sachin Verma Jun 11 '13 at 17:20