Java String, javaString

Source: Internet
Author: User

Java String, javaString

As the most common java class, String has been plagued by many programmers for a long time. It makes us love and hate it. How should we use it correctly, ginger brings you together to unveil its mysterious veil.

 

I. The final identifier is defined in the String class, which determines its immutable attributes for life.

  Final: When a class is modified, the class cannot be inherited.

Original design intention: the class does not need subclass, the implementation details of the class cannot be changed, and it is sure that the class will not be extended. The compiler will optimize final to improve the execution efficiency.

 

2. Any operation of the String class will generate a new String object.

  As we have mentioned above, String is final. Since it cannot be changed, new objects will naturally be generated. Let's look at the following code:

String a = "abc123 ";

A. subString (3 );

System. out. println ();

What will the above Code output? The answer is abc123, because a is never changed, the result of a. subString (3) is not assigned to variable.

 

Iii. String constant pool

  I believe that students who have experience in the interview must have encountered such a problem: String a = new String ("a"); how many objects are generated by this code?

My answer is one or two.

1: If the variable "a" is defined in this line of code, only the new String ("a") object is generated.

2: on the contrary, there will be two objects, "a" and new String (".

Ps: What about variable? Isn't it an object?

A is just a variable. It stores the reference of the object new String ("a"). It cannot be called an object and is just an alias.

We know that some of the basic types of variables and Object Reference variables defined in the function are allocated in the function stack memory. Newly created objects and arrays are stored in the heap memory.

In addition, a constant pool exists, which is determined during the compilation period. It is saved in the class file and contains the String constant. For example,

String s = "abcd ";

String s2 = "abcd ";

Here, only one object "abcd" is generated and stored in the constant pool, and s1 = s2 is true.

String s3 = "AB" + "cd ";

S3 = s? The answer is true, because the compiler will optimize the "+" Operation of the direct String constant to s3 = "abcd" and store it in the constant pool.

 

Iv. String concatenation

1) Add a few strings and use "+" directly, for example, String a = a1 + a2 + a3;

2) concatenate a large number of strings and use StringBuilder, for example:

StringBuilder sb = new StringBuilder ();

For (int I = 0; I <100; I ++ ){

Sb. append (I );

}

String s = sb. toString ();

StringBuilder internally maintains a char array with an initial value of 16, which doubles when the volume is insufficient. Therefore, if you can predict the size of the final concatenated string, you can write it as follows during initialization:

StringBuilder sb = new StringBuilder (100); avoid insufficient space and repeatedly open up memory, affecting efficiency.

Ps: String is a constant. Every time "+" is used to generate new objects, the memory is opened up repeatedly, and the efficiency naturally decreases.

3) Considering thread security, StringBuffer is required. Its internal implementation is similar to that of StringBuilder, but data synchronization is less efficient than StringBuilder.

 

5. String comparison

1. Empty comparison:

If (null = s | "". equals (s )){...}

Place null = s on the left side and "" on the left side of equals, because s may be null, which directly causes the program null pointer exception: java. lang. NullPointerException.

Ps: another way of writing, if (null = s | s. length () = 0) {...}, said this efficiency is higher than equals.

 

2. Compare two objects

In most cases, we are concerned with the string value, rather than the memory address, and use equals for comparison.

When you use "=" to compare strings at runtime, the results will often not satisfy you. Sometimes it will make us step in the trap for a long time and keep asking ourselves, why is this logic always incorrect? Because it cannot be executed forever.

 

I wrote it here today. Is there anything that can touch you?

 

Follow ginger to talk about technology, no.: helojava, or scan the following QR code.


One post per day, chicken soup for technical purposes.

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.