JVM Languages

Tools

How do I know the class of an object?

For instance you have a collection of objects in an List object and you want to do some logic based on the object’s class. This can easily be done using the instanceof operator. The operator returns true if an object is an instance of a specified class, if not it will return false.

The instanceof operator is most likely used when implementing an equals(Object o) method of an object to check if the compared object is from the same class.

I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you ?