Versions 2.X
JavaFX is obiously partly open source (Stack Overflow reference). From that link I found the source code to the equals()
method in the Color
class for versions 2.X:
/**
* Indicates whether some other object is "equal to" this one.
* @param obj the reference object with which to compare.
* @return {@code true} if this object is equal to the {@code obj} argument; {@code false} otherwise.
*/
@Override public boolean equals(Object obj) {
if (obj == this) return true;
if (obj instanceof Color) {
Color other = (Color) obj;
return red == other.red
&& green == other.green
&& blue == other.blue
&& opacity == other.opacity;
} else return false;
}
Obviously, red
, green
, blue
, and opacity
needs to be the same.
Versions 1.X
For versions 1.X i just looked at the compiled class file and feel confident enough to say that the implementation is the same as for 2.X (snippet below):
@com.sun.javafx.runtime.annotation.Public
public boolean equals(java.lang.Object arg0);
4 invokestatic javafx.lang.Builtins.isSameObject(java.lang.Object, java.lang.Object) : boolean [87]
15 instanceof javafx.scene.paint.Color [80]
18 ifeq 121
22 checkcast javafx.scene.paint.Color [80]
28 invokevirtual javafx.scene.paint.Color.get$red() : float [45]
38 invokevirtual javafx.scene.paint.Color.get$red() : float [45]
50 invokevirtual javafx.scene.paint.Color.get$green() : float [47]
60 invokevirtual javafx.scene.paint.Color.get$green() : float [47]
72 invokevirtual javafx.scene.paint.Color.get$blue() : float [48]
82 invokevirtual javafx.scene.paint.Color.get$blue() : float [48]
94 invokevirtual javafx.scene.paint.Color.get$opacity() : float [49]
104 invokevirtual javafx.scene.paint.Color.get$opacity() : float [49]
The equals()
implementation has not changed from 1.X to 2.X.
Your real problem
If Color1
and Color2
indeed are of type Color
, you are comparing them with objects of type String
:
if(Color1.equals("yellow")) && (Color2.equals("yellow"))
The comparison will fail here:
if (obj instanceof Color)
Hence, the equals()
method will always return false. You should use equals()
with another object of type Color
.