Monday, September 14, 2009

equals(), hashCode() and toString() methods in Object


//hashCode in Object class

public native int hashCode();

//Java doc for hashCode()

int java.lang.Object.hashCode()


Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

* Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
* If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
* It is not required that if two objects are unequal according to the java.lang.Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

//equals() method in Object class
public boolean equals(Object obj) {
return (this == obj);
}


package object;

public class User {

private String firstName;
private String lastName;
private Integer mark;
public User(String firstName,String lastName, Integer mark){
setFirstName(firstName);
setLastName(lastName);
setMark(mark);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getMark() {
return mark;
}
public Integer setMark(Integer mark) {
this.mark = mark;
}
}


package object;

public class ObjectTest {

public static void main(String[] args) {
User user1=new User("Vijayan", "Srinivasan", new Integer(100));
User user2=new User("Vijayan", "Srinivasan", new Integer(100));
System.out.println("[user1==user2]="+(user1==user2));
System.out.println("[user1.equals(user2)]="+user1.equals(user2));
}
}


So both case it is giving false becasue, both of the statements uses the same logic to compare 2 objects. For getting this right we have to do the following.

//Override equals() method in User class

public boolean equals(Object object) {
if(object instanceof User){
User user=(User) object;
return
equals(firstName,user.firstName) &&
equals(lastName,user.lastName) &&
equals(mark,user.mark);
}
return false;
}

// Implementation of toString() in Object class
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}


Although it is sufficient that overriding equals methods alone sufficient here it is adviced to override also for better performance. //Why?


// implementation of put() method in Hashtable class

public Object put(Object key, Object value) {
// Make sure the value is not null
if (value == null) throw new NullPointerException();

// Makes sure the key is not already in the hashtable.
HashtableEntry e;
HashtableEntry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;

for (e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) { //<------ Look here
Object old = e.value;
e.value = value;
return old;
}
}


Please note if we have proper hashCode() impelentation there is no need of calling the equals() method at all. Hence if the object is different we can let the Hashtable know ahead of calling equals() method. That is the reason it is always important to implement hashCode() when ever we override the equals()

Wednesday, September 09, 2009

Seting gnome on Linux VNC server

In redhat linux, when we start VNC server, by default it used to start with a simple termial based view. Inorder to start it as gnome based session the following command can be used

  1. Set the vnc session as gnome
    > vi ~/.vnc/xstartup (remove all the lines and add only the following lines)
    unset SESSION_MANAGER
    vncconfig -iconic &
    gnome-session &
  2. Start VNC server
  3. > vncserver - geometry 1024x768
Use the correct ip address and X-display id to connect to the VNC e.g. 10.66.92.127:1

Monday, September 07, 2009

Visio type tool on SAAS

Most of would have used Microsoft Visio for creating various diagrams such as Class, Network, Flowchart etc., Here is a tool that does everything online ( http://creately.com/ ). This tool has developed in Flex also allows us to save and collaborate among team members.