http://sarin.iteye.com/blog/603684/
Let's look at an example with the following code:
Java code public class test { public static void main (String[] args) { String str = "ABC"; String str1 = "ABC"; string str2 = new string ("abc" ); system.out.println (STR == STR1); system.out.println (str1 == "abc"); system.out.println (str2 == "abc"); system.out.println (STR1 == STR2); system.out.println (Str1.equals (str2)); system.out.println (Str1 == str2.intern ()); system.out.println (Str2 == str2.intern () ); system.out.println (Str1.hashcode () == Str2.hashcode ()); } }
public class Test {public static void main (string[] args) {
String str = "ABC";
String str1 = "abc";
String str2 = new String ("abc");
System.out.println (str = = STR1);
System.out.println (str1 = = "abc");
System.out.println (str2 = = "abc");
System.out.println (str1 = = str2);
System.out.println (Str1.equals (str2));
System.out.println (str1 = = Str2.intern ());
System.out.println (str2 = = Str2.intern ());
System.out.println (str1.hashcode () = = Str2.hashcode ()); }
}
If you can directly determine the results of these 8 outputs, the following analysis will not be seen. But I think there's a lot of people who have a superficial understanding of the problem with this string object, and here's an analysis of the Java language string classes and objects and their running mechanisms.
Make a basic description of heap (heap) memory and stack (stack) memory problems. The data structure of the heap and stack is not explained here. When the Java language uses memory, stack memory mainly holds the following: basic data types and object references, while heap memory objects, stack memory faster than heap memory. summed up in a sentence is: reference in the Stack and objects in the heap.
There are two kinds of comparisons in Java, the = = and Equals () method, Equals () is the method of the object class, and the Equals () method defined in the object class is implemented as follows:
Java code public boolean equals (Object obj) {return (this==obj); }
public boolean equals (Object obj) {return
(this==obj);
}
The string class overrides the Equals () method, changing the principle that these types of objects are equal, that is, the principle of determining whether an object is equal depends on whether the contents of the two are equal.
With that in mind, we say that the essence of the String,string class is the character array char[], followed by the final, and not inheritable, of the string class, which may be overlooked by most people, and again a string is a special package type that can be assigned directly when you use a string , you can also use new to create objects, but the implementation mechanism of the two is different. There is also the concept of a string pool in which the Java runtime maintains a string pool in which string objects in the pool cannot be duplicated, not created, and then dropped. The string pool is not part of the heap and stack, but belongs to a constant pool. The real meaning of the above code is analyzed below
Java code String str = "ABC"; String str1= "ABC";
String str = "abc";
String str1= "ABC";
The real meaning of the first sentence is to create an object "ABC" in the string pool, and then refer to the object "ABC" in the pool when Str points to it. When the second sentence executes, because "ABC" already exists in the string pool, so it is no longer created, then Str==str1 returns True. str1== "ABC" must be correct, there is only one "ABC" in the string pool, and both Str and str1 point to "ABC" in the pool.
Java code String str2 = new String ("abc");
String str2 = new String ("abc");
This is a hot issue for Java SE, and it is well known that this single sentence creates 2 string objects, and based on the above, only creates str2 references in stack memory, creates a string object on heap memory, the content is "ABC", and str2 points to the first address of the heap memory object.
The following is the question of str2== "ABC", apparently not, "ABC" is an object in a string pool, and str2 is pointing to a string object of heap memory, which is the address that must be ranged.
Str1.equals (STR2), this is true, as mentioned earlier, the equals of the string class rewrite the Equals () method of the object class, which actually determines whether the content is the same.
The next intern () method, in the Javadoc documentation, describes the Intern () method: Returns a normalized representation of a string object. How to understand this sentence. In fact, the procedure does this: The method now looks for an object in the string pool and returns a reference to the object in the string pool if it exists.
In this case, the string pool has "abc", then the call to the Intern () method returns the "ABC" object reference in the pool, which is equivalent to STR/STR1, and str2 is different because str2 is pointing to heap memory.
The Hashcode () method is the hash code that returns the contents of the string, and since the same content, the hash code must be the same, they are equal, this is easy to understand.
Look at the following example:
Java code public class test { private static string str = "ABC"; public static void main (String[] args) { String str1 = "a"; String str2 = "BC"; String combo = str1 + str2; system.out.println (Str == combo); system.out.println (Str == combo.intern () ); } }
public class Test {
private static String str = "abc";
public static void Main (string[] args) {
String str1 = "a";
String str2 = "BC";
String Combo = str1 + str2;
System.out.println (str = = combo);
System.out.println (str = = Combo.intern ());
}
This example is used to illustrate that when using the + connection string, the object is actually created in the heap content, then combo points to the address of the space at the heap memory "ABC" string, which is clearly str==combo wrong, and Str==combo.intern () is correct, There is also an "ABC" in the string pool, which is returned directly, and Str also points to the "ABC" object in the string pool. This example shows that any modification string is a reallocation of memory space, which makes the string object mutually exclusive. That is, the contents of a string are not changed until the new object is generated.
At the same time the problem has come, the use of the + connection string to generate new objects each time, but also on the heap memory, while the heap memory speed is relatively slow (relative), then a large number of connection strings when the direct + is not desirable, of course, the need for a high efficiency method. The StringBuffer and StringBuilder provided by Java are the solution to this problem. The difference is that the former is thread safe and the latter is not thread safe, StringBuilder after JDK1.5. StringBuilder with no guarantee of safety are more efficient than stringbuffer.
Since the JDK1.5, the Java Virtual machine executes the string + operation, the internal implementation is also StringBuilder, before using StringBuffer implementation.
Welcome to communicate and hope to be useful to users.