What is the difference between string and StringBuilder, StringBuffer?

Source: Internet
Author: User

For:

The Java platform provides two types of strings: string and Stringbuffer/stringbuilder, which can store and manipulate strings. Where string is a read-only string, meaning string content referenced by string cannot be changed. The string object represented by the Stringbuffer/stringbuilder class can be modified directly. StringBuilder is introduced in Java 5, and it is exactly the same as the StringBuffer method, except that it is used in a single-threaded environment because all aspects of it are not synchronized decorated. Therefore, it is also more efficient than stringbuffer.

Interview 1-Under what circumstances is a string connection with the + operator better than the Append method that calls the Stringbuffer/stringbuilder object?

Interview 2-Please say the output of the program below.

Class Stringequaltest {

public static void Main (string[] args) {
String S1 = "Programming";
String s2 = new String ("Programming");
String s3 = "program" + "Ming";
System.out.println (S1 = = s2);
System.out.println (S1 = = S3);
System.out.println (S1 = = S1.intern ());
}
}
Supplement: The Intern method of the string object will get a reference to the version of the strings object in the constant pool (if there is a string in the constant pool with the equals result of the string object is true), if there is no corresponding string in the constant pool, the string is added to the constant pool. It then returns a reference to the string in the constant pool.

What is the difference between string and StringBuilder, StringBuffer?

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.