Notes on correct use of the string class

Source: Internet
Author: User
The Java. Lang. string class is the most familiar to everyone. We rarely need to use string when writing Java programs. This article describes how to use string correctly. The content mainly involves initialization, concatenation, comparison, and other operations.

First, we must be clear that the string class is of the final type, so you cannot inherit this class or modify this class. It is very simple to use string, usually string S = "hello", but a constructor string (string S) is provided in Java API ), therefore, you can also use string S = new string ("hello") in this way. It is not recommended to initialize a string later, because the new operator means that a new object will be generated on the heap. If such an operation occurs in a loop, the cost is heavy. For example
For (INT I = 0; I <1000; I ++)
{
String S = new string ("hello ");
}
This will create 1000 string objects. Because the string class is final, such an operation actually generates a new String object each time. If you use string S = "hello"; then you can reuse it. Why can we reuse it? The following is an explanation.

When we use "+" to implement concatenation operations, for example, string S = "hello" + "world"; it is actually implemented through the append () method of the stringbuffer class, returns string to S. If you are interested, you can write a simple example and use javap to see how the virtual machine works. When using concatenation, we should also note that string is a final class. If you need to concatenate multiple times, for example:
String SQL = "XXX ";
SQL = "XXXX ";
SQL = "Ssssss ";
In order to improve efficiency and save space, we should use stringbuffer to replace "+ ";

Generally, there are two types of string comparison. One is to use =, and the other is to use the equals () method. Note that = is to compare the object address, in string, the equals () method overwrites the object class method and compares the content of the string object. So string S1 = new string ("hello"); string S2 = new string ("hello"), when we compare S1 and S2, the former should return false, because New is used to generate two different objects. The latter should return true because their content is the same and they are all "hello ". If we have another string S3 = "hello"; what should we compare with S1? The answer is: S1 = S3 is false, and equals is true. In fact, the string class maintains a string pool, and the pool Initialization is empty. When we use string x = "hello", hello will be placed in this pool, when we use string y = "hello" again, we first check whether there is an object with the same content as hello in the pool. If so, we will return this reference to y, if it does not exist, it will create one and put it into the pool. This achieves reuse. In string, there is a method intern (). It can put the string object into the pool and return the object in the pool. If we call intern, S1 = s1.intern () on S1 (string S1 = new string ("hello"), then we judge S1 and S3 by "=, you will find that the result returns true!
See the following example.

Public class stringtest
{

Public static void main (string [] ARGs)
{
String S1 = "hello ";
String S2 = new string ("hello ");
String S3 = new string ("hello ");

Teststring (S1, S2, S3 );
S2 = s2.intern ();
System. Out. println ("after s2.intern ");
Teststring (S1, S2, S3 );

}

Private Static void teststring (string S1, string S2, string S3)
{
System. Out. println ("S1 = S2 is" + (S1 = S2 ));
System. Out. println ("S2 = S3 is" + (s2 = S3 ));
System. Out. println ("s1.equals (S2) is" + s1.equals (S2 ));
System. Out. println ("s2.equals (S3) is" + s2.equals (S3 ));
}
}
The output result is
S1 = S2 is false
S2 = S3 is false
S1.equals (S2) is true
S2.equals (S3) is true
After s2.intern
S1 = S2 is true
S2 = S3 is false
S1.equals (S2) is true
S2.equals (S3) is true

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.