What is String pool in Java? -


i confused stringpool in java. came across while reading string chapter in java. please me understand, in layman terms, stringpool does.

this prints true (even though don't use equals method: correct way compare strings)

    string s = "a" + "bc";     string t = "ab" + "c";     system.out.println(s == t); 

when compiler optimizes string literals, sees both s , t have same value , need 1 string object. it's safe because string immutable in java.
result, both s , t point same object , little memory saved.

name 'string pool' comes idea defined string stored in 'pool' , before creating new string object compiler checks if such string defined.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -