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

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

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