final字符串连接
/*!!!!!!final字符串连接+*/
@Test
public void fianlStringTest() {
final String str2 = "a";
final String str3 = "b";
String str4 = str2 + str3;
String str5 = "ab";
System.out.println("1: " + (str4 == str5));//true
final String str8 = "d";
String str9 = "c" + str8 + "e";
String str89 = "cde";
System.out.println("2: " + (str9 == str89)); //true
final String strA = "f";
String strB = "g";
String strS = strA + strB;
String strPool = "fg";
System.out.println("3:" + (strPool == strS)); //false
}
final字符串连接-字节码
// access flags 0x1
public fianlStringTest()V
@Lorg/junit/Test;()
L0
LINENUMBER 55 L0
LDC "a"
ASTORE 1
L1
LINENUMBER 56 L1
LDC "b"
ASTORE 2
L2
LINENUMBER 57 L2
LDC "ab"
ASTORE 3
L3
LINENUMBER 58 L3
LDC "ab"
ASTORE 4
L4
LINENUMBER 59 L4
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.
LDC "1: "
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
ALOAD 3
ALOAD 4
IF_ACMPNE L5
ICONST_1
GOTO L6
L5
FRAME FULL [string/IfEqualsTest java/lang/String java/lang/String java/lang/String java/lang/String] [java/io/PrintStream java/lang/StringBuilder]
ICONST_0
L6
FRAME FULL [string/IfEqualsTest java/lang/String java/lang/String java/lang/String java/lang/String] [java/io/PrintStream java/lang/StringBuilder I]
INVOKEVIRTUAL java/lang/StringBuilder.append (Z)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
L7
LINENUMBER 61 L7
LDC "d"
ASTORE 5
L8
LINENUMBER 62 L8
LDC "cde"
ASTORE 6
L9
LINENUMBER 63 L9
LDC "cde"
ASTORE 7
L10
LINENUMBER 64 L10
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.
LDC "2: "
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
ALOAD 6
ALOAD 7
IF_ACMPNE L11
ICONST_1
GOTO L12
L11
FRAME FULL [string/IfEqualsTest java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String] [java/io/PrintStream java/lang/StringBuilder]
ICONST_0
L12
FRAME FULL [string/IfEqualsTest java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String] [java/io/PrintStream java/lang/StringBuilder I]
INVOKEVIRTUAL java/lang/StringBuilder.append (Z)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
L13
LINENUMBER 66 L13
LDC "f"
ASTORE 8
L14
LINENUMBER 67 L14
LDC "g"
ASTORE 9
L15
LINENUMBER 68 L15
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.
LDC "f"
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
ALOAD 9
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
ASTORE 10
L16
LINENUMBER 69 L16
LDC "fg"
ASTORE 11
L17
LINENUMBER 70 L17
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.
LDC "3\uff1a"
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
ALOAD 11
ALOAD 10
IF_ACMPNE L18
ICONST_1
GOTO L19
L18
FRAME FULL [string/IfEqualsTest java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String] [java/io/PrintStream java/lang/StringBuilder]
ICONST_0
L19
FRAME FULL [string/IfEqualsTest java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String java/lang/String] [java/io/PrintStream java/lang/StringBuilder I]
INVOKEVIRTUAL java/lang/StringBuilder.append (Z)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
L20
LINENUMBER 71 L20
RETURN
L21
LOCALVARIABLE this Lstring/IfEqualsTest; L0 L21 0
LOCALVARIABLE str2 Ljava/lang/String; L1 L21 1
LOCALVARIABLE str3 Ljava/lang/String; L2 L21 2
LOCALVARIABLE str4 Ljava/lang/String; L3 L21 3
LOCALVARIABLE str5 Ljava/lang/String; L4 L21 4
LOCALVARIABLE str8 Ljava/lang/String; L8 L21 5
LOCALVARIABLE str9 Ljava/lang/String; L9 L21 6
LOCALVARIABLE str89 Ljava/lang/String; L10 L21 7
LOCALVARIABLE strA Ljava/lang/String; L14 L21 8
LOCALVARIABLE strB Ljava/lang/String; L15 L21 9
LOCALVARIABLE strS Ljava/lang/String; L16 L21 10
LOCALVARIABLE strPool Ljava/lang/String; L17 L21 11
MAXSTACK = 4
MAXLOCALS = 12
String toLowerCase()
@Test
public void toLowerCaseTest() {
String s1 = "Hello";
String s2 = "hello";
String s3 = s1.toLowerCase();//new String("hello")
String s4 = s2.toLowerCase();//没有改变返回this 所以s3=s4
System.out.println(s1 == s3);//false
System.out.println(s1 == s4);//false
System.out.println(s2 == s3);//false
System.out.println(s2 == s4);//true
System.out.println(s2.equals(s3));//true 比较的是内容
}
标签:lang,Ljava,java,String,StringBuilder,字符串,INVOKEVIRTUAL
From: https://www.cnblogs.com/haveanicedayfh/p/17129537.html