Is there an equivalent of '==' from Java i

2019-03-04 08:09发布

问题:

I am working in JSF 2 with Primefaces 3.4 and I found an example where '==' in my xhtml does not behave like '==' in Java. I could not find details for '==' operator in Java EE 6 documentation. What does it exactly do? Is there an equivalent of Java '==' for Objects in EL?

回答1:

Is there an equivalent of Java '==' for Objects in EL?

Looks like it is not, but you don't really need it. EL == (and eq) will use the equals method when comparing object references, and it already supports null comparison. If your class happens to not override equals, then it will use Object#equals that ends using Java == for equality check.

If your class happens to override equals method, make sure to write a good implementation. As example:

public boolean equals(Object o) {
    if (o == null) {
        return false;
    }
    if (this == o) {
        return true;
    }
    if (...) {
        //add here the rest of the equals implementation...
    }
    return false;
}

More info:

  • Java EE tutorial: Expression Language: Operators