Abstract: Abstract: Original creation Place: http://www.cnblogs.com/Alandre/sediment brick slurry Carpenter hope reprint, keep abstract, thank you!
Wake up every day to tell yourself that your goal is "technology + English and life"! -Mud and brick pulp carpenter
A. Char says string
Two. The understanding of string and the source code analysis
A. Char says string
This is also the second time to chew back to the Java Foundation book, The Niche self-thinking ignorance. If the great God has a good education, you can comment on private messages. Here are my Views:
Why Char, I sell a xiaoguanzi here first. In Java, char is encoded in Unicode, accounting for 16 bits (2 bytes). from ANSI encoding (1 bytes) to Unicode encoding (2 bytes). The reason for using Unicode in Java is that Java applets (Web pages) run and Unicode contains the widest range of languages, such as: Chinese, English,spanish,german, French, and so on. Therefore, Char is one of the basic types of Java and is very convenient to use.
Note: In Java, the char type is described as a unit of code with UTF-16 encoding.Two. The understanding of string and the source code analysis
Here I answer a xiaoguanzi, why just said string and char are closely related. Because the Java string is a sequence of char values (which is actually the final char array). This is well documented in the source code:
/** The value is used for character storage. */private final char value[];
In order to prove the above things, you can write a fun code:
public class test{\u0070\u0075\u0062\u006c\u0069\u0063 static void Main (string\u005b\u005d args) {System.ou T.println ("This was not PI: \u03c0"); }}
After running you can see the following output:
This isn't pi:π
But some people don't misunderstand, like Java's
String name = "Jeff Li";
Think of it as the C language of char name[]= "Jeff Li"; This perception is wrong, and other it is more like the char* pointer:
Char *name = "Jeff Li";
The most common is also the most frequently used Boolean equals () analysis, the source code is as follows:
Public boolean equals (Object anobject) { if (This == anobject) { return true; } if (anobject instanceof string) { String anotherString = (String) anObject; int n = value.length; if (N == anotherstring.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
From the source of the slow way: Example (A.equals (B))
First: if (this = = AnObject)
This represents a, in fact, is to determine whether its B object and a reference variable represents the address of two variables stored in the heap (the same content as the stack reference is natural).
Second: if (anobject instanceof String)
The instanceof operator in Java is used to indicate at run time whether an object is an instance of a particular class. This sentence for the following strong turn String anotherstring = (string) anobject;
Third: I just started some doubts in the source: I think it is a string type of anotherstring get to the Char value sequence (char[]), this is back to the previous content.
Anotherstring.value
Guess is the above meaning, if Daniel has some place to say, please say. Because I want to achieve a simple stringcopy, this is always not, do not know why.
Four: The rest is char[] direct cycle comparison.
There are also some commonly used split (regular) format here not to publish one by one views.
In addition, there is a good code share: from Org.apache.commons.lang
public static Boolean isEmpty (String str) {return str = = NULL | | str.length () = = 0; }
Three. Thanks to the source of knowledge and summary
String is a commonly used thing, the old cliché. Take a look at your own opinion.
A. Char says string
Two. The understanding of string and the source code analysis
Resources:
http://elmer08.blog.163.com/blog/static/71204254201011121382438/
http://blog.csdn.net/sunzhenhua0608/article/details/7628663
http://blog.csdn.net/f562867448/article/details/8679550