String s1 = "a"; String s2 = "b"; String s3 = "ab"; String s6 = s1+s2; System.out.println("s3==s6 -> "+ (s3==s6)); final String s8 = "a" ; final String s9 = "b" ; String s10 = s8 + s9; System.out.println("s3==s10 -> "+ (s3==s10)); output: s3==s6 -> false s8==s1 -> true 字节码: 0: ldc #16 // String a 2: astore_1 3: ldc #18 // String b 5: astore_2 6: ldc #20 // String ab 8: astore_3 // s6的过程,这个我知道 17: new #22 // class java/lang/StringBuilder 20: dup 21: aload_1 22: invokestatic #24 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String; 25: invokespecial #30 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V 28: aload_2 29: invokevirtual #33 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 32: invokevirtual #37 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 35: astore // 下面是问题 // 同样是相加,为什么s10不走s6的过程? 而是直接取#20。 48: ldc #16 // String a //s8 50: astore 8 52: ldc #18 // String b //s9 54: astore 9 56: ldc #20 // String ab /s10 58: astore 10
wldraa