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