java in equse record and compare == (rpm)

String difference equals method of use and ==

The difference equals method and ==
  First of all we know, String can either be used as an object, but also as a base type to use. As used herein refers to a basic type only refers to, for example String s = "Hello", its use as substantially the same type on use int, int i = 1 ;, for example be used as an object, it is It refers to create a new object by new keywords, such as String s = new String ( "Hello"). But its internal action but it is still an object is created, we will discuss this later.
 
        Secondly, the comparative method of the String object need to know. Comparison between two concepts in Java objects, where the object is to get String: one is to use "==" to compare, this comparison is a reference for the two variables of type String, that if two variable of type String, String they reference the same object (i.e. a point to the same memory heap), the "==" result of the comparison is true. Another is to use equals () method to compare Object object, String object inherits from Object, and to equals () method has been rewritten. When two objects by comparing String equals () method, in fact, the contents of the string String object encapsulated compared, i.e. the same as the String object if the two strings are encapsulated content (including the same case), then equals () method returns true.
 
We will now create a String object to make a specific analysis:
 
1、/////////////////////////////////////////////////////////////////////////
String s1 = new String("Hello");
String s2 = new String("Hello");
 
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
 
Print result of the above snippet is:
 
false
true
 
        This result I believe we are well understood, two variables of type String s1 and s2, respectively, through the new keyword to create a new String object, the new object is created for each keyword assigned a new, independent memory heap . Therefore, when comparing the "==" whether they refer to the same object, it returns false. When compared by the equals () method returns true, because the two strings encapsulated content objects are identical.
 
2、//////////////////////////////////////////////////////////////////////////////////
String s1 = new String("Hello");
String s2 = s1;
 
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
 
Print result of the above snippet is:
 
true
true
 
        This result should be better understood, variable s1 or through new keyword to create a new String object, but s2 is not here to create a new String object through the new keyword, but directly to the assigned to s1 s2, that is a reference to the assignment s1 s2, s2 so the object is actually referenced objects referenced by s1. So when compared by "==" returns true. Since they refer to are the same object, when compared by equals () method must also return true, here equals () method in fact comparing the same object, it was definitely equal to their own slightly.
 
3、//////////////////////////////////////////////////////////////////////////////
String s1 = "Hello";
String s2 = "Hello";
 
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
 
Print result of the above snippet is:
 
true
true
 
        Why this result? So to analyze. First two String objects are used as a basic type, rather than by creating new keywords, so the virtual machine does not allocate new memory heap for both String objects, but to String pool to find.
 
        First is String s1 to find out whether the buffer pool has the same value "Hello" String object exists, then the buffer pool is empty String, String object is not the same value exists, so the opportunity to create this virtual String String object in the buffer pool, which action is the new String ( "Hello") ;. Then this String object reference assigned to s1.
 
        Then look for String buffer pool if there is the same value as "Hello" String object exists, then the virtual machine to find a String object to their same values, this is actually a String object is created String object as s1 s2. Since the value of a same object is found, then the virtual machine is not created for this purpose in a new String object, but the direct reference to the existence of the String object is assigned to s2.
 
        Since here it referenced s1 and s2 are the same String object, i.e. equal to their own, so that the above two comparison methods return ture.
 
        Here, the basic concept of the String object should have been understood. Now let me summarize briefly:
 
For String is used as a basic types:
 
1. If String is used as a basic type, then we treat this String object is a String buffer pool have.
2. If String is used as a base type, and this time its assigned value String buffer pool the same String object does not exist, then the time to create a new virtual machine for this purpose String object String and stored in the buffer pool.
3. If String is used as a base type, and at this time the same buffer pool exists and the specified String value String object, then the virtual machine will not do this at this time to create a new String object, and direct return String objects existing references.
 
String for use as an object:
 
1. If String is used as a target, then the virtual machine will do this to create a new String object that allocates a new memory heap for this object, and it is not owned by the String buffer pool that it is independent.
 
After understanding the above content, consider the following code snippet:
4、/////////////////////////////////////////////////////////////////////////////
String s1 = "Hello";
String s2 = new String("Hello");
 
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
 
Print result of the above snippet is:
 
false
true
 
        Analyzed according to the above summary. String The first line is to be used as a basic type of the object referenced s1 and therefore belongs String buffer pool. At this String buffer pool and its value is not the same String object exists, so the opportunity to do this to create a new virtual String object, namely new String ( "Hello") ;. String The second line is to be used as an object, so the object is not referenced by s2 String buffer pool, i.e. it is independent. By the new keyword to create a new virtual machine for this purpose String object, that is, it is assigned a new memory heap. Thus "==" result of the comparison is false, because not the same object referenced s1 and s2, which exist independently. And equals () method returns true, because the two strings encapsulated content objects are identical.
 
        Now, I believe we have to figure out exactly how the String object is a different story :) But this does not end, because the String object there are deeper applications.
 
        Here I will analyze the String object intern () method is applied:
intern () method returns a string specification of the representation of the object, i.e., the same string with a string of the content, but from the unique string String pool. It sounds a bit hard to pronounce, in fact, its mechanisms like the following code snippet:
 
String s = new String("Hello");
s = s.intern();
 
Function to achieve the above code segment simply as the following code segment:
 
String s = "Hello";
 
        You must began wondering? Then you can look at the second code segment. A second code segment, which means that by taking the same values ​​thereof a String String object from the buffer assigned to the pool by reference s. String String object if the buffer pool is not the same as the value of its existence, it is in it for this purpose to create a new String object. Then the first piece of code mean what is it? We know that through the new keyword to create an object, the virtual machine allocates a new memory heap for it. If you create the same content to ordinary objects, the virtual machine will be allocated for this purpose the same number of new memory heap, although their contents are identical. Take the String object, if the same content continuously create 10 String object (new String ( "Hello" )), Then the virtual machine 10 allocated for this purpose a separate memory heap. Assuming that the String object created by a string of content is very large, assuming a Stirng string object encapsulates the content 1M size, so if we create 10 this same String object, we will be pointless waste of memory space 9M . We know that String is the final category, it encapsulates the string is constant, so the String object inside (string) value can not be changed after it is created, and therefore String objects can be shared. So for the hypothesis just mentioned, we have created 10 String object with the same content, in fact, we only need to do this to create a String object, and then be shared by other String variable. To implement this mechanism, the only, the simplest way is to use the String buffer pool because the String object String buffer pool does not exist the same content. The intern () method is the way to use this mechanism. After calling intern () method on a String object has been instantiated virtual opportunity to find the same value String object with this Stirng object encapsulated contents in a string String buffer pool, and then refer to the original reference assigned to the String object String type variable. If the String object String buffer pool is not the same as the value of the object encapsulated by the presence of this string String content, then the virtual machine created for this purpose a new String object and its reference to the String type variable assignment that refer to the original String object . This will achieve the purpose of sharing the same String object, and that created the original using the new keyword String objects will be discarded and garbage collected away. This not only reduces the consumption of memory usage, improves performance, but also in comparison String object also more convenient, since the same String object to be shared, it is determined whether two String objects to be identical, only the need to use " == "to compare, without having to use equals () method to compare, it will not only easier to use, but also improves performance, because equals string object () method will be the dismantling of the string contents, and then one by one Compare, if the string content is very large, then the comparison operation is greatly reduced performance.
 
        Having said this, we may be a little fuzzy on the specific application, then I will give a simple example, in order to set out these concepts:
 
        Suppose there is a class that has a message recording method, which records the user's message came (assuming the message content may be large, and high repetition rate), and the message received by the sequentially recorded in a list. I think some friends would like this design:
 
import java.util. *;
 
public class Messages {
 
ArrayList messages = new ArrayList();
 
public void record(String msg) {
messages.add(msg);
}
 
public List getMessages() {
return messages;
}
}
 
        This design okay? Suppose we send to duplicate record () method with a message (messages from different users, so you can view each message as a new String ( "...")), and the message content is large, then the design will big waste of memory space, because the message recorded in the list are newly created, independent of the String object, although their contents are the same. So how can it be optimized, is actually very simple, look at an example of the optimization of the following:
 
import java.util. *;
 
public class Messages {
 
ArrayList messages = new ArrayList();
 
public void record(String msg) {
messages.add(msg.intern());
}
 
public List getMessages() {
return messages;
}
}
 
        As you can see, the original record () method messages.add (msg); snippet into a messages.add (msg.intern ()) ;, only the msg parameter called the intern () method, so the message will be repeated sharing mechanism, which reduces memory consumption and improve performance.
 
This example was a little far-fetched, but here just to set out these concepts!
 
At this point, the fog String object are eliminated, as long as you keep in mind these concepts, later String complex applications can build up on the basis of this analysis.

Guess you like

Origin www.cnblogs.com/shijianhenjinpo/p/11318148.html