What is String pool in Java? -
this question has answer here:
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
Post a Comment