Created
January 18, 2013 12:14
-
-
Save caoxudong/4564214 to your computer and use it in GitHub Desktop.
关于字符串拼接的一个记录。以前没有注意,null与字符串拼接时会多出一个"null"字符串来。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// access flags 0x9 | |
public static main(String[]) : void | |
L0 | |
LINENUMBER 43 L0 | |
ACONST_NULL | |
ASTORE 1 | |
L1 | |
LINENUMBER 44 L1 | |
NEW StringBuilder | |
DUP | |
ALOAD 1: a | |
INVOKESTATIC String.valueOf(Object) : String | |
INVOKESPECIAL StringBuilder.<init>(String) : void | |
LDC "a" | |
INVOKEVIRTUAL StringBuilder.append(String) : StringBuilder | |
INVOKEVIRTUAL StringBuilder.toString() : String | |
ASTORE 1: a | |
L2 | |
LINENUMBER 45 L2 | |
GETSTATIC System.out : PrintStream | |
ALOAD 1: a | |
INVOKEVIRTUAL PrintStream.println(String) : void | |
L3 | |
LINENUMBER 47 L3 | |
RETURN | |
L4 | |
LOCALVARIABLE args String[] L0 L4 0 | |
LOCALVARIABLE a String L1 L4 1 | |
MAXSTACK = 3 | |
MAXLOCALS = 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
在bytecode.txt的第12行表明了,在做字符串拼接前,会调用String.valueOf方法,当传入该方法的参数为null时,会返回字符串"null"。 | |
所以才会有如output.txt那样的结果。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nulla |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.java.lang.string; | |
import java.util.regex.Pattern; | |
public class TestString { | |
public static void main(String[] args) { | |
String a = null; | |
a += "a"; | |
System.out.println(a); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment