Description of Blackhorse programmer-string, stringbuffer, and stringbuilder

Source: Internet
Author: User
Tags string methods

 

Http://edu.csdn.net/heima Android Training
Http://edu.csdn.net/heima; Java training is coming to communicate with you!

1. String object

1. Features of the string class:

A. The most common string data objects used to describe in Java.

B. String data is an object.

C. Once the string data is initialized, it cannot be changed.

D. string objects are stored in the constant pool and the String constant pool.

2. Create a String object using the new method)

String S1 = "ABCD"; // This Is A String object created in the constant pool.

String S1 = new string ("ABCD"); // create an object in the constant pool and back up one copy in the heap memory.

3. Comparison

=: Comparison of basic data types.

Equals: Object comparison.

4. Methods in the string object.

There are many constructor methods.

Int length (): gets the string length.

Int indexof (INT ch): obtains the location of a specified character.

Int indexof (string Str): returns the position of the specified string.

Char charat (INT index: returns the char value at the specified index.

String substring (INT beginindex, int endindex): returns a new string, which is a substring of this string.

String substring (INT beginindex): returns a new string, which is a substring of this string.

Char [] tochararray (): converts this string into a new character array.

String touppercase (): converts a string to uppercase.

String tolowercase (): converts all strings to lowercase letters.

Static string valueof (all ):
Returns the string representation of the parameter.

String tostring (): returns the object itself.

Int lastindexof (INT ch ):
Returns the index of the last occurrence of the specified character in this string.

Isempty (): returns true if the string length is 0.

Byte [] getbytes (): converts a string into a character array.

Ii. stringbuffer string buffer.

1. features:

A. The buffer is used to cache data, which means it is a container.

B. The Container length is variable.

C. Provides operations on the content in the container. (Four types: add, delete, modify, and query)

D. multiple types of elements, basic types, and application types can be added to the buffer.

E. To use the data in the container, use the tostirng method to return the string.

2. Common Methods: Check the constructor.

Append () append, return or container, can be added consecutively.

Tostring () returns a string.

Insert () is added at any position.

Delete () deletes a specified object.

Delete (0, SB. Length) clears the buffer.

SB. Replace () modification.

Setcharat ()

Reverse () reversely prints the string.

 

Note: Generally, the bottom layer of the container encapsulates an array of variable length.

3. stringbuilder container.

1. stringbuilder Overview:

A. jdk1.5 only appears.

B. The functions are the same as those of the stringbuilder container.

2. Differences between sringbuffer and stringbuidler.

A. The stringbuidler does not need to judge the synchronization lock. The thread is not safe, but the efficiency is high.

Stringbuffer is thread-safe.

B. For containers, multi-threaded operations are prone to security problems.

Because a thread is responsible for adding and deleting, security issues may occur due to random threads.

Each time a method is called to operate the container, a lock is determined, so the efficiency is relatively low.

3. What is the difference between stringbuffer and array containers?

A. the array container has a fixed length and the container has a variable length.

B. Once the array container is initialized, the element type is clarified.

Stringbuffer can store any type, including basic and reference.

C. The elements stored in the array can be operated through the badge.

All elements stored in stringbuffer are converted into strings and can only be operated using string methods.

4. When to use the string buffer?

The number and type of data can be fixed or not fixed.

As long as the final data needs to be converted into strings for operation, the string buffer can be used.

 

Http://edu.csdn.net/heima Android Training
Http://edu.csdn.net/heima; Java training is coming to communicate with you! See http://edu.csdn.net/heima for details

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.