public class Test {
public static void main(String[] args) {
String a = "hello2";
// 当final变量是基本数据类型以及String类型时,如果在编译期间能知道它的确切值,则编译器会把它当做编译期常量使用。也就是说在用到该final变量的地方,相当于直接访问的这个常量,不需要在运行时确定。这种和C语言中的宏替换有点像。因此在上面的一段代码中,由于变量b被final修饰,因此会被当做编译器常量
final String b = "hello";
String d = "hello";
String c = b + 2;
String e = d + 2;
// == 比较的实际是内存地址是否相等,而 equals 比较的是两个对象的引用是不是同一个相等,即同一个对象
// String 类中对 equals 进行了改写,比较字符串是否相等,所以。。。
System.out.println((a == c));
System.out.println(a);
System.out.println(e);
System.out.println((a == e));
System.out.println(a.equals(e));
}
}
参考: https://www.cnblogs.com/dolphin0520/p/3736238.html
https://www.cnblogs.com/dolphin0520/p/3592500.html