About the various = = and equals of string, some people do not understand
Nutshell
String S1 = "Hello" declares a constant, which is in a constant pool.
String s2 = new String ("Hello"), which declares an object that has a separate space in memory.
= = Compares the address of a string
Equals compares the contents of a string
Public classteststring{ Public Static voidMain (string[] args) {String str1=NewString ("Hello"); String str2=NewString ("Hello"); String STR3= "Hello"; String STR4= "Hello"; System.out.println (Str1.hashcode ()); System.out.println (Str2.hashcode ()); System.out.println (Str1.equals (str2)); System.out.println (str1==str2); System.out.println (Str1.equals (STR3)); System.out.println (str1==STR3); System.out.println (Str3.equals (STR4)); System.out.println (STR3==STR4); }}
The output is:
6960965069609650truefalse true True False
A comparison of strings in Java