String object and String literal are different

String s = new String("hello");

String t = "hello";

In above code, s is a String object and t is a String literal, that means 's == t ' is not true.

String literals are in 'constant pool' and are compiled into .class file. While String Object is dynamically constructed in running time.

 

Likewise, the a and b in the bellow code is not '==' each other but 'equals' each other.

String a = "";

String b = new String("");

 

String is an constant object, which means you can't change it's value. This is exactly why StringBuilder is introduced.

你可能感兴趣的:(object,String,equals,each)