Wednesday, August 31, 2005

Answering Java Questions

This blog entry is for answering the questions posted by Rahul in the last blog entry "Java Questions".

Question: Why should we override the equals() method of the Object class?
Answer: equals() is one of the methods of the class java.lang.Object and the default implementation is inherited by all the instances of all the classes. Important thing to understand with the default implementation of equals() is, it compares only the references and not the state of the object. Lets take an example.
public class Person
private int age;
private String name;

public Person(String n, int a)
age = a;
name = n;
Now you have following instances of Persons
Person p1 = new Person("Rahul", 23);
Person p2 = new Person("Rahul", 23);

p1.equals(p2) should return true, right ? But if you use the default implementation of equals() given by Object, the result will be false. This happens because Object's implementation of equals() compares only references, and as a result an object is always equal to "itself". Since p1 and p2 are references to different objects the equals would return false.

Now, lets add the equals method to the class Person as following:
public boolean equals(Object p)
if (p == null)
return false;
// This is called principal of non-nullity. Essentially a comparison
// with a null reference should return false.
if ( ! (p instanceof Person))
// if p is not a Person the comparison is false.
return false;
if (name.equals( && (age == p.age))
return true;
return false;
Now p1.equals(p2) would return true because we are not comparing references but the "state" of the instance also. It is very important to override equals() for classes whose instances would be used as keys in the hash tables, hash maps etc for exactly the reasons mentioned above.

Question: If we override the doGet() and doPost() method of a Servlet class, which one is called first, by the container?
Answer: Lets understand the basic concept of a servlet here. In very simple words, a servlet is a way to extend the server. doGet() and doPost() are methods specific to HttpServlets and are not called by the servlet container directly.
The method which the servlet container calls on the servlet is the service() method. It is the service() method in the HttpServlet which calls doGet() or doPost() based on the type of the incoming HTTP request. If the request is a GET request, doGet() would be called and on the other hand if it is a POST request, doPost() would be called.
Some clarification: Lets understand the basic HTTP concepts over here. When the browser requests a resource from the server it will send a HTTP GET request. Which litterally stands for "GET me xyz resource". So when you type a URL like: The translation done by the browser is "GET index.html" and the server sends off the file index.html to the browser.

POST is a method by which the browser wants to post or send some data to the server.

So when you directly call a servlet using URL like: the browser will use a GET request and eventually (after service() call from container) doGet() would be called.

I hope these clear your doubts, Rahul.
Technorati Tags:
Blogs linking to this article


Post a Comment

Links to this post:

Create a Link

<< Home