java - .equals() or == when comparing the class of an object -
in java, when testing object's class equal use .equals() or ==
for example in code below be:
if(object.getclass() == myclass.class)
or
if(object.getclass().equals(myclass.class))
you can use instanceof kind of comparision too, anyway, class implementation don´t override equals comparing using == correct, check runtime class, f.e: code shows true:
public static void main(string[] args) { object object = ""; if (object.getclass() == string.class) { system.out.println("true"); } }
the general rule comparing objects use 'equals', comparing classes if don´t want use instanceof, think correct way use ==.
Comments
Post a Comment