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 ?
equals, so it compares the reference, which is different.Object.equalsmethod (and should do the same forhashCode, if you plan to use instances e.g. withinMaps), were you compare all properties.RGBColorarray and at each iteration take every three values to recompute and overwrite someHEXAvalue? 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 theequalsandhashCodefor you to address the actual problem you are asking about.