On Wed, Jun 3, 2009 at 1:16 PM, jtmelton <jtmelton_at_gmail.com> wrote:
> I wanted to ask why JAXB compares strings with the equals operator (==)
> instead of the equals method. My understanding is that the equals
I don't know for sure but I suspect it is because it can be done, and
if so, is more efficient than equals() comparison.
Specifically:
> only works when comparing the same object or 2 strings that have both been
> initialized to the same value via the standard double quote initialization.
This is not the definition of when it works, although it's the most
common case. In general, identity comparison can be used if instances
have been canonicalized; for Strings most common method is to call
String.intern() (which is automatically done for String constants in
source code).
> In this case, it appears that an initialized string is being compared to a
> method parameter. Why would JAXB do it this way? As an example please look
> at the leaveElement method at this link.
If (and only if) method is known to have been canonicalized,
comparison is an efficient way to verify equality.
-+ Tatu +-