What is the easiest way to do “yes” in Java?

Many languages have tools to check whether an object belongs to a certain type (including a parent class), which are implemented with 'is' and used as follows:

if(obj is MyType)

Or a little cumbersome, you can use the 'as' keyword in other languages to check for soft type conversion and see if the result is null

I haven't used java for years and I'm recovering it quickly, but can java do it easily without going into the reflection API?

Thank you in advance for your reply I've searched here and elsewhere, but the keywords involved are very common. Even if I'm sure there's a simple answer, it's difficult for Google to search it

Solution

if (objectReference instanceof type){
if (objectReference instanceof type){
    //Your code goes here
}

More information here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>