您的位置:首页 > 其它

怎么理解字符串常量和字符串字面量

2016-03-14 15:18 295 查看
/**
*  1) 字符串字面量和常量称为“静态字符串”<br>
*  2) 字面量和常量的连接在编译期间执行,优化为一个静态字符串<br>
*  3) 在运行期间,Java在静态缓冲池中创建静态字符串,并且尽量使用同一个字符串对象。<br>
*  4) 动态字符串:字符串运算结果,或者连接结果或者 new运算创建的字符串,等运行期间创建的字符串不参与静态优化<br>
* @author Heying
*
*/


public static final String S = "123ABC";
public static final String SS = "ABC";
public static void main(String[] args) {
String s1 = "123ABC";
String s2 = 123 + SS;
String s3 = 123 + "ABC";
String s4 = 1+2+3+ "ABC";
String s5 = "1"+2+3+ "ABC";
String s6 = '1'+2+3+ "ABC";
String s7 = "ABC";
String s8 = 123+s7;
String s9 = new String("123ABC");
String s10 = "123abc".toUpperCase();

System.out.println(s1); //123ABC
System.out.println(s2); //123ABC
System.out.println(s3); //123ABC
System.out.println(s4); //6ABC
System.out.println(s5); //123ABC
System.out.println(s6); //54ABC
System.out.println(s7); //ABC
System.out.println(s8); //123ABC
System.out.println(s9); //123ABC
System.out.println(s10); //123ABC
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: