0

String in Java is Immutable. When we use String literal (double quotes) to create a String, it first looks for String with same value in the String pool.

String first = "abc";

My question is what's the searching algorithm does the JVM searches for "abc"? It just loop the whole String Pool values?

1
  • 2
    OP wants to know not just the concept of String pool but the actual searching algorithm. Check this answer https://stackoverflow.com/a/35498461/10317684. Short answer would be it depends but most likely hashing. Commented Dec 10, 2018 at 4:13

1 Answer 1

2

It just loop the whole String Pool values?

No.

My understanding is that the string pool is a form of hashtable implemented in native code.

So the search algorithm is a hashtable algorithm. Under normal circumstances, interning a string is an amortized O(1) operation.

(The linked Q&A includes some links to the C++ source code.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.