Sting Constant Variable

概述

类、方法、变量尽量指定final修饰

字符串拼接背后不一定是StringBuilder

代码1

public class Constant {

         public static void main(String[] args) {

         }

         /**

              0: ldc           #8                  // String hello

         2: astore_0

         3: ldc           #46                 // String helloworld

         5: astore_1

         6: ldc           #48                 // String hellohelloworld

         8: astore_2

         9: getstatic     #31                 // Field java/lang/System.out:Ljava/io/PrintStream;

        12: aload_2

        13: invokevirtual #37                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V

        16: return

          * */     

     //没有出现StringBuilder.append

         public static void f1() {

                   final String x="hello";

             final String y=x+"world";

             String z=x+y;

             System.out.println(z);

         }

         /**

             0: ldc           #19                 // String hello

         2: astore_1

         3: ldc           #21                 // String helloworld

         5: astore_2

         6: new           #42                 // class java/lang/StringBuilder

         9: dup

        10: ldc           #19                 // String hello

        12: invokespecial #44  // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V

        15: aload_2

        16:invokevirtual#46 //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;

        19: invokevirtual #50           // Method java/lang/StringBuilder.toString:()Ljava/lang/String;

        22: astore_3

        23: getstatic     #25                 // Field java/lang/System.out:Ljava/io/PrintStream;

        26: aload_3

        27: invokevirtual #31                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V

        30: return

          * */

         public void f2(){

               final String x="hello";

               String y=x+"world";

               String z=x+y;

               System.out.println(z);

         }

}

代码2


public class StringConstant {

                   public static void main(String[] args) {

                 String hello = "Hello", lo = "lo";

                 System.out.print((hello == "Hello") );      //都是常量池的内容,true

                 System.out.print((Other.hello == hello) );  //true

                 System.out.print((hello == ("Hel"+"lo")) );  //true

                 System.out.print((hello == ("Hel"+lo)));     //false

                 System.out.println(hello == ("Hel"+lo).intern());  //ture

             }

                   public static class Other{

                            public static String hello = "Hello";

                   }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值