How Hash of a String Literal in String Literal Pool Computed?
NickName:Shashi Ask DateTime:2016-02-10T22:15:14

How Hash of a String Literal in String Literal Pool Computed?

I was seeing the implementation of String class in Java, and the constructor of this goes like this

public String(String var1) {
        this.value = var1.value;
        this.hash = var1.hash;
    }

So, if you'll do

String s1 = "String";
String s2 = new String("String");

even though s1 and s2 are two different objects, s1.hashCode() == s2.hashCode() returns true as expected, because in it's constructor, String Class takes hash from it's String literal.
I've deduced that hash of this String literal is computed when String literal is created in String literal Pool, and not when String object is created.
My question is, how and where the hash of this String literal is computed in Java?
is there some native implementation for this?

Copyright Notice:Content Author:「Shashi」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35317546/how-hash-of-a-string-literal-in-string-literal-pool-computed

Answers
Neil 2016-02-10T14:27:11

If we look at the code for String, we see that the value of hash is initially zero. When hashCode is called, it defaults to the value of hash unless it is zero in which case the calculation is performed. So when is hash calculated before it gets passed in the copy constructor of String?\n\nSimply put, it doesn't. It copies the value of hash of the passed string, which, if the hash had been calculated, would be non-zero. Otherwise the new string instance takes on a hash value of 0 and both instances could potentially perform the same calculation twice. \n\nIt is a reasonable optimization since you wouldn't want to force calculation of the hash code if it isn't necessary. The real optimization is recycling of string instances to avoid copying in the first place.",


Robert Bräutigam 2016-02-10T14:23:02

Maybe this is what you are looking for: \n\nhttp://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.hashCode%28%29\n\nIt is right there in String, will be cached in an internal variable. Admittedly I don't know whether there is some magic in literal strings that sidesteps this implementation.",


More about “How Hash of a String Literal in String Literal Pool Computed?” related questions

How Hash of a String Literal in String Literal Pool Computed?

I was seeing the implementation of String class in Java, and the constructor of this goes like this public String(String var1) { this.value = var1.value; this.hash = var1.hash; ...

Show Detail

How String Literal Pool Works

String str = new String("Hello"); Normally I have read, in many articles available on internet, that two objects are created when we write the statement above. One String object is created on the ...

Show Detail

Java String literal pool and string object

I have known that JVM maintains a string literal pool to increase performance and maintain JVM memory and learned that string literal is maintained in the string pool. But I want to clarify something

Show Detail

How does Java initialize String literal

Javadoc said that: The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. We know that String class has ...

Show Detail

Java String Literal Pool

Let's say we have the following code String x = new String("xyz"); String y = "abc"; x = x + y; A friend of mine says that 4 objects are created in total, where I say that only 3 are created. Can

Show Detail

String Literal Pool

Well, I've got such a code String s = "hello"; String s2 = s + "world"; I know that variable s is stored in Java Heap in String Literal Pool. Also I know that variable s2 is stored in Java Heap (

Show Detail

In Java, when we print a string literal on to the terminal, does this string literal also be stored in the string pool?

I am aware that when we initialize a string literal to a variable this literal will be stored in the string pool by the JVM. Consider the piece of code below. System.out.println("This is a str...

Show Detail

java: readLine method put string into string literal pool?

I know that String a = "hello"; will put the "hello" into string literal pool. my question is: 1. String a = "hello"; String b = "hell"+"o"; Does the string literal pool has

Show Detail

Is String Literal Pool a collection of references to the String Object, Or a collection of Objects

I am all confused after reading the article on javaranch site by Corey McGlone, The author of The SCJP Tip Line. named Strings, Literally and the SCJP Java 6 Programmer Guide by Kathy Sierra (co-f...

Show Detail

String literal memory utilization

In java there is concept of string literal pool. If I am not creating any string in my code, this memory pool is waste for me. How can I use this memory area instead of keeping it for string litera...

Show Detail