
Hello,
can any one please expain me.I have doubt in Object class. Why it is necessary to override hashCode method with equals method. Since without overriding the hasCode method I am getting the proper output but it is always advisable to override hashCode along with Equals.
package com.lara;
public class B { int i,j;
B() {
} B(int i,int j) { this.i=i; this.j=j; } public String toString() { return "i="+i +","+ "j=" +j; } public boolean equals(Object obj) { B b=(B)obj; boolean falg=(i==b.i&& j==b.j); return falg; }
} class Ma { public static void main(String[] args) { B b= new B(10,10); B b1= new B(10,10);
System.out.println(b.equals(b1));
System.out.println(b.toString());
System.out.println(b1.toString());
}
}
Regards: Akash
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.