public class StringConcat { public static void main(String[] args) { String str = "aa" + "bb" + "cc" + "dd"; System.out.println("str : " + str); } }
先用 javac 编译,再用 javap 反编译
public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=3, locals=2, args_size=1 0: ldc #2 // String aabbccdd 2: astore_1 3: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 6: new #4 // class java/lang/StringBuilder 9: dup 10: invokespecial #5 // Method java/lang/StringBuilder."<init>":()V 13: ldc #6 // String str : 15: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 18: aload_1 19: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 22: invokevirtual #8 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 25: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 28: return
public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=1 0: ldc #2 // String aabbccdd 2: astore_1 3: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 6: aload_1 7: invokedynamic #4, 0 // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;)Ljava/lang/String; 12: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 15: return
-XX:+PrintStringTableStatistics
查看 -XX:StringTableSize=N
调整大小,但绝大部分情况下不需要调整 -XX:+UseStringDeduplication
$ java -XX:+PrintStringTableStatistics -version java version "1.8.0_191" Java(TM) SE Runtime Environment (build 1.8.0_191-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode) SymbolTable statistics: Number of buckets : 20011 = 160088 bytes, avg 8.000 Number of entries : 9616 = 230784 bytes, avg 24.000 Number of literals : 9616 = 380296 bytes, avg 39.548 Total footprint : = 771168 bytes Average bucket size : 0.481 Variance of bucket size : 0.483 Std. dev. of bucket size: 0.695 Maximum bucket size : 5 StringTable statistics: Number of buckets : 60013 = 480104 bytes, avg 8.000 Number of entries : 672 = 16128 bytes, avg 24.000 Number of literals : 672 = 45472 bytes, avg 67.667 Total footprint : = 541704 bytes Average bucket size : 0.011 Variance of bucket size : 0.011 Std. dev. of bucket size: 0.106 Maximum bucket size : 2
-XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining
查看 $ java -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining -version 64 1 3 java.lang.String::hashCode (55 bytes) 66 2 3 java.lang.String::charAt (29 bytes) @ 18 java/lang/StringIndexOutOfBoundsException::<init> (not loaded) not inlineable 67 3 3 java.lang.String::length (6 bytes) 68 4 n 0 java.lang.System::arraycopy (native) (static) 68 5 3 java.lang.String::equals (81 bytes) java version "1.8.0_191" Java(TM) SE Runtime Environment (build 1.8.0_191-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
转载请注明出处:http://zhongmingmao.me/2019/04/27/java-core-string/
访问原文「Java核心 -- 字符串」获取最佳阅读体验并参与讨论