Reprint Please specify source: http://blog.csdn.net/dmk877/article/details/49420141
Whether you are a novice programmer or an old hand, you must feel particularly familiar with string, because the string class has been learned since we learned Java basics, but is the string type as simple as we think? In fact, the knowledge point of String type is still more. Today and everyone to discuss, about the string of some easily confusing place, nonsense not much to say into the topic ... Please comment if there is any mistake, please leave a message if you have any questions. I will change or answer in the first time
Through this blog you will learn the following knowledge
Differences between ①== and equals, string a= "abc" and string A=new string ("abc") of heap memory and stack memory changes
②string a= "abc" and string A=new string ("abc") Two ways of declaring the difference
③ "abc". The difference between equals (ABC) and A.equals ("abc") from the source to analyze why "abc". Euqals (str) avoids null pointers
The value of the ④string object cannot be modified, why the value can be re-assigned, and change
⑤string A; and String a = null,string a = "" difference ⑥string S1 = new String ("S1"); String s2 = new String ("S1"); a total of several objects were created? 1, = = and equals difference, string a= "abc" and string A=new string ("abc") of the heap memory and stack memory changes we first look at a piece of code
[Java]View Plaincopy
- Public class Stringdemo {
- public static void Main (string[] args) {
- String str1 = "Hello";
- String str2 = new String ("Hello");
- String STR3 = str2;
- System.out.println ("STR1==STR2:" + (STR1==STR2));
- System.out.println ("STR1==STR2:" + (STR1==STR3));
- System.out.println ("STR1==STR2:" + (STR2==STR3));
- }
- }
Print the results as follows why? The above example str1, STR2, STR3 content are the same, but the result of comparison is some equal, some are not equal, this is why? First look at the memory allocation diagram above, it is clear that the contents of each string object are actually saved to the heap memory, and the contents of the heap are equal. However, for str1 and STR2, the address heap memory addresses are unequal, so although the content is equal, but the address values are not equal, "= =" is used for numeric comparisons, so str1 and str2 are not equal, STR2 and STR3 are equal because str2 and STR3 point to the same memory address. So "= =" is used for the comparison of address values. So how do you tell if the contents of two strings are equal? Let's look at an example first.
[Java]View Plaincopy
- Public class Stringdemo {
- public static void Main (string[] args) {
- String str1 = "Hello";
- String str2 = new String ("Hello");
- String STR3 = str2;
- System.out.println ("str1 equals str2:" + (Str1.equals (str2)));
- System.out.println ("str1 equals str2:" + (Str1.equals (STR3)));
- System.out.println ("str1 equals str2:" + (Str2.equals (STR3)));
- }
- }
The operation results are as follows
Because the Equals method works by comparing content, the return result here is true. Summing up the above two examples we can conclude that the difference between "= =" and equals is: "= =" is used for numerical comparison, in string "= =" for the comparison of address values, and equals compares the contents of string. 2. The difference between the two ways of declaring the first thing to understand is that a string is an anonymous object of string. We can pass "Hello". Equals ("Hello") prints the result to true for validation, because "Hello" can be called by "Hello". Equals () directly to the method in string, so for string str1= "Hello" , in effect, the use of a heap memory space in the heap memory to the Str1 object, and using this method has another benefit, that is, if a string is already referenced by a name, then there will be the same string declaration later, will not re-open space, and continue to use the already open heap memory, what do you mean? We use an example to illustrate that the code for this example is as follows
[Java]View Plaincopy
- Public class Stringdemo {
- public static void Main (string[] args) {
- String str1 = "Hello";
- String str2 = "Hello";
- String str3 = "Hello";
- System.out.println ("STR1==STR2:" + (str1 = = str2));
- System.out.println ("STR1==STR3:" + (str1 = = STR3));
- System.out.println ("STR2==STR3:" + (str2 = = STR3));
- }
- }
Printing results are as follows
Hey, what's the situation? We have just concluded that "= =" compares the address value in the string, why is the print result true? In fact, this is a shared design in Java, the design idea is to form a pool of objects in Java, to hold multiple objects in this pool, the newly instantiated object if it has been defined in the pool is no longer redefined and removed from the pool to continue to use. String uses this design, which has a string pool maintained by the string class in the Java runtime environment. When executing the statement string str1= "Hello", first look in the string pool for the existence of the string "Hello", if present, assign "Hello" directly to str1, and if it does not exist, create a new string "Hello" in the string pool before assigning it to str1. When executing the statement string str=new string ("Hello"), create a new string "Hello" (Note: The new string "Hello" is not in the string pool), regardless of whether there is a string "Hello" in the string pool, and then pay it to Str. The previous statement is efficient, and the latter statement is inefficient because the new string takes up memory space. String str = new String () creates an empty string that is the same as String Str=new string (""). We conclude that the string created by "" is a constant, the compilation period has been determined to be stored in the string pool, and objects created with the new string ("") are stored in the heap memory (head) created at run time. By comparing the two implementations above, it is possible to know which is more appropriate and that the operation of the string should be done directly with the direct assignment, rather than using the constructor method to pass the string in such a way that the garbage space can be avoided. 3. What is the content of the string that is not mutable? Maybe some say you don't fool me here, how could this not be nonsense? Let's take a look at an example code like this first.
[Java]View Plaincopy
- public class stringdemo {
- public static void main (string[] args) {
- string str=
- str=str+ " world!";
- system.out.println ( Span class= "string", "str=" +str)
- }
- }  
Printing results Maybe some people will say you see this is not a change? The start is "Hello" followed by "Hello world!". From the program run results found that the contents of the string object has actually been modified, but the content is really modified it? The following memory allocation diagram illustrates that the contents of a string cannot be changed from a clear discovery that a change in the contents of a string object is actually done through a "disconnect-connect" change in the memory address, and nothing changes in the contents of the string itself. 4. "ABC". The difference between equals (str) and str.equals ("abc"), from the source code analysis why "ABC". Euqals (str) avoids null pointers we should all have heard of "abc". Equals (str) is a notation that avoids null pointers, Why? There may be a lot of people who write like this, but don't understand what the principle is? Let's start with an example.
[Java]View Plaincopy
- Public class Stringdemo {
- public static void Main (string[] args) {
- String str1 = "Hello";
- String str2 = null;
- System.out.println (Str1.equals (str2));
- }
- }
Look at the code again.
[Java]View Plaincopy
- public class stringdemo {
- public static void main (string[] args) {
- string str1 =
- string str2 = null;
- system.out.println (Str2.equals (str1));
- }
- }  
The only difference with the above code is str1.equals (STR2), here is Str2.equals (str1), we look at the printing results will be what kind. I said to his uncle, the empty hands, why? Why St1.equals (STR2) will print false, and str2.equals (STR1) to the null pointer, this is not scientific ah. To know the reason is the most persuasive source code, we look at the equals source in the String class is not OK, OK let's go to see the string class in the source of equals is as follows
[Java]View Plaincopy
- /**
- * Compares this string to the specified object. The result is {@code
- * true} if and only if the argument are not {@code null} and is a {@code
- * String} object that represents the same sequence of characters as this
- * object.
- *
- * @param anobject
- * The object to compare this {@code String} against
- *
- * @return {@code true} if the given object represents a {@code String}
- * Equivalent to this string, {@code false} otherwise
- *
- * @see #compareTo (String)
- * @see #equalsIgnoreCase (String)
- */
- public Boolean equals (Object anobject) {
- //Judging whether it is compared with yourself
- if (this= = anobject) {
- return true;
- }
- //Determine if the AnObject passed is a string type instance
- 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;
- //characters-by-character comparison
- While (n--! = 0) {
- if (v1[i]! = V2[i])
- return false;
- i++;
- }
- return true;
- }
- }
- return false;
- }
From the source code we see the first to determine whether it is compared with their own, and then judge the object is not a string type of instance, note that the role of instanceof this keyword is to determine which class an object is an instance, note here is the instance, we direct string str2= NULL, at this point str2 is not an instance of string, and you can verify it, because string str2=null, declares a string type of str2, and requests an address in memory, but the address does not point to any reference address, So when executing this judgment, jump out of the IF statement and return false. So I won't be able to quote you a free hand. 5.String A; and string a = null,string a = "" Difference of string A; Declare a string of type A, that is, no memory address in the request, and not in memory any point to the reference address;
string a = null; declares a string type A, while requesting an address in memory, but the address does not point to any reference address;
String a = ""; Declare a string of type A, both in the memory of the application of the address, which also points to a reference to the string reference address;
String a=null,string b= "Hello"; System.out.println (a+b); output Nullhello; 6.String S1 = new String ("S1"); String s2 = new String ("S1"); a total of several objects were created? For this problem, it is not difficult to understand the above analysis, first string s1 = new String ("S1"), we see ("S1"), at this time "S1" as a constant is read into, So you create an object in the String pool, and then you see that new creates another object in the heap memory (head), so when you execute string s1=new string ("S1"), you create two objects, and then you execute string s2=new string ("S1"), ("S1") is also read as a constant, but since "S1" is already in the string pool, this time the object is not created in the string pool, and the object must be created when new is executed, so the string S2=new string ("S1" ); When an object is created. Look at the following two snippet code: The first code: string s1 = new String ("S1"); Create two objects, one reference
String s2 = new String ("S1"); Creates an object and creates one object at a time for each subsequent execution, one that references the second segment of code: String s3 = "XYZ"; Create an object, a reference
String S4 = "XYZ"; Do not create an object, just create a new reference
"Reprint" Java basis of the string equals, declaration way, such as big summary