I have the problem that a given object could be one of several different objects (like files in a directory, that could be sound, image, text, ...) Is there the possibility to find out the type of the object beside from the 'instanceof' operator? Couse that would result in an endless if - then row ...
The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .
We can use the isInstance() method to check the class of an object at runtime. Furthermore, isInstance() handles autoboxing as well. When we make use of the isInstance() method, we protect our program from attempting illegal downcasts, although, using the instanceof operator will be smoother in this case.
The Java Object getClass() method returns the class name of the object. The syntax of the getClass() method is: object.getClass()
Java does not have typeof but rather has the getClass(), instanceof and java. lang. Class. isInstance().
foo.getClass()
gets the java.lang.Class
object describing the class of the object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With