Java String Related

Source: Internet
Author: User
Tags comparable

I. Common methods of the string class

1.intindexOf (string s) string lookup2.intlastIndexOf (String str)3.CharCharAt (intindex) Gets the character at the specified index position4. String substring (intbeginindex) Get substring5. String substring (intBeginindex,intEndIndex)6. String trim () to remove spaces7.BooleanstartsWith (string prefix) to determine the start and end of a string8.BooleanendsWith (String suffix)9.Booleanequals (string otherstr) determines whether strings are equal10.Booleanequalsignorecase (String otherstr)11.intCompareTo () comparison of two strings in dictionary order12. Case conversion of String toLowerCase () letters13. String toUpperCase ()
14. String[] Split (string regex) string SplitString[] Split (String regex,intlimit)16.StaticString ValueOf (Boolean,Char(Char[]),int,Long,float,Double, Object) get various types of string representations

. String intern () generates one and only one String reference for each unique sequence of characters, intern: Put in the meaning, put in the constant pool

Two. Properties of the String class

In the JAVA language there are 8 basic types and a more special type String . These types provide a constant pool concept in order to make them faster and more memory-efficient during operation. Chang is similar to a cache provided at the Java system level. The 8 basic types of constant pools are system-coordinated, and String the constant pools of types are more specific. There are two main ways to use it:

    • Objects declared directly with double quotation marks are String stored directly in the constant pool.
    • If the object is not declared with double quotation marks String , you can use String the provided intern method. The Intern method queries the current string for existence from the string constant pool and puts the current string in a constant pool if it does not exist

There are 3 basic features of String:

1. invariance (read-only feature);

2. For the optimization of constant pool;

3. Final definition of the class.

invariance means that once a String object is generated, it can no longer be changed. This feature of String can be generalized into the invariant (immutable) pattern , where the state of an object does not change after the object is created. The main function of invariant mode is that when an object needs to be shared by multiple threads, and when access is frequent, it can omit the time of synchronization and lock waiting, thus greatly improving system performance .

optimization for a constant pool means that when two string objects have the same value, they reference only the same copy in the constant pool, which can save significant memory space when the same string repeats itself.

Every method in the string class that looks to modify a string value is actually creating a completely new string object that contains the modified string content. (external (e.g., re-assigned) and internal modifications are re-created with a new string object)

Why not change? SOURCE Analysis:

A string object can be thought of as an extension and further encapsulation of a char array, which consists mainly of 3 parts:The char array, the offset, and the length of the String . A char array represents the contents of a string, which is a superset of the string represented by a string object. The actual contents of the String also need to be positioned and intercepted in this char array by offset and length .

 Public Final classStringImplementsJava.io.Serializable, comparable<string>, charsequence{/**The value is used for character storage.*/    Private Final Charvalue[]; /**The offset is the first index of the storage, which is used.*/    Private Final intoffset; /**The count is the number of characters in the String.*/    Private Final intcount; /**Cache The hash code for the string*/    Private intHash//Default to 0

A slight change in the

Java7:

 public  final  class   String  implements  java.io.Serializable, Comparable<string> /**   The value is Used for character storage.  */ private  final  char   value[];  /**   Cache the hash code for the string     */ private  int  hash; //  Default to 0  

In JDK7, there is only one value variable, that is, all the characters in value are the object of the string.

Three. Compiler optimizations for string

1. "+" and "+ =" for string are the only two overloaded operators in Java

2. Reload "+" with StringBuilder

The Java.lang.StringBuilder class is automatically introduced by the compiler when a "+" connection is made between string objects. Although we did not use the StringBuilder class in the source code, the compiler used it in its own way because it was more efficient.

Java String Related

Related Article

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.