Both versions are basically equal since java.lang.Class
uses the standard implementation inside Object
:
public boolean equals(Object obj) {
return (this == obj);
}
And since Class
is final you can't override that.
Note, however, that once you're working with different classloaders you might get two classes that are basically the same but different instances, i.e. java.lang.String != java.lang.String
if both come from different classloaders (that particular example probably won't happen, it's just an illustration).
As a final note, you might want to use equals()
anyways if the instance you're calling it on can never be null
(and String.class
won't be null). That way you're reinforcing the habit to use equals()
over ==
and in the extremely unlikely case that Class
will change its implementation of equals()
at some point it's probably safer to use it right now.