Search code examples
javastringbuilderhashcode

Why does changing StringBuilder change its hashCode?


this is input.

    public static void main(String[] args){

        StringBuilder builder = new StringBuilder("hello");
        printBuilder(builder);

        // append
        builder.append(" everyone");
        printBuilder(builder);

        builder.append(", what's up?");
        printBuilder(builder);
    }

    private static void printBuilder(StringBuilder dataBuild){
    StringBuilder build = new StringBuilder(dataBuild);
    System.out.println("data = " + build);
    System.out.println("length = " + build.length());
    System.out.println("capacity = " + build.capacity());
    int addressBuild = System.identityHashCode(build);
    System.out.println("address = " + Integer.toHexString(addressBuild);
    }

this is output.

  • data = hello
  • length = 25
  • capacity = 21
  • address = 5b480cf9
  • data = hello everyone
  • length = 14
  • capacity = 30
  • address = 6f496d9f
  • data = hello everyone, what's up?
  • length = 26
  • capacity = 42
  • address = 723279cf

why the address different from other? i though will be the same. i tried different like insert, replace, charAt, but still the address was different. can anyone tell me why?.


Solution

  • Inside printBuild(), you make a new StringBuilder with every call. The hash is printed for the new builder every time:

    int addressBuild = System.identityHashCode(build);
    

    You can't reasonably expect a different result. If you want to see the same hash every time, remove the following line:

    StringBuilder build = new StringBuilder(dataBuild);
    

    Operate on the input argument dataBuild directly: it's passed in as a reference.

    To elaborate: System.identityHashCode produces a result that is only guaranteed to be identical if the objects are the same (as in ==, not equals()). If course, as with any hash, two unequal objects may have the same hash, but the function does try to reduce overlap. As expected, the result is often based on memory location, although it generally isn't the memory location itself.