January 4, 2011
to see an example:
Example A:
Java code

String str1 =” java ” ;
String str2 = “java”;
System.out.print (str1 == str2);
String str1 = “java” ; String str2 = “java”; System.out.print (str1 == str2);
earth a little Java-based people know will output false, because == compares the references, equals compare the content. I did not flicker all, you can run on your machine, the result is true! The reason is simple, String objects are placed in the constant pool, and again “java” when the string, JVM is very excited to have a reference point to str2 “java” object, it considers itself to save the memory overhead. Oh it is not difficult to understand
example B:
Java code

String str1 = new String (” java “);

String str2 = new String (“java”);
System.out.print (str1 == str2);
String str1 = new String (“java”) ; String str2 = new String (“java”); System.out.print (str1 == str2); seen on the cases are wiser, this certainly will output true! Unfortunately, JVM does not do so, the results are false. The reason is very simple, example A, the kind of statement is really a way to create the String constant pool “java” object, but once they see the new keyword, JVM will allocate space on the heap as a String. Both methods dubious statement, that is what I put “how to create a string object” into the reasons behind the terms. We have to yourself, there is an example.
example C:
Java code

String str1 =” java “;
String str2 =” blog “; < br />
String s = str1 str2;
System.out.print (s == “javablog”);
String str1 = “java” ; String str2 = “blog”; String s = str1 str2; System.out.print (s == “javablog”); Look at this example, many comrades not arrogantly is true or false, right. Loves to play fast thinking people would say it is false … … Congratulations to you, you will answer in it! To the “right” you completely remove the word correctly. The reason is simple, JVM will indeed have type such as String str1 = “java”; of the String object on a string constant pool, but it is done at compile time so, while String s = str1 str2; at run-time we know (we saw through the course, but Java must be only known at runtime, the human brain and the structure of different computer), that str1 str2 is created on the heap, s of course impossible to point to the reference string constant pool object. No people continue to see examples of the collapse of D.
example D:
Java code

String s1 =” java “;
String s2 = new String (” java “);
System.out.print (s1.intern () == s2.intern ());< br />
String s1 =” java “; String s2 = new String (“java”); System.out.print (s1.intern () == s2.intern ());
intern () is the stuff? Anyway, the result is true. If you have not used this method and trained programmers will see the JDK documentation. Simply put, it is to use intern () method you can use “==” string compare the contents. I see the intern () method in the end what the use, before I consider it much more than a. In fact, I wrote a lot more than this, intern () method, there are still many problems, such as efficiency, to achieve the non-uniform … …
example E:
Java code
< br />
String str1 = “java”;
String str2 = new String (“java”);
System.out.print (str1.equals (str2));
String str1 = “java”; String str2 = new String (“java”); System.out.print (str1.equals (str2)); both in the constant pool or objects in the heap, with the equals () method is to compare the contents of that simple!
above quote from: http://hi.baidu.com/dairywg/blog/item/495f81b1188 5fa500823027f.html
note the following java code C2 () is not mentioned in the article above.
Java code

public class StringCompare {
public static void A () {
String str1 = “java”;
String str2 = “java”;
System.out.println (str1 == str2); / / true
}

public static void B () {
String str1 = new String (“java”);
String str2 = new String (“java”);
System.out.println (str1 == str2); / / false
}
public static void C () {

String str1 = “java”;
String str2 = “blog”;
String s = str1 str2;
System.out.println (s == “javablog”); / / false
}
public static void C2 () {
String str1 = “javablog”;
String str2 = “java” “blog”; / / at compile time optimization into a String str2 = “javablog”;
System. out.println (str1 == str2); / / true
}
public static void D () {
String s1 = “java “;
String s2 = new String (” java “);
System.out.println (s1.intern () == s2.intern ()); / / true
}
public static void E () {
String str1 = “java”;
String str2 = new String (“java”);
System.out.println (str1.equals (str2)); / / true
}
public static void main (String [] args) {
A ();
B ();
C (); < br />
C2 ();
D ();
E ();
}
}
output ============
true
false
false
true
true
true