Depth understanding of string

Introduction string

String program is in development, one of the most frequently used type, has the same basic type of position, even in the JVM (Java Virtual Machine) compile time of a string of special treatment, such as splicing operation may be directly JVM synthesis of a final string, so as to achieve efficient operation.

1 String properties

  • String is immutable standard class (immutable), any changes to it, in fact, create a new object, then references to the object;
  • After the String object constant pool assignment will be in the cache, if the next time you create will determine whether there has been constant pool of cached objects, if any, directly back to the reference to the creator.

2 Create a string

String created in two ways:

  • String str = "laowang";
  • String str = new String("laowang");

3 Precautions

View the code below:

        String s1 = "laowang";
        String s2 = s1;
        String s3 = new String(s1);
        System.out.println(s1 == s2);
        System.out.println(s1 == s3);

Output:   to true , false 

Why is that? The reason is that when s3 will be re-created using the new String in a heap memory area, and s2 will be used as a reference to s1, so the results are completely different.

Use string

1 plus string fight

Plus a string of fight in several ways:

  • String str = "lao" + "wang";
  • String str = "lao"; str += "wang";
  • String str = "lao"; String str2 = str + "wang";

2 JVM optimized string

According to the previous knowledge we know that for any action String actually creates a new object and then returns the address referenced object, but also on the JVM String special treatment in order to improve the efficiency of the program, such as the following Code:

String str = "hi," + "lao" + "wang"; 

After the JVM optimized code like this:

String str = "hi, laowang";

Verification code is as follows:

        String str1 = "hi," + "lao" + "wang";
        String str2 = "hi,laowang";
        System.out.println(str1 == str2);

The results:   to true 

This shows that in some cases the next time JVM special treatment of type String.

3 string interception

Use string interception   substring ()  method, using the following:

        STR = String "abcdef" ;
         // Results: cdef (2, taken from the start of subscripts to the end, including the start index) 
        System.out.println (str.substring (two ));
         // Results: cd (from subscript 2 is taken to start at index, including the start index is not included 4 superscript end) 
        System.out.println (str.substring (2,4));

4 String Format

String formatting can make the code more simple and more intuitive, such as "My name is Wang, 26 years old this year, like reading" In this message, the name, age, interests are to be dynamically changed, if you use the "+" it is easy to splicing errors, this time to string formatting method String.format () comes in handy, as follows:

        String str = String.format ( "My name is% s,% d years old this year, like% s", "Pharaoh", 26, "Reading");

The conversion operation Description List:

The conversion operation Explanation
%s String type
%d Integer type (decimal)
%c Character Types
%b Boolean
%x Integer type (hexadecimal)
%The Integer type (octal)
%f Floating-point type
%a Floating-point type (hex)
%e Index type
%% Percentage Type
%n Newline

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

String Comparison 5

According to the previous knowledge we know that object using the String and new String declaration is different, that there is no easy way, can ignore their creation mode (there is no new) but only compare their values are the same? The answer is yes using  equals ()  methods may be implemented, as follows:

        String s1 = "hi," + "lao" + "wang";
        String s2 = "hi,";
        s2 += "lao";
        s2 += "wang";
        String s3 = "hi,laowang";
        System.out.println(s1.equals(s2));  // true
        System.out.println(s1.equals(s3));  // true
        System.out.println(s2.equals(s3));  // true

 More use equals comparison results are true.

If you want to ignore the case of the string ratio may be used equalsIgoreCase (), sample code:

        String s1 = "Hi,laowang";
        String s2 = "hi,laowang";
        System.out.println(s1.equals(s2));    // false
        System.out.println(s1.equalsIgnoreCase(s2));    // true

6 String , StringBuffer, StringBuilder

String types are related to these three: String, StringBuffer, StringBuilder, which StringBuffer, StringBuilder is a variable of type string, StringBuffer using synchronized to protect the security thread in the string concatenation, so multi-threaded string concatenation in We recommended StringBuffer.

StringBuffer use:

 

Guess you like

Origin www.cnblogs.com/Night-Watch/p/11685006.html