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

/**
 *  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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值