0

so I'm doing some generics stuff and I'm not sure why this issue occurs, hope someone could explain this to me. I'm hazarding a guess and thinking that some precision (for doubles, float) is lost, but I can't be sure.

SomeX<Double> a = new SomeX<>(4.4);
SomeX<Double> b = new SomeX<>(4.4);

a.getValue() == b.getValue(); // returns false
a.getValue().equals(b.getValue()); // returns true


SomeX<Integer> x = new SomeX<>(4);
SomeX<Integer> y = new SomeX<>(4);

x.getValue() == y.getValue(); // returns true
x.getValue().equals(y.getValue()); // also returns true

As reference, this is the code for my equals:

  @Override
  // Type conversion of all variables in this method is safe
  // because originally the values are all initially SomeX of type T.
  @SuppressWarnings("unchecked")
  public boolean equals(Object obj) {
    // if obj is a SomeX, then we'll need to check
    if (obj instanceof SomeX) {
      SomeX<T> objCast = (SomeX<T>) obj;
      // if obj is wrapped up in another SomeX
      if (this.getValue() instanceof SomeX && objCast.getValue() instanceof SomeX) {
        // then we simply unwrap it
        SomeX<T> temp1 = (SomeX<T>) this.getValue();
        SomeX<T> temp2 = (SomeX<T>) objCast.getValue();
        while (temp1.getValue() instanceof SomeX && temp2.getValue() instanceof SomeX) {
          temp1 = (SomeX<T>) temp1.getValue();
          temp2 = (SomeX<T>) temp2.getValue();
        }
        return temp1.getValue() == temp2.getValue();
      } else {
        // otherwise, we compare the contents of each SomeX.
        if (this.getValue() == null || objCast.getValue() == null) {
          // to handle null case because equals() cannot be used
          return this.getValue() == objCast.getValue();
        } else {
          // to handle all other instances
          T temp3 = (T) this.getValue();
          T temp4 = (T) objCast.getValue();
          return temp3.equals(temp4);
        }
      }
    } else {
      // if object is not a SomeX, then return false, no need to check
      return false;
    }
  }

bakano
  • 11
  • 1
  • 1
    your code contains a lot of weird stuff. are you actually checking whether the 'this' is an instance of the current class?? Do you understand how pointless that is? – Stultuske Feb 18 '21 at 13:02
  • == is a referential comparison, the equals method is to compare the value. That's the difference – Stultuske Feb 18 '21 at 13:02
  • @Stultuske hi, I'm exactly sure I understood what you're saying -- is using "this" wrong? I'm very new in Java, and I'm being taught to always use "this" when referring to the current instance of the class. If you're referring to why I'm using instanceof, it's because this.getValue() could be wrapped in SomeX. Could you elaborate more on why it is pointless? I'm actually really clueless on why it is pointless. On the second part.. yes, I do understand the differences between the two, but it doesn't really explain why SomeX would return true for both "==" and equals() method.. – bakano Feb 18 '21 at 13:04
  • Integer values in the range -128 to 127 are always auto-boxed to the same java.lang.Integer instance, so you "can" compare them using ==, but it's a bad idea. There is no such instance cache for autoboxing of java.lang.Double. – Erwin Bolwidt Feb 18 '21 at 13:07
  • @ErwinBolwidt I see... thank you! So in view of this, you should only ever use "==" when you're comparing primitives and null? – bakano Feb 18 '21 at 13:12
  • @bakano primitives, enums and maybe null, but there are other solutions for that. this points to the current instance, that is correct, but in class a validating that this.instanceof(thisClass) is nonsense, since it can't possibly be not an instance of thisClass – Stultuske Feb 18 '21 at 13:14
  • @bakano also: sometimes == returns true for both value and reference, if the instances point to the same object in memory – Stultuske Feb 18 '21 at 13:16
  • @Stultuske I don't see any `this instanceof ...` - what are you referring to? – Erwin Bolwidt Feb 18 '21 at 13:21
  • @ErwinBolwidt I mean the this.getValue() instanceof -> this to me means this entire equals method is either bad designed or in the wrong class. that should rather call a this.getValue().equals(o.getValue()) or similar, but not all in there – Stultuske Feb 18 '21 at 13:34
  • @Stultuske hm, but I can't do this.getValue().equals(objCast.getValue()) simply because it would never return a true, since they're two different instances of an object. What I'm trying to achieve with the equals() method (remember I'm overriding it), is to check: 1) IF they are the same object 2) and if they have the same value It is not a given that this.getValue() always returns a Class or a value (String, Integer, etc), it could always return either, hence the need to check. – bakano Feb 18 '21 at 13:45
  • Anyway! Thank you both for the insight -- I'm really new to Programming in general and just started on Java as part of my University requirements, so any insight is good insight for me. I appreciate both of your help =) – bakano Feb 18 '21 at 13:51
  • @bakano but I can't do this.getValue().equals(objCast.getValue()) simply because it would never return a true, since they're two different instances of an object. .... ok, you lost me here. It only makes sense to call this if they're two different instances of an object. If it's the same instance, you know they're equal before testing – Stultuske Feb 18 '21 at 14:16

0 Answers0