1

So at school we are learning Java for the first time and we wanted to simulate a Java libray application where we have two classes named "Book" and another one name " and there can be no book with the same ISBN in the same shelf. So what i did was like the following inside of the shelf class and it works fine.

private boolean checkIsbnNumber(Book book){
        boolean isbnTaken = false;
        for(Book bList : this.bookList){
            if(bList.getIsbn().equals(book.getIsbn())){
                System.out.println("The books isbn number is taken");
                isbnTaken = true;
            }
        }
        return isbnTaken;
    }

But what our lecturue told us to is the following, go on the book class and override the hash and the equals method to the following.

@Override
    public boolean equals(Object obj) { 
        if (obj instanceof Book) 
            return isbn.equals(((Book)obj).isbn); 
        else 
            return false; 
    } 

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + Objects.hashCode(this.isbn);
        return hash;
    }

Then do the following inside of the shelf class once again.

public boolean checkISBN(Book b){
        boolean isbnExists = false;
        for (Book bList : bookList)
        {
            if(bList.getIsbn().equals(b.getIsbn())){
                System.out.println("Book ISBN already exist");
                isbnExists = true;
            }
        }
        return isbnExists;  
    }

What i want to know is what is the point of doing it? is there any benefits over it? and why are we calling the equals method inside the equals method again? Some help would be greatly appreciated.

A. R. Younce
  • 1,913
  • 17
  • 22
Steve
  • 1,213
  • 5
  • 16
  • 29
  • Umm...i'm not seeing the difference. Aside from the private vs public, the only obvious changes are to variable/method names and output text. (BTW, outputting text here seems wrong.) What the method actually *does*, how it works, hasn't changed one bit. – cHao Dec 06 '14 at 17:52

3 Answers3

3

if you really want to use equals method than your comparison should be like this :-

public boolean checkISBN(Book b){
    boolean isbnExists = false;
    for (Book bList : bookList)
    {
        if(**bList.equals(b)**){
            System.out.println("Book ISBN already exist");
            isbnExists = true;
        }
    }
    return isbnExists;  
}

Notice highligheted part.

your lecturer wants to do this to make understant the usage of equals method. let me explain you difference between your approach and his approch. Then advantage of doing this :- 1.Without using equals :- In this approach , your comparing the ISBN of book, and you are not putting book having same ISBN in shelf.

  1. With equals overriding :- First , you are overriding equals, which means you are saying two book having same ISBN are equal. Step two you comparing if books are equal.

So what is advantage , let say you need to other operation based on same equality also in your program , then you will not end up writing ISBN logic everywhere , you just need to write equals. Advantage 1 :- In real world, differnt parts of software are written by different persons, so if you are writting book , other person will not need to know what makes two book equal. his part of design or requirment, will be two write code based on equality.

Advantage 2 :- Tommarrow, you decide that equality of book is not only based on ISBN no. but also author , you only need to update equals method and everyones code will work fine no need to change everyone.

In real programming world, object oriented coding done for real objects and sometimes object changes due to businees channges in real world

Panther
  • 3,312
  • 9
  • 27
  • 50
  • Not a good practice to highlight something within the code part. – Maximin Dec 06 '14 at 18:02
  • Okay this makes perfect reasoning to me thank you for explanation. – Steve Dec 06 '14 at 18:05
  • Maximin thanks for suggestion , however i highlighted as my piece as same as his , only single line. And he is college student to I thought he might miss the difference. I believe he missed same also when his lecuterer missed same – Panther Dec 06 '14 at 18:06
2

What i want to know is what is the point of doing it?

Well your condition is that no two ISBNs can be the same. You're changing your equals method so that it will consider two Book objects equal if the ISBN is the same.

and why are we calling the equals method inside the equals method again?

Your ISBN is an Object. When you're comparing two Objects, if you do it like this..

Object1 == Object2

Then you are testing if they are the same object. Not if they have the same value. When you call equals, you are testing some other condition. For example, in the case of String..

String s = new String("Hello");
String s2 = new String("Hello");

s == s2; // Returns false.
s.equals(s2); // Returns true.

This is because s is not the same object as s2, but it has the same value.

Extra Reading

  • Here is a really in depth analysis of why you need to override the hashCode method.

  • Here is an SO detailing exactly why you need to use equals with Objects and not ==.

Community
  • 1
  • 1
christopher
  • 26,815
  • 5
  • 55
  • 89
1

In general, you override equals() for two reasons:

  • You want to have a custom equality semantcs for your object, or
  • You want to use your object in a hash container.

In both cases you must override hashCode as well.

For example, if you want to attach comments to each book without modifying the book object itself, having proper equals and hashCode would let you do this:

Map<Book,String> comments = new HashMap<Book,String>();
comments.put(myBook, "This is a good book");

Although you can determine equality of book objects by checking their ISBN outside the class, it is better to have this functionality inside the class as well, because people who do not know your specific class would be able to understand what is going on when they see equals. They would also be able to use your class without learning it in-depth, because writing this

if (book1.equals(book2)) ...

is easier than writing this

if (book1.getIsbn().equals(book2.getIsbn())) ...
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • The easier part one i understand okay, so the lecturere has to change the code to that aswell right? – Steve Dec 06 '14 at 17:57
  • @Colosuslol Right, once you have `equal` implemented the way you do, your `checkISBN` can switch to using `if (book1.equals(book2))` code. – Sergey Kalinichenko Dec 06 '14 at 18:03