[Reserved] string string pools and “==” and “equals ()” the difference between
May 2, 2011
string string pool: used to store string constants;
strong> works:
strong> strong> If the constant string already exists, then the address of the string directly to the reference; if the constant string does not exist, create a constant string , and put the string on the string pool, then the address of the string to the reference.
strong> “==” strong> : 1, for data types, the comparison is numeric; 2, for reference types, compared
strong> strong> in the memory address.
strong> euqals (): strong> comparison is a string of data is the same
strong> intern (): strong> operation string pool
When the intern method is invoked, if the pool already contains a string equal to this String object (the object by the equals (Object) method to determine the ), it returns the string pool. Otherwise, this String object is added to the pool, and returns a reference to this String object.
strong> analysis example:
strong> public class TestString {
strong> < strong> public static void main (String [] args) {
String s1 = “abc”;
String s2 = “abc” “d”;
String s3 = new String (“abc”);
String s4 = “abcd”;
strong> System.out. println (“=================”);
System.out.println (s1 == s2); / / false
System.out.println (s1 == s3); / / false
System.out.println (s2 == s3); / / false
System.out.println (s2 == s4); / / true
System.out.println (“=================”);
System.out.println (s1.equals (s2 ));// false
System.out.println (s1.equals (s3 ));// true
System.out.println (s2.equals (s3 ));// false
System.out.println (s2.equals (s4 ));// true
System.out.println (“=================”);
String s5 = new String (” hello “);
String s6 = s5.intern ();
System.out.println (s5 == s6); / / false
System.out.println (s5.equals (s6 ));// true
strong> System.out.println (“========= ========”);
strong> String a = “ab”;
String b = “a” “b “;
System.out.println (a == b); / / true
System.out.println (“========== =======”);
String str1 = “java”; / / strong> known at compile-time constant pool object on
< br /> String str2 = “blog”;
String str = str1 str2; / / str1, str2 is variable at run time to know, that str1 str2 is created on the heap
System.out.println (str); / / output is javablog
System.out.println (str == “javablog “);// output is false
strong> System.out.println (“=================”);
}
strong> }
strong> analysis:
strong> When confronted with String a = “Hello”; this statement, Java will look for first in the string pool already exists “Hello” string, and if not, to establish the string “Hello” object, then add it to the string pool, then it returns a reference to the variable a, a point to this address strong>; then encounters the statement String b = “Hello”, then the pool has a string “Hello”, so directly to the variable b also point to this address, eliminating the need for re-allocation of trouble. In Java, the operator “==” for two basic types, is to determine its contents are the same for the two objects, it is the address to determine whether its the same strong>, so a == b returns true.
then String c = new String (“Hello”) and how to handle it? If this is written, it will not go to visit a string pool, but the first to open up space for the variable c, then the value is written to space. Therefore a == c returns false, c == d also returns false.
As String equals method, because it is not the address of the object, but the value of the object strong>, it returns true is not surprising.
—————————————— ——– ——————
Java virtual machine has a string pool, holds almost all of the strings the object. string expression always points to the string pool object strong>. Use the new operator to create a string object string does not point to the object pool but you can use the intern method (String intern class method: public native String intern ();) to point to the string pool object (Note: this is a local method call this method, JAVA virtual machine first checks whether the string pool object already exists and is equal to the object exist – using the equals method to determine if a string is returned object reference to the pool; if No, you first create a pool in the same string value of the String object, and then returns a reference to it). If the pool two equal strings using “==” to compare will return true.
—————————————— ——– ———————
Stringstr1 = “java”;
Stringstr2 = “blog”;
Strings = str1 str2;
System.out.println (s == “javablog”);
The result is false. Jvm does type, such as Stringstr1 = “java”; of the String object on the constant pool, but it is done at compile time then, and Strings = str1 str2; is to know at run time, that str1 str2 is created on the heap strong>, so the result is false the.
compare two strings already in the string pool object can use the “==” for, with more than equals operator faster
