hello,
see this code:::
private int[] val;
private int hash;
public int hashCode() {
int h = hash;
if (h == 0) {
int len = val.length;
for (int i = 0; i < len; i++) {
h = 31*h + val[i];
}
hash = h;
}
return h;
}
// The equals method has been omitted for clarity.
A (int[] val) {this.val = (int[])val.clone();}
public static void main (
String[] args) {
A a = new A(new int[]{1,2,3});
System.out.print(a.hashCode());
}}
o/p::: 1026 i too got this which is correct ans indeed.But my que is why is this obj immutable.
As provided in the explaination:::
The hashCode is calculated using all three array elements. Since the object is immutable, the hashcode is only calculated the first time the hashCode method is invoked
pls explain.