这就是我所说的:
package finalExample;
public class FinalExample {
public static void main(String args[]) {
final String str1 = "str";
final String str2 = "ing";
String str11 = "str";
String str22 = "ing";
System.out.println("Using equals() for non final :" + str11.equals(str22));
System.out.println("Using == for non final :" + (str11==str22));
System.out.println("MAGIC Using == for non final :" + str11==str22);
System.out.println("Using equals() for final :" + str1.equals(str2));
System.out.println("Using == for final :" + (str1==str2));
System.out.println("MAGIC Using == for final :" + str1==str2);
}
}
输出是:
Using equals() for non final :false
Using == for non final :false
false
Using equals() for final :false
Using == for final :false
false
两个 MAGIC 语句都不会打印。为什么会这样?