DESKTOP-ESBC3OR_20200828-蒋生桃
问题
final String x = "疑是" ;
final String y = "地上霜" ;
String s = "疑是地上霜" ;
String p = x + y ; // 注意这里的 + 起连接作用(连接字符串)
System.out.println( s == p ); // true
String q = x + "地上霜" ;
System.out.println( s == q ); // true
String w = "疑是" + y ;
System.out.println( s == w ); // true
String e = "疑是" + "地上霜" ;
System.out.println( s == e ); // true
为什么被final修饰的字符串变量相加之后与 s 比较是否相等结果都是 true ?
解决
因为被final修饰了, String p = x + y ; 就相当于 String e = "疑是" + "地上霜" ;所以返回true
吐槽
明天考试,加油
点赞