Drop my java questions

Source: Internet
Author: User
1. What is the difference between arraylist, struct list, and vector?

Arraylist is based on the array structure, while list is based on the two-way linked list structure. It can be seen from the operational features of arrays and linked lists that arraylist is faster in search and slower in addition and deletion.

The slow list, on the contrary, is faster when adding or deleting data, and slower when searching.

Like arraylist, vector is stored in an array structure. However, vector uses the synchronized method, which is thread-safe, so it is less efficient than arraylist. If thread security is not required, it is not required.

2. What are the differences between string, stringbuffer, and stringbuilder?

String is a final object, that is, the value cannot be changed. For example, string a = "ABC"; A = a + "D"; the actual process is as follows: first create a stringbuffer sb = new stringbuffer (STR), and then call sb. append (), and then call STR = sb. tostring () is assigned to a new string. Therefore, the string addition and deletion operations are far slower than stringbuffer. When string addition and deletion operations are frequent, there will be a large number of objects in the memory space, increasing the garbage collection overhead.

When using stringbuffer, create an object first, and then change the value of the object stringbuffer sb = new stringbuffer (); sb. append ("ABC ");

The addition and deletion operations of stringbuilder are faster than those of stringbuffer, but it is not thread-safe. Stringbuffer is thread-safe.

So it is so troublesome to add, delete, and delete strings. Isn't it necessary to use stringbuffer all the time? String is applicable when it is called multiple times and rarely modified. In this case, if stringbuffer is used, a new object will be created each time a call is made, and many objects will be allocated in the memory. If the string method is used, no new object is required.

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.