Saturday, May 3, 2008

Comparison of Objects with equals

== relational operator is used to compare basic data types in java e.g. if we want to compare two integers, we will do it as follows:

int a = 5;
int b = 5;
if (a == b)
System.out.println("equal");
else
System.out.println("not equal");

Output of above java code will be "equal". but this "==" operator works only with basic data types, even it will not work if we use wrapper classes of basic data types e.g.

Integer a = new Integer(5);
Integer b = new Integer(5);
if (a == b)
System.out.println("equal");
else
System.out.println("not equal");


Output of above code will be "not equal". Why "==" operator does not applicable for objects is that because it compares two references of object not contents of objects. Now for comparison of contents of objects there is "equals" method in each object e.g. if we change above comparison of wrapper classes with this code, it will work correctly:

Integer a = new Integer(5);
Integer b = new Integer(5);
if (a.equals(b))

System.out.println("equal");
else
System.out.println("not equal");

Output of above code will be "equal" This "equals" work fine for wrapper classes but does not run correctly for custom classes because it compares two object references by default just like "==" operator. To change behaviour of "equals" we will have to override it in our classes. Suppose i have a class "Book" which has attributes like name, isbn, author, publisher. Now uniqueness of book is checked using isbn number so to compare two books, override "equals" method and in that implementation check the "isbn" of book. Code Structure is given below:

public class Book{
private String isbn;
private String name;
private String author;

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

@Override
public boolean equals(Object obj) {
Book book = (Book)obj;
if (this.isbn.equals(book.getIsbn()))
return true;

return false;
}

public static void main(String args[]) {
Book b1 = new Book();
b1.setName("Java");
b1.setAuthor("Amer");
b1.setIsbn("12334");
Book b2 = new Book();
b2.setName("Java");
b2.setAuthor("Amer");
b2.setIsbn("12334");
if (b1.equals(b2))
System.out.println("equal");
else
System.out.println(" not equal");
}
}

output of above code will be equal. even if name or author of two are different because my implementation of "equals" just checks for "isbn".

In my later post, i will exlain best practices related to overriding of "equals" method.

Friday, April 25, 2008

Object Oriented Programming vs Non Object Programming

Often people are confused about real benefits which are provided by OOP. I am trying to remove their confusion in this episode of blog. Every innovation in this world is related to solution of a problem. There is a problem, then we try to find out solution of that problem. So we have to work in two spaces. one is Problem-space in which we concentrate on problem only. and second one is Solution-space in which we develop solution. When we develop solution through computer, we have to think in terms of machine which happened in assembly language. It is impossible for one to get a sense of problem while reading an assembly program. So there is a big gap between problem and solution. People have to develop different artifacts to bridge this gap. After assembly, some other languages came like C, prolog and others , which provided some abstraction on machine but still there was a big gap e.g. prolog concentrates on reasoning and facts. After that OOP made its position, which really reduced the gap between problem-space and solution-space. What we understand and see in problem, we can easily map to a solution which provides us good sense of problem. I explain it with an example e.g. we are developing a solution for a library. Now everyone knows that library has some books and students draw books from library for some time and then return to library. Now if we think on problem, we will come to know that everything in library is relating to three entities Book, Students, and Library itself. Now when we develop solution of this in OOP, we will map these entities to classes and by taking a general look on it, one can easily understand what's going on in the solution and to what things this solution is related.
In short it is the OOP which reduced the gap between problem-space and soluton-space.