We have already discussed about the toString() method so now we will explain the remaining 10 methods of Object class in java.lang package.
2) hashCode():
Prototype of this method is
public native int hashCode()
In java, JVM assigns one unique id to every object through which we can represent it in the memory. This unique id is referred as hashcode in java language. JVM requires hashcode of an object so that it can save these objects into hash tables, hash set or hash map. If we want then we can override the hashCode() in order to generate our own hashcode. If we do not override hashCode() then Object class hashCode() is executed. Object class hashCode() generates the hashcode depending on the address of the object. But if we have override the method in our class then the generated hashcode will not be related to its address anymore. Overriding of hashCode() is recommended if we need to generate a unique number for every object.
Consider the following 2 cases in which we have override hashCode():
//Method 1 public class Employee1 { //code //overriding hashCode() public int hashCode() { //returning the same number for all object return 1000; } //some other code } //Method 2 public class Employee2 { //code //overriding hashCode() public int hashCode() { //returning an unique number for all object return "empid"; } //some other code }
To demonstrate the difference between hashCode() and toString() consider the following code:
public class Mall { //overriding toString() public String toString() { return "test"; } public static void main(String []args) { Mall m = new Mall(); String str = new String("Thomas"); Integer i = new Integer(100); System.out.println(m); System.out.println(str); System.out.println(i); } }
Output of this code which overrides toString() is:
test Thomas 100
Another Example
public class Mall { int id; //overriding hashCode() public int hashCode() { return id; } public static void main(String []args) { Mall m = new Mall(); String str = new String("Thomas"); Integer i = new Integer(100); System.out.println(m); System.out.println(str); System.out.println(i); } }
New output:
Mall@0 Thomas 100
Note- If we do not override toString() in our class then it internally call hashCode() method and execute it. But if we do override toString() in our class then the hashCode() method will not call and execute.
Consider a following example where we have override both toString() and hashCode() in a single class.
public class House { int roomno; House(int roomno) { this.roomno = roomno; } //overriding hashCode() public int hashCode() { return roomno; } //overriding toString() public String toString() { return roomno+"_"; } public static void main(String []args) { House h1 = new House(100); House h2 = new House(101); System.out.println(h1); System.out.println(h2); } }
Output:
100_ 101_
Note- In the above code, we call the toString() method, while printing the object.
3. clone()
Prototype of this method is
protected native Object clone() throws CloneNotSupportedException
Through this method we can create an exact duplicate object of any other object. Main motive behind creating this method is to maintain a backup of our data. Any object is clone able only if the corresponding class implements Cloneable interface. Cloneable is a marker interface that has no methods and is present in java.lang package. For example:
public class Showroom implements Cloneable { int i = 10; int j = 20; public static void main(String []args) throws CloneNotSupportedException { Showroom s1 = new Showroom(); Showroom s2 = (Showroom)s1.clone(); s2.i = 1001; s2.j = 1002; System.out.println(s1.i+"....."+s1.j); System.out.println("Comparing hashcodes of both objects"); System.out.println(s1.hashCode() == s2.hashCode()); System.out.println("Comparing both objects using =="); System.out.println(s1 == s2); } }
Output:
10.....20 Comparing hashcodes of both objects false Comparing both objects using == false
Deep Cloning & Shallow Cloning
Shallow cloning is the process of creating just duplicate reference variable but not a duplicate object. And, deep cloning is the process of creating exactly independent objects. For example:
Showroom s1 = new Showroom(); Showroom s2 = s1; //shallow cloning Showroom s3 = (Showroom)s1.clone(); //deep cloning