Interview Prerequisites: String,stringbuffer,stringbuilder Difference

Source: Internet
Author: User
Tags ticket stringbuffer

First of all, these three classes are used to handle strings,


StringBuilder was introduced after JavaSE5 to replace StringBuffer,


StringBuffer is safe relative to the StringBuilder thread, so the overhead is a bit larger

StringBuilder threads are unsafe and string operations are faster.

What is the concept of thread-safe insecurity?

Sell the ticket example everybody has heard, suppose altogether 50 tickets, if lets the a,b,c three people simultaneously sell the ticket, if is the thread unsafe will be what kind of.

Suppose a sold 30, but at this point the number of votes has not yet been modified B to start selling tickets, which will lead to a problem, B will get the margin of the ticket is 50. This leads to the problem of different steps. That is, if more than one person operates an object at the same time, if the lock is not synchronized, it can cause thread-insecure objects.


As for the difference with string, this is reflected when the string string uses the plus sign:

String str = "a" + "B" + "C" + "D";

When you create a string using the above method, each connection to a string using the "+" sign creates a new string object, which can be a huge waste of memory


The problem with this create object requires detailed analysis: (Refer to http://www.iteye.com/topic/634530)

My main focus is on stacks, heaps, and constant pools.

1, for example, to create an object

String str = "ch";

A CH object is created in the constant pool, and a reference to the object is created on the stack. That means the code creates only one object.

2, but if it is

String str1 = "ss" + "HH";
Creates a reference str1 in the stack, creating three objects in the constant pool: SS object, HH object, and Sshh object, which is because,

The program first goes to the constant pool to find out if there is an SS string object that does not exist, then the SS object is created.

HH object to the same reason

Because character creation belongs to immutable objects, if you want to get to a Sshh object you can only go to create a Sshh object in Chang again


3 If you are using new to create a string

String str2 = new String ("tt");

First, the program creates a new string object in the heap, because "TT" is required in the new process, so it goes to the constant pool to find the TT object, and if it does not exist, it is created.

So this code creates an object in a constant pool: TT, creates an object new String in the heap, creates a reference to the object in the stack str2


4, if it is with StringBuilder:

StringBuilder str3 = "pp";

StringBuilder STR4 = Str3.append ("ll");

The program first creates the PP object in a constant pool, creates a ll in the constant pool when append, creates only two objects here, and creates 3 references in the stack. This is because the StringBuilder allocated space is variable, so at the time of append, it is directly to add the LL object to the storage area where the PP resides.


All of the above analysis assumes the result of creating a string object that does not have a corresponding string in the regular pool, and no longer needs to create the object if there is a corresponding string in the constant pool

Summarize:

The position of the object exists in the heap (new), in the Constant pool ("", directly defined)

The reference to the object exists in the stack












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.