Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

immutable object ????

 
Mona Gadkari
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"varijasmom"-
Welcome to the JavaRanch! Couple things for ya....
1st thing:
Please adjust your displayed name to meet the JavaRanch Naming Policy. Basically it should be both a first AND last name (seperated by a space of course). You can change it here. Thanks!!
2nd thing:
Also, when you're posting code... try surrounding them in [CODE] tags. It helps to preserve the whitespace and makes it easier for us to help you out.
Thanks! And again, welcome to the JavaRanch.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object of type A seems to be immutable since there's no way to change the int array object passed to the constructor after the object has been constructed. All the instance variables used for calculation hashCode() are private as well.

If your class is immutable (the instance data cannot be modified once initialized), you can base the hash code on the data field values. You should even calculate the hashCode() just once for an instance (after all, no data is going to change after the object has been instantiated - the class is immutable) and store it in a private instance variable. Next time onwards, the hashCode() method can just return the private variable's value, making the implementation very fast.

Read this thread about immutable objects and their equals() and hashCode() implications:
Implementing equals() and hashCode()
[ Jess modified the CODE tags to QUOTE tags so that it all wouldn't be on one giant line ]
[ November 18, 2003: Message edited by: Jessica Sant ]
 
mrudul joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnt understand why the output came like that.I tried to run the program and got the same output, but how does program control flow?Why is the clone object created ?
will you please explain/Thank you very much
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
clone makes a copy of the argument, now the object passed to the constructor can be modified (by code out of the constructor) without interferring with the copy passed. Thus the hashcode will be always the same, and it needs be calculated only once.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic