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

来源:互联网 发布:纯电动汽车销量数据 编辑:程序博客网 时间:2024/04/28 11:31
/** *  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}
1 0
原创粉丝点击