package tk;
public class application
{
public static void main(String[] args){
final String str1 = "hello" ;
String str2 = "hello";
System.out.println(str2 == "hello");
System.out.println(str1 == str2);
String str3 =str1 + "world";
String str4 =str2 + "world";
System.out.println(str3);
System.out.println(str3 == str4);
System.out.println(str3.equals(str4));
}
}
equals()比较的是自变量的数值,而==比较的则是自变量引用的是否是同一对象
true
true
helloworld
false
true
str3==str4false的原因是:str1被final修饰后变为常量,所以str3为常量加常量在常量池里面,str4则是变量加常量;结果在堆里面,所以结果不同