Source code-String class

Source: Internet
Author: User
1. ClassificationBasic properties: String The final attribute is a char[] value; Construct method area: More complex is constructed by Unicode code and byte[]; string comparisons: Equality, size (sort); query: IndexOf, StartsWith, Endwith, contains interception: subString, Tool method: Format printing, Unicode code point related to the bit operation method; 2. The basic

Why is the object-oriented encapsulation idea masking a lot of low-level detail, the string class as our most frequently used class, has helped us achieve many functions, 2.1 immutability

There are many immutable classes in the JDK, the so-called immutable classes are not expressed in the string declaration of final, but the internal value is not allowed to be modified, all operations on value is to return a new object, such as BigDecimal, the following is the wrong way

String str = "  Hello, hi";
Str.trim ();
Str.replace (', ', ', '); the line is not compiled, the Chinese char is problematic
System.out.println (str);
2.2 string constant pool

The JVM establishes the concept of a constant pool in order to improve the efficiency of 8 basic types (Short/int/long/double/float/char/boolean/byte) and string. These variables are placed into the constant pool portion of the class file after being compiled into a class file. A string constant pool is created by the JVM runtime, and the precompiled string is added to the constant pool, and the string can be added to the constant pool by intern;

public class Stringpooltest {public

    static final string str1 = "a";//A entering a constant pool, str1 using a constant pool to refer to public

    final string str 2 = "B"; b into the constant pool, STR2 uses a constant pool to refer to the public

    String STR3 = "C";//C into a constant pool, str3 use a constant pool to refer to the public

    final String STR4 = str2 + str3;// BC does not enter the constant pool, is actually STR4 = new StringBuilder (str2). Append (STR3). toString ();

    public static void Main (string[] args {
        string str5 = "D";//D into the constant pool
        final String str7 = "F";//F Enter constant pool
        fina L String str8 = str5 + "J"; The DJ does not enter the constant pool is actually str8 = new StringBuilder (STR5). Append (j). ToString ()}

}

Use Javap-v to decompile the details of the view byte code as follows

 public class Com.saillen.demo.lang.StringPoolTest Minor version:0 major version:52 Flags:acc_public, Acc_super C  Onstant Pool: ... #4 = String #44//C//The StringBuilder is explicitly introduced here, but there is no import in our code #6 =            Class #46//Java/lang/stringbuilder ...//indicates that the reference value of D is defined in #51 #12 = String #51             D #13 = string #52/E #14 = string #53//F #15 = string
  #54//J. ...//#17 = Utf8 str1 #20 = String #56/A #21 = Utf8 str2 #22 = Utf8 str3 #23 = Utf8 STR4 #35 = Utf8 s               TR5 #36 = Utf8 str6 #37 = Utf8 str7 #38 = Utf8 str8 ... #42 = Utf8               b #44 = Utf8 c #51 = Utf8 D #52 = Utf8 e #53 = Utf8      F #54 = Utf8         J #56 = Utf8 initialization instruction public com.saillen.demo.lang.StringPoolTest () for a {//class; Descriptor: () V flags:acc_public code:stack=3, Locals=1, args_size=1//loading B,STR1 code was optimized, because it was not used and            is very intelligent//here JDK8 optimizes the useless code, but JDK7, 6 is not///optimized but exists in the constant pool because str1 can be used in other classes, 4:aload_0 5:LDC
        #2//String B 7:putfield #3//Field str2:ljava/lang/string;                  Load C 10:aload_0 11:ldc #4//String C 13:putfield #5

        Field str3:ljava/lang/string; STR4 actually,//Call new StringBuilder ()//Then call append (STR2). Append (STR3)//Then call ToString ()//Then 
        Assign value to STR4 16:aload_0 17:new #6//class Java/lang/stringbuilder 20:dup 21:invokespecial #7//Method Java/lang/stringbuilder. "
 <init> ":() V       Load b do append (str2) 24:LDC #2//String b 26:invokevirtual #9
        Method Java/lang/stringbuilder.append: (ljava/lang/string;) Ljava/lang/stringbuilder;
        Load C do append (STR3) 29:aload_0 30:getfield #5//Field str3:ljava/lang/string; 33:invokevirtual #9//Method Java/lang/stringbuilder.append: (ljava/lang/string;) ljava/lang/stringb
        Uilder; ToString to STR4 36:invokevirtual #10//Method java/lang/stringbuilder.tostring: () Ljava/lang/stri
        Ng

    39:putfield #11//Field str4:ljava/lang/string; ...//other STR5, STR6, STR7, str8 sourcefile: "Stringpooltest.java"

can refer to: http://blog.csdn.net/sugar_rainbow/article/details/68150249

Note: This ' basic ', ' JVM ' level is concerned with JDK versions, and some of the conclusions are not necessarily "correct"; Unicode and UTF-16 Java uses the Unicode character set to represent each character in memory (not just letters and Chinese characters); The Unicode Standard identifies a unique code point (codepoint) for each character, and the Unicode standard converts the code point to a 16-\uxxxx form; The Unicode standard manages the code point subregion, called a plane (plane); Unicode refers to the first 65536 characters as basic plane (BMP); Unicode is just a standard, no matter how it is actually stored; UTF-16 is only one implementation of Unicode;

Several questions about Unicode: How the code points are represented: for example, Codepoint is 1, then 1 bytes is enough, but if it is 100,000, then 1 bytes are certainly not enough; How to store the data. The simplest or occupies the smallest space.

can refer to: http://blog.csdn.net/gjb724332682/article/details/43229075

Question: why 1byte = 8bit. Why 1 byte is not 4bit or 16bit. 2.3Java UTF-16 Java uses utf-16 to store and represent characters – each character in the JVM is 2 bytes, and 2 bytes can represent the Codepoint range: 256 * 256 = 65536, which can represent BMP content o~ 0xFFFF Range; The CharSet class is Char's wrapper class also provides a number of Unicode manipulation methods, preserving the maximum and minimum number of characters, and what to do if a character such as a Chinese character codepoint is large-2 chars, or 4 bytes; In class, in order to reduce the size of the file, the character constants is encoded in UTF8, but the JVM is UTF16 in memory, that is, when the load is encoded, the number of bytes. Char in the end is a few bytes, it is clear that Char is 2 bytes, then why Chinese characters are 2 characters, 4 bytes, how to define. The substance of char stores an int value, and if Codepoint is represented in 2 bytes, then a char is enough to read a char and then look for the corresponding character to print when the print is displayed; if Codepoint needs 4 bytes, 2 characters, Then read two consecutive chars to print.

char c = ' I '; Syntax does not give an error, but does not compile the
 String s = "I";//char[] the length is 2;
what does char and string in 2,4 Java store in the end?String: A char[] array is stored; char: The int value of the Unicode codepoint of the character; The essence of encoding conversion in Java is to read the value of the target encoded codepoint to the codepoint value of Unicode and then store it in memory or somewhere else in UTF-16 format; 3 Source Code 3.1 Properties
String is a constant, after the string is created, the value of the string cannot be modified,
//final cannot be modified public
final class string implements Java.io.Serializable, Comparable<string>, charsequence {

    /** stores the original character/
    private final char value[];

    /** because of the immutability of the string, you can cache a hash value
    /private int hash;//Default to 0

    private static final long Serialversionuid =-68 49794470754667710L;

    Serialization using
    private static final objectstreamfield[] Serialpersistentfields =
        new objectstreamfield[0];

    Internal methods are used to compare string sizes public
    static final comparator<string> case_insensitive_order
                                         = new Caseinsensitivecomparator ();
3.2 Common Methods
    Judge length is char[] length public int length () {return value.length;
    //Is not an empty string, space is not an empty string; public Boolean IsEmpty () {return value.length = 0; //Index characters, be aware that chars not removed from BMP may not be converted to a full character; public char charAt (int index) {if (Index < 0) | | (index >= value.length))
        {throw new stringindexoutofboundsexception (index);
    return Value[index]; //Here Copy a new char to prevent string immutability from being corrupted void GetChars (char dst[], int dstbegin) {system.arraycopy (value, 0
    , DST, Dstbegin, value.length);  ///Copy part, or arraycopy///Do array copy or use arraycopy public void getChars (int srcbegin, int srcend, char dst[],
        int dstbegin) {if (Srcbegin < 0) {throw new stringindexoutofboundsexception (Srcbegin);
        } if (Srcend > Value.length) {throw new stringindexoutofboundsexception (srcend); } if (Srcbegin > Srcend) {throw newStringindexoutofboundsexception (Srcend-srcbegin);
    } system.arraycopy (value, Srcbegin, DST, Dstbegin, Srcend-srcbegin); }
3.3 Comparison of equality, hash

The equivalent of the idea of comparison is ultimately: cycle by comparison char[], if inconsistent false

    The Equals method compares the char characters to the same as public boolean equals (Object anobject) {if (this = = AnObject) {Retu
        RN true;
            } if (AnObject instanceof string) {string anotherstring = (string) anobject;
            int n = value.length;
                if (n = = anotherString.value.length) {char v1[] = value;
                Char v2[] = Anotherstring.value;
                int i = 0;
                    while (n--! = 0) {if (V1[i]!= v2[i]) return false;
                i++;
            return true;
    return false; //You can see that the character is computed one at a time the final hash//if calculated once again then no longer count//for "" String, Hashcode is 0 public int hashcode () {int h =
        Hash
              if (h = = 0 && value.length > 0) {char val[] = value; Participates in the calculation for Unicode codes for (int i = 0; i < value.length i++) {h = * H + val[i];
        hash = h;
    return h;
 }
more than 3.4 kinds of valueof methods

valueof (Object obj) will not have null problems, can replace our obj.tostring ();

  public static String valueof (Object obj) {return (obj = null)?
    "Null": obj.tostring ();
    public static String valueof (char data[]) {return new string (data);
    public static String valueof (char data[], int offset, int count) {return new String (data, offset, count); public static String copyvalueof (char data[], int offset, int count) {return new String (data, offset,
    count);
    public static String copyvalueof (char data[]) {return new string (data); public static String valueof (Boolean b) {return B?
    ' True ': ' false ';
        public static String valueof (char c) {char data[] = {C};
    return new String (data, true);
    public static String valueof (int i) {return integer.tostring (i);
    public static String valueof (long l) {return long.tostring (L);
    public static String valueof (float f) {return float.tostring (f);public static String valueof (double D) {return double.tostring (d);

 }

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.