java hashCode inconsistent ?

While trying to make some kind of header check to see if someone modified the header of my property files I observed something I don’t understand.
I found out that the standard java Method ‘.hashCode’ that every Object provides behave some kind of stange.

Here is my Code

1. StringBuilder sb = new StringBuilder();
   try {
       int i = 0;
2.     for (String s : file2Text.getFileText("./test.txt", "UTF-8")\
                      .split(System.getProperty("line.separator"))) {
3.       if (i++ < 10) {
4.          sb.append(s);
         } else {
} catch (FileNotAccessibleException ex) {
5. System.out.println(String.valueOf(sb.hashCode()));

So I create a StringBuilder (1) and append (4) the first 10 lines (3) from
file ‘./test.txt’ (2). After that if simply print out the hash Code of the
String Builder (5). Note that the file I read in will stay the same.
I run it and it prints out

fine, so far.

Now I transfer my program to a Linux (2.6.18-6-amd64 #1 SMP  2008 x86_64 GNU/Linux) box.
Copy the  file test.txt and rerun my program.

Hmm, well thats not what I expected cause

Maybe something related to the system architecture because locally I’m working with Darwin
(9.6.0 Darwin Kernel Version 9.6.0: root:xnu-1228.9.59~1/RELEASE_I386 i386).

So I already thought that it wouldn’t be possible to make this kind of property header check.
While playing around I then rerun the my code with the same text file on the amd machine.
And suddenly my hash code changes to:


Ok again

1408212765, and again, 1408212765 and again 7281581206 and again 1408212765.


Why does my hash code changes? I read from the same file on the same system changing nothing between the runs.
Shouldn’t a hash value stay the same ?


because of too less time at the moment I’m going to investigate some time whenever there is time to have a closer look at this

Leave a Reply