• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

immutable object ????

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic