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

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -