-1

the targetPixValList is a list contains Double objects and it contains also similar values in successive position in the list, and when i tried to compare two Double values using Code_1, the cnt returns zero. and when i used code_2, the cnt returns value.

and the type of the list is

why "==" operator does not work with Double?

Code_1:

int cnt = 0;
    for (int i = 0; i < cs.targetPixValList.size()-1; i++) {
        if (cs.targetPixValList.get(i) == cs.targetPixValList.get(i+1))
            ++cnt;
    }

Code_2:

 int cnt = 0;
    for (int i = 0; i < cs.targetPixValList.size()-1; i++) {
        if (cs.targetPixValList.get(i).equals(cs.targetPixValList.get(i+1)))
            ++cnt;
    }
Amrmsmb
  • 1
  • 27
  • 104
  • 226

3 Answers3

7

Double is a class wrapper for primitive double. When comparing instances of Double (or object references) use equals method rather than ==.

Also, for your case, the comparison of Double by using equals could also give wrong results due to float point comparison (done behind the scenes). If you're working with sensitive floating points values, I recommend using BigDecimal instead of Double and use BigDecimal#compareTo rather than equals because BigDecimal#equals does not take into account the scale, while BigDecimal#compareTo does.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I think if we are doing just "comparision", then the `equals` method of Double should be sufficient. But yes, if arithmatic operations such Add, divide, etc. are being performed, then we would need to: 1. Convert Double to Big Decimal, and 2. Specify the "Rounding" mechanism to be used to handle overflow scenarios if any. – Mecon Apr 27 '15 at 15:48
  • @Mecon when using `BigDecimal`, it's better to use `BigDecimal#compareTo(anotherBigDecimal) == 0` rather than using `BigDecimal#equals` because `equals` does not take into account the scale, while `compare` does. This is one of those odd cases in the JDK where the `equals` and `compare` methods are not consistent. – Luiggi Mendoza Apr 27 '15 at 15:51
1

You use .equals(otherObject) when comparing objects. You're comparing Double which is an object. If you were using double instead, a primitive, you could use == to compare values.

Alternatively, get the double value from Double object:

if (yourDoubleObject.doubleValue() == otherDoubleObject.doubleValue()) {
  // Do some things
}
Chackle
  • 2,249
  • 18
  • 34
0

== operator gives correct results in primitive types like int, long, double. If you use the operator with objects it will compare the references by default.

Using equals method may give the correct result dependingof the object. You should override equals method to receive logically check if the objects are equals.

For Double object, ,it also checks the referance equivalence of the objects. You should use Double#doubleValue method to check value equivalence.

Here is the code;

 for (int i = 0; i < cs.targetPixValList.size()-1; i++) {
        if (cs.targetPixValList.get(i).doubleValue() == cs.targetPixValList.get(i+1).doubleValue())
            ++cnt;
    }

See also :

How to override equals and hashcode

Community
  • 1
  • 1
erencan
  • 3,725
  • 5
  • 32
  • 50