Java Basic Knowledge Summary (String, StringBuffer, StringBuilder)

Source: Internet
Author: User

1.String string:

The string class is described in Java. Encapsulates the object with a string. The advantage is that you can easily manipulate the common data of strings. After the object is encapsulated, you can define n multiple properties and behaviors.

How do I define a string object? string s = "abc"; The data that is caused by double quotation marks is a string object.

  features : Once the string is initialized, it cannot be changed and stored in a constant pool in the method area.

  

String S1 = "abc"; //S1 points to only one object in memory ABC .

string s2 = new String ("abc"); //S2 has two objects pointing to the content ABC ,new.

System.out.println (S1==S2);//false

System.out.println (s1.equals (S2));//true, in the string equals compares the contents of the strings.

Method of String:

1: Constructor method: converts a byte array or an array of characters into a string.

string S1 = new string ();//A string that creates an empty content.

String s2 = Null;//s2 does not have any object pointing, it is a null constant value.

string s3 = "";//s3 points to a specific string object, except that there is no content in the string.

New String (char[]);//Converts a character array into a string.

New String (char[],offset,count);//Converts a portion of a character array into a string.

2.StringBuffer String Buffer:

constructs a string buffer with no characters, with an initial capacity of three characters.

Characteristics:

1: You can modify the contents of a string.

2: is a container.

3: Is variable-length.

4: Any type of data can be stored in the buffer.

5: Eventually it needs to become a string.

3.StringBuilder String Buffer:

JDK1.5 appeared stringbuiler; constructs a string generator with no characters, with an initial capacity of three characters.

This class is designed to be used as StringBuffer a simple replacement for a string buffer that is used by a single thread (this is common). Method is the same as StringBuffer;

The difference between StringBuffer and StringBuilder:

StringBuffer thread safety.

StringBuilder thread is not secure.

single-threaded operation, using StringBuilder high efficiency.

Multi-threaded operation, using StringBuffer safe.

Java Basic Knowledge Summary (String, StringBuffer, StringBuilder)

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.