简体   繁体   中英

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?.

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.

System#identityHashCode does not return the address of the object.

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode(). The hash code for the null reference is zero.

Each object of a class has a different hash code. Since you are creating a new object with each call to printBuilder , you are getting a different hash code.

Note: The comparison using == returns true for the references having equal hash codes.

class Main {
    public static void main(String[] args) {
        Boolean b1 = true;
        Boolean b2 = Boolean.valueOf("true");
        System.out.println(System.identityHashCode(b1));
        System.out.println(System.identityHashCode(b2));
        System.out.println(b1 == b2);

        String s1 = "Hello";
        String s2 = s1;
        String s3 = new String("Hello");// Creates a new object and hence s3 will have a different hash code
        System.out.println(System.identityHashCode(s1));
        System.out.println(System.identityHashCode(s2));
        System.out.println(s1 == s2);
        System.out.println(System.identityHashCode(s3));
        System.out.println(s2 == s3);
    }
}

Output:

992136656
992136656
true
511833308
511833308
true
1297685781
false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM