string interning - what is Intern() in java? -


this question has answer here:

public static void main(string[] args) {     string literalstr = "abc";     string literalstr2 = "abc";     string str = new string("abc");     string str2 = new string("abc");   if (literalstr == literalstr2) {      system.out.println("literal string... use string pooling"); } if (str != str2) {       system.out.println("object string... dont use string pooling"); } if (str.intern() == str2.intern()) {      system.out.println("interning ... use string pooling"); }     // system.out.println(ric2); } 

what purpose of intern()?

just read java doc, , tell :

returns canonical representation string object.

a pool of strings, empty, maintained privately class string.

when intern method invoked, if pool contains string equal string object determined equals(object) method, string pool returned. otherwise, string object added pool , reference string object returned.

it follows 2 strings s , t, s.intern() == t.intern() true if , if s.equals(t) true.

all literal strings , string-valued constant expressions interned. string literals defined in §3.10.5 of java language specification


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -