String解读
一 、背景
工资5000的我今天参加了个视频面试、面试官问了我String,说实话我还真的不太会,下班后,从满是尘土的阳台上翻出来我那本独家秘籍《Think in JAVA》 虽然是个盗版,但是确实挺厚的。我翻到了目录找到了第十三章,上面写着三个大字 字符串 308页,我马上翻到308也,结果没有找到这三个大字、我瞬间想起来我买的是拼XX的书,但是我很机智,一点点往后翻,找到了503页 ,这就是为啥这么多人要抵制盗版了,但是对于一个非常穷逼的我来说,有本实体书已经是很奢侈了。
二、看书总结
- 不可变的String
- String是个final 也就说不可变
- String中的每一个能修改它的方法实际都是New了一个新的String
- “+” 和StringBuilder可以通过javap看到,"+"最后编译成了StringBuilder,但是效率不一样。
- String可以通过反射修改,本质是一个字符数组。
- String a =“abc”; 首先看常量池有没有,有的话指向,没有的话创建,并指向。
- String a =“abc”; String a=new String(“abc”); 不一样,一个常量池,一个堆内存。
三、"+" 和StringBuilder字节码
public class StringOption {
public String whitherStringBuilder(String[] args) {
String result = "";
for (int i = 0; i < args.length; i++) {
result += args[i];
}
return result;
}
public String whitherStringBuilder2(String[] args) {
String result = "";
for (String arg : args) {
result += arg;
}
return result;
}
public String explicit(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < args.length; i++) {
sb.append(args[i]);
}
return sb.toString();
}
}
字节码
public class com.nw.luban.StringOption {
public com.nw.luban.StringOption();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public java.lang.String whitherStringBuilder(java.lang.String[]);
Code:
0: ldc #2 // String
2: astore_2
3: iconst_0
4: istore_3
5: iload_3
6: aload_1
7: arraylength
8: if_icmpge 38
11: new #3 // class java/lang/StringBuilder
14: dup
15: invokespecial #4 // Method java/lang/StringBuilder."<init>":()V
18: aload_2
19: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
22: aload_1
23: iload_3
24: aaload
25: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
28: invokevirtual #6 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
31: astore_2
32: iinc 3, 1
35: goto 5
38: aload_2
39: areturn
public java.lang.String whitherStringBuilder2(java.lang.String[]);
Code:
0: ldc #2 // String
2: astore_2
3: aload_1
4: astore_3
5: aload_3
6: arraylength
7: istore 4
9: iconst_0
10: istore 5
12: iload 5
14: iload 4
16: if_icmpge 51
19: aload_3
20: iload 5
22: aaload
23: astore 6
25: new #3 // class java/lang/StringBuilder
28: dup
29: invokespecial #4 // Method java/lang/StringBuilder."<init>":()V
32: aload_2
33: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
36: aload 6
38: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
41: invokevirtual #6 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
44: astore_2
45: iinc 5, 1
48: goto 12
51: aload_2
52: areturn
public java.lang.String explicit(java.lang.String[]);
Code:
0: new #3 // class java/lang/StringBuilder
3: dup
4: invokespecial #4 // Method java/lang/StringBuilder."<init>":()V
7: astore_2
8: iconst_0
9: istore_3
10: iload_3
11: aload_1
12: arraylength
13: if_icmpge 30
16: aload_2
17: aload_1
18: iload_3
19: aaload
20: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
23: pop
24: iinc 3, 1
27: goto 10
30: aload_2
31: invokevirtual #6 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
34: areturn
}
四、解读
https://www.jianshu.com/p/6a8997560b05
这篇写的很详细,有兴趣的可以学习一下。
在这里插入图片描述
在这里插入图片描述
效率问题可想而知。
五、idea安装javap