java String comparison in concat -


package data; public class {      string s = "maew";         string s2 = s + "class";         string s1 = "maewclass";         system.out.println(s2 == s1);     } } 

but both in string constant pool , if same content object created 1 more reference not created. s2 , s1 should point same object in string constant pool.so answer should true why giving false

string constant pool internal java feature should never rely on. instance following code produce "true"

string s1 = "hello"; string s2 = "hello"; boolean result = s1 == s2; 

but following code produce "false":

string s1 = "hello"; string s2 = new string("hello"); boolean result = s1 == s2; 

string constant pool behavior may change 1 java version since internal optimization feature. shouldn't relied on. in case, suspect because used string s2 = s + "class"; did create new instance.

in case string comparison must done method equals() of class string


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -