New to Java and the error "int cannot be dereferenced"

I am new to java and have been working on this exercise for a while, but keep getting the error: int cannot be dereferenced. I saw a couple of similar questions, but still can not understand my own case. Here are the full codes:

package inclass;

class OneInt {
  int n;

  OneInt(int n) {
    this.n = n;
  }

  @Override public boolean equals(Object that) {
    if (that instanceof OneInt) {
        OneInt thatInt = (OneInt) that;
        return n.equals(thatInt.n); // error happens here
    } else {
        return false;
    }
  }

  public static void main(String[] args) {
    Object c = new OneInt(9);
    Object c2 = new OneInt(9);
    System.out.println(c.equals(c2));
    System.out.println(c.equals("doesn't work"));
  } 
}

Thanks for helping me with this little nuisance.

+5
source share
2 answers

equals- class method. intis primitive, not a class. Just use ==instead:

return n == thatInt.n;
+7
source

To compare ints, just use the operator ==:

if (n == thatInt.n)

, int , . int.

+4

All Articles