符号引用和直接引用
总结:JVM对于直接引用和符号引用的处理是有区别的,可以看到符号引用时,JVM将使用StringBuilder来完成字符串的 添加,而直接引用时则直接使用String来完成;直接引用永远比符号引用效率更快,但实际应用开发中不可能全用直接引用,要提高效能可以考虑按虚拟机的思维来编写你的程序。1.0 直接引用:public class StringAndStringBuilder{
总结:JVM对于直接引用和符号引用的处理是有区别的,可以看到符号引用时,JVM将使用StringBuilder来完成字符串的 添加,而直接引用时则直接使用String来完成;直接引用永远比符号引用效率更快,但实际应用开发中不可能全用直接引用,要提高效能可以考虑按虚拟机的思维来编写你的程序。
1.0 直接引用:
public class StringAndStringBuilder{
public static void main(String[] args){
System.out.println ("s=" + "asdfa");
}
}
反编译后的:
F:\java\res\字节码>javap -c StringAndStringBuilder
Compiled from "StringAndStringBuilder.java"
public class StringAndStringBuilder extends java.lang.Object{
public StringAndStringBuilder();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2; //String asdfa
2: astore_1
3: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
6: ldc #4; //String s=asdfa
8: invokevirtual #5; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
11: return
}
2.0 符号引用:
public class StringAndStringBuilder{
public static void main(String[] args){
String s="asdfa";
System.out.println ("s=" + s);
}
}
反编译后的:
F:\java\res\字节码>javap -c StringAndStringBuilder
Compiled from "StringAndStringBuilder.java"
public class StringAndStringBuilder extends java.lang.Object{
public StringAndStringBuilder();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2; //String asdfa
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 s=
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/la
ng/String;
25: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
28: return
}
更多推荐
所有评论(0)