Java side question the most ripped string

Source: Internet
Author: User


The ToString method inside SB is as follows, why need this, look at the JVM virtual machine instruction, the + number will become new SB (), and then call the ToString method
  Public String toString () {        //Create a copy, don ' t share the array        return new String (value, 0, count);    }

  


public class Threadexception{public static void Main (string[] args) {
No final code string hello = "Hello"; String hel = "hel"; String lo = "Lo"; System.out.println (Hello = = "hel" + "lo"); System.out.println (Hello = = "hel" + lo);}}
The code after the final code is back-compiled
import java.io.printstream;public class Threadexception{public static void main (string[] paramarrayofstring) {String str1 = "Hello"; String str2 = "hel"; String str3 = "Lo"; System.out.println (str1 = = "Hello"); System.out.println (str1 = = "Hel" + STR3);}} 
The instruction set executed by the virtual machine without final is the following public static void Main (java.lang.string[]); code:0: LDC #2//String HELLO2:ASTORE_13:LDC #3//String HEL5:ASTORE_26:LDC #4//String Lo8:astore_3//The value of the top of the operand stack is guaranteed Local variable table exists, variable address place local variable table 3 position 9:getstatic #5//Field JAVA/LANG/SYSTEM.OUT:LJAVA/IO/PRINTSTREAM;12:ALOAD_113:LDC #2//String Hello15:if_acmpne 2218:iconst_119:goto 2322:iconst_023:invokevirtual #6//Method java/io/printstream.println: (Z) V26 : getstatic #5//Field java/lang/system.out:ljava/io/printstream;29:aload_130:new #7//Class Java/lang/stringbuilder3 3:dup34:invokespecial #8//Method java/lang/stringbuilder. " <init> ":() V37:LDC #3//String hel//This sentence loads the constant to the operand stack hel39:invokevirtual #9//Method Java/lang/stringbuilder.appe Nd: (ljava/lang/string;) ljava/lang/stringbuilder;42:aload_3//loading a local variable into the operand stack, from the local variable table 3, place the value inside the operand stack 43:invokevi Rtual #9//Method java/lang/stringbuilder.append: (ljava/lang/string;) ljava/lang/stringbuilder;46:invokevirtual #10 Method Java/lang/stringbuIlder.tostring: () Ljava/lang/string;//sb.tostring49:if_acmpne 5652:iconst_153:goto 5756:iconst_057:invokevirtual # 6//Method java/io/printstream.println: (Z) V60:return

Look at the following, add a final

The final code is added.
public class Threadexception{public static void Main (string[] args) {String hello = "Hello"; String hel = "hel"; final string lo = "Lo"; System.out.println (Hello = = "hel" + "Lo"),//truesystem.out.println (hello = = "hel" + lo);//true}}
Add final post-decompile code
Import Java.io.printstream;public class threadexception{public static void Main (string[] paramarrayofstring) { String str4 = "Hello"; String STR5 = "hel"; System.out.println (STR4 = = "Hello"); System.out.println (STR4 = = "Hello");} }

Look under the final virtual machine code public static void Main (java.lang.string[]); code:0: LDC #2//String Hello   //load constant to the operand stack, constant is HELLO2:ASTORE_13:LDC #3//String hel5:astore_26:getstatic #4//Fie LD JAVA/LANG/SYSTEM.OUT:LJAVA/IO/PRINTSTREAM;9:ALOAD_110:LDC #2//String Hello12:if_acmpne 1915:iconst_116:goto 2019 : iconst_020:invokevirtual #5//Method java/io/printstream.println: (Z) v23:getstatic #4//Field Java/lang/system.out:l JAVA/IO/PRINTSTREAM;26:ALOAD_127:LDC #2//String Hello  load constant 29:if_acmpne 36 compare 32:iconst_133:goto 3736:iconst_ 037:invokevirtual #5//Method java/io/printstream.println: (Z) V40:return}

Look at this code again.

public class threadexception{
Added finalpublic static void Main (string[] args) {string s = new String ("ABC"); final string S1 = "abc"; String s2 = new String ("ABC"); Final string s3 = "ABC"; System.out.println (s = = S.intern ()); System.out.println (S1 = = S2.intern ()); System.out.println (s2.intern () = = S2.intern ()); System.out.println (s3 = = S1);}}
Added final anti-compile back code import java.io.printstream;public class threadexception{public  static void Main (string[] paramarrayofstring)  {    string str1 = new String ("abc");    String str2 = new String ("abc");    System.out.println (str1 = = Str1.intern ());    SYSTEM.OUT.PRINTLN ("abc" = = Str2.intern ());    System.out.println (str2.intern () = = Str2.intern ());//This paragraph is not very surprised    System.out.println (true);}  } Isn't that ridiculous?

See, no final.

public class Threadexception{public static void Main (string[] args) {No final code string s = new String ("abc"); String S1 = "abc"; String s2 = new String ("abc"); String s3 = "ABC"; System.out.println (s = = S.intern ()); System.out.println (S1 = = S2.intern ()); System.out.println (s2.intern () = = S2.intern ()); System.out.println (s3 = = S1);}}
No final anti-compiled code import java.io.printstream;public class threadexception{public  static void Main (string[] paramarrayofstring)  {    string str1 = new String ("abc");    String str2 = "abc";    String str3 = new String ("abc");    String STR4 = "abc";    System.out.println (str1 = = Str1.intern ());    System.out.println (str2 = = Str3.intern ());    System.out.println (str3.intern () = = Str3.intern ());    System.out.println (STR4 = = str2);}  }

is not all feel very nonsense, or C + + primer inside that sentence, for the literal string processing, some compilers will save one, some will save multiple copies, so,

To determine if string literals are equal, honestly for loops

Java side question the most ripped string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.