1

This is probably very easy, but I can't figure out what I'm doing wrong. I'm doing a java file for a junit test already provided for me, as follows

public void testEquals() {
    LogicVariable a = new LogicVariable("Y1");
    LogicVariable b = new LogicVariable("Y1");
    assertTrue(a.equals(b)); // same name
    assertTrue(a.equals((Object)b));
}   

I got to the point where I have this for my test (have in mind that this code is not for this test only, so I have two constructor with the same name)

public class LogicVariable {

protected String name;
protected boolean value ;

public LogicVariable(String name) {
    this.name = name;
}

public LogicVariable(String name, boolean value) {
    this.name = name;
    this.value = value;
}   

As I understand, having more than one constructor with the same name, but different inputs, it automatically chooses the suited one for the arguments given (in this case, skiping the one with the boolean). Am I wrong in thinkin that? I can't figure why are a and b different, given the fact that they're both objects with only their name.

Appreciate your help

  • 2
    If you do not override equals, it just compares references. – J Fabian Meier Oct 22 '19 at 17:38
  • 1
    That post answers a little more than is being asked here, but does answer this: "this method returns true if and only if x and y refer to the same object (x == y has the value true)". – Carcigenicate Oct 22 '19 at 17:38
  • 1
    Also see this [post](https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java), it has some useful information. – Nexevis Oct 22 '19 at 17:38

0 Answers0