• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

whizlabs mock: final varables in equal and hashcode?

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[code]
public class TestHash {
private int length;
private int width;
private final double area;

public TestHash (int l, int w)
{
length = l;
width = w;
area = length * width;
}
}
[code]

from this example, I learnt a final variable and a variable changeble by two variables should be involved in hashcode manipulation, what about for equals?

it makes sense for me to use it for equals, for example,
final integer for area, of shapes, can be used to compare so is one of the methods to find out if they are equal objects.
am i right?! :roll:
[ November 13, 2007: Message edited by: adam Lui ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Adam,

as long as the hash code contract is fulfilled, you can use the area variable for equals and for hashCode.

But this is more a question of what you want:
If a 3*7 and a 7*3 rectangle should be treated as equal, because the area is the same, you could use the area variable for equals and hashCode.
If you're looking at them as being not equal because the first looks like

and the second looks like


then you could use area in hashCode but not in equals.
Only the hashCodes must be the same if equals returns true.

The fact that it is final is not important here. By the way, the other variables are also kind of final, because there is no method to change them after they get their values (once and for all) in the constructor.

Hope I got your question right,
Bu.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic