There is need to compare two objects based on class they implement? When to compare using getClass()
and when getClass().getName()
?
Is there any differ
Use class.equals()
:
if (nextMonster.getClass().equals(monster.getClass()))
or, because each class is like a singleton - there's only one instance of each Class per class loader, and most JVMs only have the one class loader - you can even use an identity comparison:
if (nextMonster.getClass() == monster.getClass())