A simple solution of string and StringBuffer

Source: Internet
Author: User

    • String

A key concept of string is that a string object can never be changed after it is created. In Java, a string is an object, such as a string S=new string (), and an assignment s= "AAA";

You can create an instance of the learned string object. Once you assign a value to string, the value will never change. Although the value is immutable. But the reference variable is not.

If you assign a value to a string object, you want to change its value. Like this s=s.concat ("ABCs"). Java does not modify add ABCs on a string object, but instead creates a new object and then

The new object is given to string S. Strictly speaking, Java actually creates three objects: the initial object AAA and the Connection object constant parameter ABCs and the resulting new object Aaaabcs. When we call the string "AAA" before S.concat

remains in memory, but it is considered a lost state. Any code has no way to access it.

(Hashcode is the value of a JDK based on the address of an object or a string or a number of int types. Refer to [1] public int hashcode() to return the hash code value of the object. )

 Public class teststring {    publicstaticvoid  main (String agrs[])    {        System.out.println ("String");        string s=new string ("Init");        System.out.println (S.hashcode ());         //         Connect the new string        s=s.concat ("add");        System.out.println (S.hashcode ());        System.out.println ();    }}

Output:

String
3237136
1948334481

As you can see from the example above, the reference object of string s also changes with the addition of a new string at the end of the string. It can be explained that sring is modified after the assignment to produce a new string object, and then the string s is referenced instead of directly modified.

    • StringBuffer and StringBuilder

string buffers Similar to string are extensively modified for strings, Java.lang.StringBuffer and Java.lang.StringBuilder are the best choices. Because it does not cause a large number of string objects to be discarded like string. The following code distinguishes between string and StringBuffer. Sun's recommendation is to prioritize StringBuilder when possible, because in most implementations it is faster than StringBuffer.

1  Public classteststring {2      Public Static voidMain (String agrs[])3     {4System.out.println ("String");5String s=NewString ("Init");6 System.out.println (S.hashcode ());7         //connect a new string8S=s.concat ("Add");9 System.out.println (S.hashcode ());Ten System.out.println (); One          ASystem.out.println ("StringBuffer"); -StringBuffer stf=NewStringBuffer ("Init"); - System.out.println (Stf.hashcode ()); the //connect a new string -Stf.append ("Add"); - System.out.println (Stf.hashcode ()); -     } +}

Output:

String
3237136
1948334481

StringBuffer
1826043875
1826043875

As you can see from the above, StringBuffer and Stringbulider do not create new objects like string assignments, but instead modify them directly on the original object.

The main differences between Stringbulider and StringBuffer:

StringBuilder and StringBuffer have exactly the same API except that Stringbulider is not thread-safe, which means it's not thread-synchronized.

String and StringBuffer simple solution

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.