how many methods in object class in java?
There are many methods in Object Class.
Clone () – protected Object clone() throws CloneNotSupportedException
This method create exactly duplicate object is called cloning.
hashCode () – public native int hashCode()
In every object jvm will generate a unique number based on the address of an object. which is hashcode.jvm use this hashcode while inserting object to hashset,hashmap etc. we will be override the hahscode in the class at depend on requirement
toString () – public String to String()
we are use this method for string representation of object. In all wrapper class to String method is overridden.
Equals () – public boolean equals(Object o)
We will be use this method to check equality of two object. this method is always executed with address comparision or reference comparision.we can override equals method for content comparision.
Notify () – Wakeup a single thread that is waiting on this Object monitor.
Wait () – cause current thread to wait, until some other thread invoke or notify.
Finalize () – is invoked by garbage collector that there is no reference for that object.
public class Test {
private String name;
public Test(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//hashCode method
public int hashCode() {
return name.length();
}
// equals method
public boolean equals(Object obj) {
Test t = (Test) obj;
if (t.getName().equals(this.getName())) {
return true;
} else {
return false;
}
}
//toString method
public String toString() {
return "Parent [name=" + name + "]";
}
//finalize method
protected void finalize() throws Throwable {
super.finalize();
}
//clone method
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
how many methods in object class in java? Read More »