public class Employee {
private String firstName;
private String lastName;
private int age;
public Employee(String firstName, String lastName, int age) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public boolean equals(Employee s) {
if (this.firstName==s.firstName && this.lastName == s.lastName) { //Line 1
return true;
}
return false;
}
public static void main(String agrs[]) {
Employee e1 = new Employee("Jon", "Smith", 30);
Employee e2 = new Employee("Jon", "Smith", 35);
System.out.println(e1.equals(e2));
}
}
行 1 は、2 つの文字列を == 演算子で比較しているときに true を返しました。e1 と e2 の "Jon" と "Smith" は 2 つの異なる参照 (メモリの場所) を持つと思いました。
e1 と e2 の "Jon" と "Smith" が同じ参照を持つように処理している概念は何ですか? (文字列のキャッシュ??! または単なる偶然ですか?)