3

Why does my hashCode method (btw. generated with Intellij IDEA IDE) return different values for the same object while running the unit test normally and debugging it. While debugging my assert passes and both compared objects are same but when I run the test it results in the false assertion. Here is the class with hashCode method:

public class IntegerMatrix {

 private static final int HASH_CODE_MULTIPLIER = 31;

 /**
  * Number of rows.
  */
 private int rows;

 /**
  * Number of columns.
  */
 private int columns;

 /**
  * The actual data.
  */
 private int[][] data;

 public IntegerMatrix() {
     this(0, 0);
 }

 /**
  * Initialization constructor.
  *
  * @param rows    Number of rows.
  * @param columns Number of columns.
  */
 public IntegerMatrix(int rows, int columns) {
    if (rows < 0) {
        throw new IllegalArgumentException("Number of rows must be non-negative");
    }
    if (columns < 0) {
        throw new IllegalArgumentException("Number of columns must be non-negative");
    }
    this.rows = rows;
    this.columns = columns;
    data = new int[rows][columns];
 }

 public IntegerMatrix(IntegerMatrix matrix) {
    this.rows = matrix.getRows();
    this.columns = matrix.getColumns();
    this.data = new int[matrix.getRows()][matrix.getColumns()];
 }

 ... some other methods

 @Override
 public int hashCode() {
    int result = rows;
    result = HASH_CODE_MULTIPLIER * result + columns;
    result = HASH_CODE_MULTIPLIER * result + Arrays.deepHashCode(data);
    return result;
 }
}

When I create two IntegerMatrix objects and then compare them with auto-generated equals method (see code below)

@Override
public boolean equals(final Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    IntegerMatrix matrix = (IntegerMatrix) o;
    return rows == matrix.rows && columns == matrix.columns && Arrays.deepEquals(data, matrix.data);
}

I get true in debug mode and false while just running the unit test. Also in debug mode IntegerMatrix#hashCode returns the same hash code for both matrices (which are actually equal to each other, I personnally controlled the row and columns count as well as data entries) and while running it normally and printing the hash codes to the console gives different ones. Why is it so? What am I missing?

UPDATE

I forgot to notice that mvn test also runs successfully.

Arthur Eirich
  • 3,368
  • 9
  • 31
  • 63
  • Do you happen to know at which point it's returning false? Or true, for that matter? – Iluvatar Dec 05 '16 at 09:12
  • Possible duplicate of http://stackoverflow.com/questions/21762754/unique-java-objects-different-behaviour-in-run-and-debug – Raghuveer Dec 05 '16 at 09:12
  • @Iluvatar I have a matrix which is being modified in method under test and a second matrix with preset values. Comparing them in run mode in the unit test gives me `false`, though both matrices are actually equal, while comparing them in debug mode gives `true` when I step over the line with `equals` method. Hope it is understandable – Arthur Eirich Dec 05 '16 at 09:16
  • It's hard to say exactly then. I still think it would be valuable to know whether `equals()` is returning in the initial `if` statements or the final line. You can also try printing out the value of each of the comparisons you do to see if they match up with what you expect. – Iluvatar Dec 05 '16 at 09:24
  • Can you provide more info like is it a multi-threaded ENV ? – Raghuveer Dec 05 '16 at 09:27
  • @Iluvatar I did print the values of the comparison. The thing is debug mode says they are equal which is fine while run mode prints false. – Arthur Eirich Dec 05 '16 at 09:31
  • @Raghuveer It is not multithreaded. – Arthur Eirich Dec 05 '16 at 09:31
  • did you override the hashcode method as well.cuz that method will also be considered if your objects are in a map. – Priyamal Dec 05 '16 at 09:34
  • But which condition fails? Is it `rows == matrix.rows`, `columns == matrix.columns`, or `Arrays.deepEquals(data, matrix.data)`? Assuming it's that line that's returning, one of them must fail, and if none of them do then there are deeper things at work here. – Iluvatar Dec 05 '16 at 09:35
  • @Iluvatar `Arrays.deepEquals` condition prints `false` while running. Debugging works fine. I also tried Apaches `ArrayUtils.isEquals` - same result, running prints `false` while debugging gives `true`. – Arthur Eirich Dec 05 '16 at 10:03

0 Answers0