Skip to main content

Dustin Marx: The Subtle Nuance of the new Keyword with Reference Types in Java

Dustin Marx investigates The Subtle Nuance of the new Keyword with Reference Types in Java:

One of the trickier aspects of "general Java" development is related to comparing Java reference types for equality.  Fortunately, most of us learn early in our Java development experience that we can generally use the reference types' overridden versions of Object.equals to safely check the content of the objects, which is almost always what we want.  Object identity equality comparison with == is not what we want as frequently, but it can sometimes be mistakenly added to Java code and not discovered immediately because often even == between two seemingly different reference type objects can evaluate to true.  This is demonstrated in this blog post. The following simple class demonstrates how == can appear to behave erratically...

Community: General