Java string"

Source: Internet
Author: User
Java string"     Strings are one of the many types we use. Even in this case, there are many obfuscation points for us, and there are still many areas that we are not very clear about. We are familiar with common usage and will not repeat it here. I just want to find out some places that we usually don't care about. Maybe we have already used them, but it's not very sure yet. To sum up them, we can say it's just a breeze.   First, Concerning the string object's question about the string object, every time I argue over the BBS. The tendency is to: String S = "ABC"; in this statement, S is a reference, which points to the object "ABC ". In this way, the following statement: String S1 = "ABC"; string S2 = "ABC"; generates only one object, that is, "ABC ". String S = new string ("ABC"); in this statement, s is an object. The following statement: String S1 = new string ("ABC"); string S2 = new string ("ABC"); generates two objects: S1 and S2. With the above explanation, it is not difficult to understand why we give priority to the previous expression in code, because the previous one is reference, no matter how many identical references are used in the Code, only one object is generated. In the latter way, even if the content of multiple objects is the same, they are different objects with their own memory overhead, which greatly increases our memory overhead. Similarly, the following code and their running results can be explained. String S1 = "ABC"; string S2 = "ABC ";

String S3 = new string ("ABC ");

Code: system. out. println (S1 = S2); running result: true code: system. out. println (S1 = "ABC"); running result: true code: system. out. println (s1.equals (S2); running result: truesystem. out. println (S1 = S3); running result: false code: system. out. println (s3.equals ("ABC"); running result: True   Second, Difference between string and stringbuffer since statement: String S = "ABC"; S is a reference. In the following statement: String S = "ABC"; S = S + "D";, there should be three objects: "ABC", "D", and "ABCD ". Reference s first points to the object "ABC", and then to the object "abacd ". In this way, we can see the difference between string and stringbuffer. For the following code: String S = ""; for (INT I = 0; I <10; I ++ ){

S = S + I;

} We will generate more than 10 objects. The following code:

Stringbuffer sb = new stringbuffer ();

For (INT I = 0; I <10; I ++) {sb. append (I);} generates only one object. Therefore, when the string object needs to be added, the use of stringbuffer is much better than the use of string, especially when the loop is added.   Third, The conversion of other basic types and strings in the actual encoding process often needs to be converted to other basic types, as shown in the following examples: string S = ""; for (INT I = 0; I <10; I ++ ){

S = S + I;

}, The S = S + I; statement needs to convert the integer data I into a string, and then add it with S. In fact, apart from S = S + 1;, there is also a conversion method that we don't often use, as follows: S = S + integer. tostring (1); After testing the time consumed by the above two conversion methods, we found that the time consumed by the next conversion method is much lower than that consumed by the previous conversion method. Therefore, you may wish to use the following conversion method in the coding process.   Fourth, The overload tostring () method of the object class tostring () is a public method of the object class, and the object class is the parent class of all Java classes. Therefore, any other Java class can rewrite this method for its own use. For example, the tostring () method is rewritten for all basic types of packaging classes, such as integer, float, and double. The function is to convert the type to the string type. Rewriting tostring () is generally used to provide some basic information about this type. For a complex category, see the following:

Public class Complex

{Private double real; private double image ;......} Its tostring () method can be written as follows:

Public String tostring ()

{String rI = ""; if (this. image <0) {rI = RI + "-" + double. tostring (math. ABS (this. image) + "I";} else {rI = RI + "double. tostring (this. image) + "I";} return double. tostring (this. real) + Ri;} The tostring () method above just expresses a mathematical expression of the plural number, which is exactly the result we hope to get after the calculation of the plural number. For example, for an employee class: employee. The most common and expected names are their names, as shown below:

Public class employee

{Private string firstname; private string secodename ;...... Public String tostring (){

Return this. firstname + this. secodename;

}......} Therefore, in some classes, rewriting the tostring () method is very useful and may be used frequently as the display data or in debugging statements.   Fifth, Escape Character Problems Escape Character problems are common problems in programming. For example, if you encounter ">" or "<" in HTML programming, the browser will regard them as reserved characters of HTML, such as the tag start character and end character, in this way, some errors will occur. Another example is that in an SQL statement in JDBC programming, an error occurs if quotation marks such as "" or "" appear, SQL statements treat them as the start or end characters of an SQL statement. Also, for example, "?" in the URL statement And other characters, will also cause confusion of the URL expression. When we encounter the above situations, we all find some special characters to replace them, and then replace them after they are used up. These special characters are escape characters. However, it is quite troublesome for us to find their escape characters by ourselves, but it is prone to errors. In view of this, the Jakarta commons package provides us with a solution for escape characters. Using this solution, we do not need to find escape characters by ourselves, you only need to call the API of the Jakarta commons package, as shown below:

String S = "There is always <(less) or> (more) than you want .";

String S1 = stringescapeutils. escapehtml (s );

System. Out. println (S1 );

String S2 = stringescapeutils. unescapehtml (S1 );

System. Out. println (s2.equals (s); system. Out. println (S2); output result:

There is always & lt; (less) or & gt; (more) than you want.

True

There is always <(less) or> (more) than you want.

Isn't it easy? We just need to introduce corresponding package of Jakarta commons in the above Code.   Sixth, The question of Chinese and English word splitting the question of Chinese and English mixed sentences is a solution that I once saw on BBS. I think it is very interesting. Although I haven't used it till now, I put it in the summary of this article, which is a collection of the usage of strings. We all know that Java uses unicode encoding. All characters occupy one word and two bytes. How can we extract Chinese and English words in a mix of Chinese and English words? The following is a simple solution: String S = "I am a Chinese AB ";

Byte [] bt = S. getbytes ();

Int I = 0;

While (I <BT. length) {string SS; If (BT [I] <0) {Ss = new string (BT, I, 2); I + = 2 ;} else {Ss = new string (BT, I, 1); I ++;} system. out. println (SS );}   The output is: I am a Chinese AB student.

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.