class string{
static String str = new String("yes");
string(){}
}
public class rtyh {
public static void main(String[] args){
string strr = new string();
String st = new String("yes");
String str = new String("yes");
String str1 = "yes";
String str2 = "yes";
System.out.println(str1==str2);
System.out.println(st.equals(str));
System.out.println(st==str);
System.out.println(str.equals(strr.str));
System.out.println(str==strr.str);
}
}
結果為 ture
ture
ture
ture
false
所以,==用來對引用的判斷,如果是引用的同一個內存單元則相等,否則不相等.equals()用來對字符串內容的逐一判斷.