0

I write a little script to get pixel of an image and put it in an ArrayList, and i create a class who contains these values.

Here a parts of my code:

int arrC[] = {255, 0, 0};
Color   red = new Color(arrC),
        red2 = new Color(arrC);
if(!red.equals(red2)) System.out.print("It's not the same color !");

And the class Color:

class Color {

    private int RED;
    private int GREEN;
    private int BLUE;
    private String HEXA;

    public Color(int RGBColors[]) throws ColorException {
        if(RGBColors.length == 3) {
            for(int rgbcolor : RGBColors) {
                HEXA = String.format("#%02x%02x%02x", RGBColors[0], RGBColors[1], RGBColors[2]);
            }
        }else {
            throw new ColorException("Erreur : Number of the value incorrect. 3 excepted not: " + RGBColors.length);
        }
    }

    public Color(int hexacolor) {
        System.out.println(hexacolor);
    }

    /* Getter & Setters */

    public int getRED() {
        return this.RED;
    }

    //...

}

But i don't understand why variable red are not equals with the variable red2 even if they have the same propreties. How can do that ?

GreenMatt
  • 18,244
  • 7
  • 53
  • 79
Lemarcque
  • 9
  • 4
  • 2
    Because you did not implement `equals`, so it compares the reference, which is different. – tobias_k Aug 15 '16 at 22:00
  • 1
    You need to implement the `Object.equals` method (and should do the same for `hashCode`, if you plan to use instances e.g. within `Map`s), were you compare all properties. – qqilihq Aug 15 '16 at 22:01
  • Related: http://stackoverflow.com/q/27581/3182664 (and thousands of other questions) – Marco13 Aug 15 '16 at 22:01
  • Where exactly in your constructor do you set the values of red, green or blue? Why are you iterating over the 3 elements of the `RGBColor` array and at each iteration take every three values to recompute and overwrite some `HEXA` value? Is there any good reason to violate basic naming guidelines making fields look like constants? Please correct trivial mistakes in your code, which have nothing to do with equality of instances first... and then let your IDE generate the `equals` and `hashCode` for you to address the actual problem you are asking about. – Oleg Sklyar Aug 15 '16 at 22:37
  • @OlegSklyar You're right. I didn't even know why i do that ! Thank you. – Lemarcque Aug 17 '16 at 23:49

2 Answers2

0

You need to override and implement equals() and subsequently hashcode() in your Color class.

Something like:

@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + BLUE;
        result = prime * result + GREEN;
        result = prime * result + ((HEXA == null) ? 0 : HEXA.hashCode());
        result = prime * result + RED;
        return result;
    }

@Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Color other = (Color) obj;
        if (BLUE != other.BLUE)
            return false;
        if (GREEN != other.GREEN)
            return false;
        if (HEXA == null) {
            if (other.HEXA != null)
                return false;
        } else if (!HEXA.equals(other.HEXA))
            return false;
        if (RED != other.RED)
            return false;
        return true;
    }
UserF40
  • 3,533
  • 2
  • 23
  • 34
0

The default equals() method on java.lang.Object compares memory addresses, which means that all objects are different from each other (only two references to the same object will return true).

So you need to override equals() and hashcode() method of the Color class for the code to work properly.

AmanSinghal
  • 2,404
  • 21
  • 22