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?

Yingkai
  • 28
  • 5
  • 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](https://stackoverflow.com/a/35498461/10317684). Short answer would be it depends but most likely hashing. – Ricky Mo Dec 10 '18 at 04:13

1 Answers1

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216