0

I am making a method .equals replacing the equals method used. It accepts a object. I want it to check if that object equals the class that runs the .equals class.

I know I want to compare all the private methods I have to that object.

Is there a way to do this without making another private class to get the private variables from the object? How do I do this to compare equality not identity? I am stuck on this. Do i have to use == to compare?

Also looking online i see others use recursion. If this is the way i have to do it can you show and explain it to me?

so an example i have

public boolean equals(Object o)
{

this is in a class we will call bobtheBuilder (first thing to pop in my head) I want to check if the object o is equal to the class. bobTheBuilder has private object array and a private int. I assume I want to check if the array and int of this class equal the array and int of the object. First I know i need to check if it equals null so

private _arrayObject[];

private _integerBob;

public boolean equals(Object o)
{
    boolean result = true;

    if (o == null)
        result = false

    bobTheBuilder compared = (bobTheBuilder) o;

    if(compared.getArray != _arrayObject[] || compared.getInt != _integerBob)
        result == false

    return result
}

I know that equal checks for equality not identity so I assume this is wrong. How would I do this

Community
  • 1
  • 1
user1670252
  • 47
  • 1
  • 1
  • 8
  • 2
    I think your description would make sense with some sample code. – Bhesh Gurung Oct 18 '12 at 22:51
  • Objects aren't equal to classes. Are you comparing two classes, or two objects? – user207421 Oct 18 '12 at 23:05
  • im sending in a object of bobTheBuilder into bobTheBuilder – user1670252 Oct 18 '12 at 23:11
  • There is no "right" answer to this. You have to decide what "equality" means for your object. Comparing the array fields for identity is as valid a choice as doing an element-per-element comparison. Now, if you want to ask /how/ to do a deep array comparison, ask that directly instead of going on a tangent. – millimoose Oct 18 '12 at 23:22
  • Also, please clean up your code samples, that's not even remotely valid Java. – millimoose Oct 18 '12 at 23:22

3 Answers3

1

So, you have a private array X and an int Y in a class Z.

Now, for checking the equality and not indentity,

  1. First thing you should have setters and getters that will set and return the value of the private variables, e.g.

public void setArrayX(Object[] arr), public Object[] getArrayX()

2.now, the eqauls method would look like,

boolean equals(Object o) {
  if(Arrays.equals(this.getArrayX(),o.getArrayX()) && this.getIntY() == o.getIntY())
   return true;
  return false;
}
Arham
  • 2,072
  • 2
  • 18
  • 30
  • remember im replacing .equals so will that do the same thing. isn't == comparing identity not equality – user1670252 Oct 18 '12 at 23:10
  • @arham It's usually okay to access private fields directly in an `equals()` implementation - it's not like there's much sense in encapsulating fields from the class containing them. (Of course, this is unless your getters have side effects you wish to be invoked but that's probably rarely the case with `equals()` as well.) – millimoose Oct 18 '12 at 23:19
  • @user1670252 I have edited the code. In your specific case of an int and an array comparison. For int you'll have to use == and for an array which is a representation of Object[] you'll have to use Arrays.equals to compare the content of both the arrays. – Arham Oct 18 '12 at 23:26
  • @millimoose how do you access private fields of an object instance in the equals method without some method returning them? – Arham Oct 18 '12 at 23:27
  • @Arham You can access private fields of another object that is of the same class. – millimoose Oct 18 '12 at 23:29
  • so i create another equals class to compare arrays – user1670252 Oct 18 '12 at 23:30
  • @user1670252 Have you checked my edited code? Doesn't the Arrays.equals() method help in comparing the contents? As millimoose said you need to set your expectation clear as to what exactly do you you want. – Arham Oct 18 '12 at 23:33
  • @user1670252 You can use `deepEquals()` in [`Arrays`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html) – millimoose Oct 18 '12 at 23:36
1

Adapted from the examples in Effective Java, 2nd edition by Joshua Bloch:

public boolean equals(Object o) {
  if (o == this) {
    return true;
  }

  if (!(o instanceof BobTheBuilder)) {
    return false:
  }

  BobTheBuilder bob = (BobTheBuilder) o; //guaranteed to succeed

  return Arrays.equals(bob._arrayObject, _arrayObject)
    && bob._integerBob == _integerBob;
}

Don't forget to override hashCode as well.

The text in the book refers to the contract set out for the equals method in Object which is here

barrowc
  • 10,444
  • 1
  • 40
  • 53
  • 1
    This implementation is formally incorrect for non-final classes: http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html. Basically, it allows subclasses to make equality non-transitive. The article also goes on to say how to do it Really correctly, and [Part 2](http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals-2.html) deals with how to correctly allow mixed-type comparisons.) – millimoose Oct 18 '12 at 23:49
  • Unsurprisingly there have been some previous SO discussions on the correct implementation of equals - e.g. http://stackoverflow.com/q/596462/53614 – barrowc Oct 19 '12 at 00:17
  • 1
    Oh, I know there's practical reasons to violate the contract on transitivity - there's not much use for it. But in nonfinal classes (i.e. what most people write when they're not sweating every design detail), Bloch's implementation can also be non-reflexive, which sounds pretty likely to cause bugs - you'd have to consistently put the parent class on the left side of comparisons to not always get `false`. `if (this.getType() != o.getType()) return false;` is a simple fix for that, and marginally less restrictive than only implementing `equals()` for final classes. – millimoose Oct 19 '12 at 01:04
  • 1
    Basically, you have to pick which bothers you less: losing mixed-type comparisons, or having to mark any `equals()` override as `final` to avoid bugs. – millimoose Oct 19 '12 at 01:15
  • @barrowc: I don't see any fundamental "problem" in allowing subclasses to add additional value components, if the parent class defines equality in terms of some virtual members. Basically, formulate a "canonical" representation for every subclass, and then specify that two objects are equal if they would share the same canonical representation. The canonical representation could be defined in such a way that any subtypes with certain additional fields would compare unequal to any other object which lacked them. – supercat Oct 13 '13 at 17:27
  • @barrowc: I don't know of any particular "go-to" pattern which will handle all such class families well; the required pattern will vary depending upon how much information is encapsulated, how many subclasses there are, etc. In general, it's probably good to have a few virtual methods to check whether things can be quickly determined to be the same or different, and if not examine them in more detail. Not a trivial problem, but not impossible either. – supercat Oct 13 '13 at 17:33
0

I think the best way to check the class is through the

someObject.getClass().isInstance(anotherObject)

command.

This works with inheritance as well.