What are the methods in java.lang.object?
>> Monday, May 30, 2011
The non-final methods are equals(), hashCode(), toString(), clone(), and finalize(). The other methods like
wait(), notify(), notifyAll(), getClass() etc are final methods and therefore cannot be overridden.
equals(): This method checks if some other object passed to it as an argument is equal the object in which this method is
invoked.
toString(): The toString() method provided by the java.lang.Object returns a string, which consists of the class name followed by an “@” sign and then unsigned hexadecimal representation of the hashcode, for example Pet@162b91. This hexadecimal representation is not what the users of your class want to see.
Example:
public class Pet {
int id;
String name;
public boolean equals(Object obj){
//as shown above.
}
public int hashCode() {
//as shown before
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(“id=”).append(id);
sb.append(“,name=”).append(name);
return sb.toString();
}
}
clone(): It can be used to create a bitwise of exact copy of original object.
The above four are non final methods.
wait(), notify(), notifyAll(), getClass() etc are final methods and therefore cannot be overridden.
equals(): This method checks if some other object passed to it as an argument is equal the object in which this method is
invoked.
public class Pet { int id; String name; public boolean equals(Object obj){ if(this == obj) return true; // if both are referring to the same object if ((obj == null) || (obj.getClass() != this.getClass())) { return false; } Pet rhs = (Pet) obj; return id == rhs.id && (name == rhs.name || (name != null && name.equals(rhs.name)) ); } //hashCode() method must be implemented here. … }
hashCode(): This method returns a hashCode() value as an Integer and is supported for the benefit of hashing based
java.util.Collection classes like Hashtable, HashMap, HashSet etc. If a class overrides the equals() method, it
must implement the hashCode() method as well. The general contract of the hashCode() method is that:
1. Whenever hashCode() method is invoked on the same object more than once during an execution of a Java
program, this method must consistently return the same integer result. The integer result need not remain
consistent from one execution of the program to the next execution of the same program.
2. If two objects are equal as per the equals() method, then calling the hashCode() method in each of the two
objects must return the same integer result. So, If a field is not used in equals(), then it must not be used in
hashCode() method.
3. If two objects are unequal as per the equals() method, each of the two objects can return either two different
integer results or same integer results (i.e. if 2 objects have the same hashCode() result does not mean that they
are equal, but if two objects are equal then they must return the same hashCode() result).
java.util.Collection classes like Hashtable, HashMap, HashSet etc. If a class overrides the equals() method, it
must implement the hashCode() method as well. The general contract of the hashCode() method is that:
1. Whenever hashCode() method is invoked on the same object more than once during an execution of a Java
program, this method must consistently return the same integer result. The integer result need not remain
consistent from one execution of the program to the next execution of the same program.
2. If two objects are equal as per the equals() method, then calling the hashCode() method in each of the two
objects must return the same integer result. So, If a field is not used in equals(), then it must not be used in
hashCode() method.
3. If two objects are unequal as per the equals() method, each of the two objects can return either two different
integer results or same integer results (i.e. if 2 objects have the same hashCode() result does not mean that they
are equal, but if two objects are equal then they must return the same hashCode() result).
Example:
public class Pet { int id; String name; public boolean equals(Object obj){ //as shown above. } //both fields id & name are used in equals(), so both fields must be used in //hashCode() as well. public int hashCode() { int hash = 9; hash = (31 * hash) + id; hash = (31 * hash) + (null == name ? 0 : name.hashCode()); return hash; } }
toString(): The toString() method provided by the java.lang.Object returns a string, which consists of the class name followed by an “@” sign and then unsigned hexadecimal representation of the hashcode, for example Pet@162b91. This hexadecimal representation is not what the users of your class want to see.
Example:
public class Pet {
int id;
String name;
public boolean equals(Object obj){
//as shown above.
}
public int hashCode() {
//as shown before
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(“id=”).append(id);
sb.append(“,name=”).append(name);
return sb.toString();
}
}
clone(): It can be used to create a bitwise of exact copy of original object.
The above four are non final methods.
0 comments:
Post a Comment