equals()のtrue, falseの判定
Java初心者です。equals()で以下のコードで文字列を比較しているのですが、実行結果が「first and second book are equal」となります。equals()は文字列の比較なのはわかりますが、なぜ、「first and other book are equal」も出力されないのでしょうか?
Java
1public class Book { 2 private String ISBN; 3 public Book(String ISBN){ 4 this.ISBN = ISBN; 5 } 6 public String getISBN(){ 7 return ISBN; 8 } 9 public static void main(String[] args){ 10 Book firstBook = new Book("0201914670"); 11 Book secondBook = firstBook; 12 Book otherBook = new Book("0201914670"); 13 14 if(firstBook.equals(secondBook)){ 15 System.out.println("first and second book are equal"); 16 } 17 if(otherBook.equals(firstBook)){ 18 System.out.println("first and other book are equal"); 19 } 20 } 21}
first and second book are equal
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。