String Buffer and string build learning Notes

Source: Internet
Author: User

public class Concatenation {public static void main (string[] args) {String mango = "Mango";    String s = "abc" + Mango + "def" + 47;  System.out.println (s); }}/* output:abcmangodef47*///:~

By introducing a piece of code, everyone knows that the "+" and "+ =" operators are the only two overloaded operators in Java (Java does not allow programmers to reload any operators), and "+" can be used to concatenate strings, as shown in the preceding code.

You may all know how this code works, according to the programming idea Book description, you can use the JDK's own tool JAVAP to decompile the above code, the command is as follows: javap-c concatenation here, "-C" means that the JVM code will be generated. I ran. The results are as follows:

Public class concatenation {  public concatenation ();     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 mango        2: astore_1       3: new             #3 &NBSP;&Nbsp;                //  class java/lang/stringbuilder       6: dup        7: invokespecial  #4                    // method java/lang/stringbuilder. " <init> ":() v      10: ldc             #5                    // string abc      12: invokevirtual   #6                    // method java/lang/stringbuilder.append: (ljava/lang/string;) ljava/lang/stringbuilder;       15: aload_1      16: invokevirtual  #6                    // method java/lang/ Stringbuilder.append: (ljava/lang/string;) ljava/lang/stringbuilder;      19:  ldc            #7                    // String def       21: invokevirtual  #6                    // method java/lang/stringbuilder.append :(ljava/lang/string;) ljava/lang/stringbuilder;      24: bipush         47      26: invokevirtual  #8                     // method java/lang/ Stringbuilder.append: (I) ljava/lang/stringbuilder;      29: invokevirtual   #9                    // method java/lang/stringbuilder.tostring: () ljava/lang/string;       32: astore_2      33: getstatic      #10                  //  Field java/lang/system.out:ljava/io/printstream;      36: aload_2       37: invokevirtual  #11         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;METHOD&NBSP;JAVA/IO/PRINTSTREAM.PRINTLN: ( ljava/lang/string;) v  &nBsp;   40: return} 

It is said that the assembly language experience will look familiar, the focus here is the compiler automatically introduced the Java.lang.StringBuilder class, although the source is not using the StringBuilder class, but the compiler took the liberty to use it, because it is more efficient.

StringBuilder is introduced in Java SE5, if you look at the source will notice such a comment

* @author Michael McCloskey * @see Java.lang.StringBuffer * @see java.lang.String * @since 1.5 */public Final Class StringBuilder

Since 1.5, then what is Java used before this, is StringBuffer.

* @author Arthur van Hoff * @see Java.lang.StringBuilder * @see java.lang.String * @since JDK1.0 * * Public Final class StringBuffer

1.0 has already been, and is thread-safe, so the overhead is also larger, so in Java SE5/6 (now to 8), the operation of the string should be faster.

Read the source code will find that the first sentence of the comment on whether the thread is safe to do a description

/* Copyright (c) 1994, Oracle and/or its affiliates. All rights reserved. * ORACLE proprietary/confidential. Use are subject to license terms. */package java.lang;import java.util.arrays;/** * A thread-safe, mutable sequence of characters. * A string buffer is like a {@link string}, and can be modified. At any * point-in-time it contains some particular sequence of characters, but * the length and content of the sequence CA n be changed through certain * method calls.

Look at the source code will find that many methods of the StringBuffer class are using the Synchronized keyword declaration, and StringBuilder is not, StringBuilder is non-thread-safe.

The parent classes of StringBuffer and StringBuilder are Abstractstringbuilder, and their constructors are constructed by calling the parent class.

/** * Constructs a string buffer with no characters in it and an * initial capacity of characters.    */Public StringBuffer () {super (16); }
/** * Constructs a string builder with no characters in it and an * initial capacity of characters.    */Public StringBuilder () {super (16); }
/** * Creates an abstractstringbuilder of the specified capacity.    */abstractstringbuilder (int capacity) {value = new char[capacity]; }


String Buffer and string build learning notes

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.