https://www.jianshu.com/p/1cc6bfc4ee48
public class TestMain01 {
public static void main(String[] args) {
String hw = "hello world";
String hello = "hello";
final String finalWorld2 = "hello";
final String finalWorld3 = hello;
final String finalWorld4 = "he" + "llo";
String hw1 = hello + " world";
String hw2 = finalWorld2 + " world";
String hw3 = finalWorld3 + " world";
String hw4 = finalWorld4 + " world";
System.out.println(hw == hw1);
System.out.println(hw == hw2);
System.out.println(hw == hw3);
System.out.println(hw == hw4);
}
}
output
false
true
false
true
无final
public class TestMain01 {
public static void main(String[] args) {
String hw = "hello world";
String hello = "hello";
String finalWorld2 = "hello";
String finalWorld3 = hello;
String finalWorld4 = "he" + "llo";
String hw1 = hello + " world";
String hw2 = finalWorld2 + " world";
String hw3 = finalWorld3 + " world";
String hw4 = finalWorld4 + " world";
System.out.println(hw == hw1);
System.out.println(hw == hw2);
System.out.println(hw == hw3);
System.out.println(hw == hw4);
}
}
output
false
false
false
false