String in 6.Java

Source: Internet
Author: User

    • Features of the 1.String

    • Feature One: immutability
    • String S=new string ("Yangyun")
    • S=s.touppercase ();
    • The space occupied by the s,s here is not the same (the address is not the same), if the toUpperCase function does change the contents of the original S.

Why is string a immutable object? -----Cankai

    • 1. Efficiency issues
    • If you know that an object is immutable, then you need to copy the object's contents without copying it itself and just copying its address, and copying the address (usually a pointer size) requires very little memory efficiency.
    • 2. Security
    • Immutable objects are safe for multithreading because, in the case of multithreading, the value of a mutable object is likely to be changed by other processes, which can result in unpredictable results, and the use of immutable objects avoids this.
    • 3. String constant pool needs-----Follow-up introduction
    • String s1= "AB" + "CD";

      String s2= "abc" + "D";

    • S1,S2 point to the same place, the hashcode value is the same (can cache the value of hashcode), if the string can be changed, then the S1 change will affect the S2, will bring security problems.

    • Feature two: overloading "+"
    • The meaning of operator overloading: Specific operators have special meanings in special classes;
    • "+", "+ =" is the only two overloaded operators in Java; Java does not allow programmers to overload any operator
    • When a connection is found that is not a string, an attempt is attempted to invoke the object's ToString method
    • The Hashcode function of the object class
      public String toString () { return getclass (). GetName () + "@" + integer.tohexstring ( Hashcode ());}
    • Feature three: Connection of strings
    • Method One: Use + = to concatenate strings
    • Consequences of using the +,+= link string: (Generates a large heap of intermediate objects that need to be garbage collected, thus inefficient)
    • JVM optimizations-Automatically connect using StringBuilder-no intermediate objects that are not referenced
    • But every time a string object is connected, the JVM does not create a string object that has no way to reference, but he creates a StringBuilder object--a waste
    • Method Two: Connect strings using StringBuilder's append function---------recommended
    • Only need to create a StringBuilder to connect
    • Feature four: "String Constants"
    • Description: A string is immutable, but a string reference (point) can be changed, and a string constant is a string literal that is bound to a constant and cannot be changed by a pointer.
    • Example:
    • Final String A=new string ("a");//A cannot change the point
    • Feature five: Important String functions (unfamiliar)
    • String.intern ()-Joins the string constant pool and returns the string
    • String.equalsignorecase ()-Ignore case comparison
    • String.IndexOf (string1)------exists containing string1, exists returns index, does not exist return-1
    • String.LastIndexOf (String)---looking forward from behind
    • String.contains (String)------exists, returns a Boolean type
    • String.Trim ()-----------------blank symbol before and after deletion
    • 2.StringBuilder Class Learning
    • Description: Used to concatenate strings and handle strings
    • constructor function:
    • StringBuilder sbuilder=new StringBuilder ();//Buffer 16
    • StringBuilder sbuilder=new StringBuilder (20);//buffer is 20
    • StringBuilder sbuilder=new StringBuilder ("Hello");//Initial character Hello
    • function function:
    • Append ();//Add a string to the end
    • Reverse ();//Reverse device string
    • Insert (index,value);//Inserts a string starting at index
    • Delete (startindex,endindex);//Remove the character of index in Startindex~endindex-1
    • Replace (startindex,endindex,string);//use String instead of the character between Startindex~endindex, length self-adjustment, String is not enough space to add, long to intercept
    • ToString (); Convert to String
    • Unconscious recursion-the invocation of the object ToString function
    • List<person> list=new ArrayList (arrays.aslist ("Yangyun"), New Person ("Tim"));
    • SYSTEM.OUT.PRINTLN (list);
    • If: person does not have a ToString () object--automatically calls the ToString function of the super.tostring ()-object of each object
    • Call the ToString () function of the object, and if the object does not have the ToString function, the ToString () function of the calling object's parent class

    • 3. String pool and heap (important)
    • Phase one: Compile phase
    • String constants are placed in a pool of literal strings, while the run-time string constant pool becomes part of the constant pool.
    • The string constants here refer to String string= "Yangyun", String string= "abc" + "Def";
    • Stage Two: Programmers use the Intern method to change the size of the string pool.
    • When the Intern () method is called, if the string pool already contains a string equal to this string object (as determined by the Equals (Object) method), the string in the string pool is returned. Otherwise, this string object is added to the string pool and a reference to this string object is returned.
    • Comparison of strings:
    • 1.string.equals (String)--Comparison of strings
    • 2.== (actually compares the string object address)

    • example:
    • String str1 = "Java";//str1 point to String pool
    • string str2 = "blog";//str2 point to character String Pool
    • string s = str1 + str2;//How many objects will this statement create
    • s is an object that points to" Javablog "in the heap, and the + operator creates two string objects in the heap, each of which is" Java "," blog ", that is, the two values are copied from the string pool, and then two objects are created in the heap. The object s is then created, and the heap address of "Javablog" is assigned to S. How many objects does this statement create in total? (three bar)
    • If you change to the following two ways:
    • Span style= "Text-decoration:underline" > 1.s is a string in a constant pool
    • string s = " Java "+" blog,   //"Javablog" directly into the string pool
    • System.out.println (s = = "Javablog");   //result is true
    • 2.s is a string in the heap
    • String s = str1 + "blog"; Instead of being placed in a string pool, it is allocated in the heap
    • System.out.println (s = = "Javablog"); The result is false
    • Summarize:
    • Three ways to create a string object
    • String string= "abc";//pool does not create a direct reference, otherwise creates and joins the pool
    • Final String string= "abc";//pool
    • String String=new string ("abc");//heap allocating memory Creating object
    • String string= "abc" +S1;//S1 created for new-then string is also using the heap

Reference documents:

Http://www.codeceo.com/article/why-java-string-class-static.html

String in 6.Java

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.