Java – is there any way to know the type of object?

The scheme is that I pass an object as a parameter in the method, and I want to perform the operation according to the type of the object in the method

The sample code is:

method(Object object){

//if object== String type print string   
 }

Solution

attempt

if (object.getClass().getName().equals("Class1Name"))
    //do something.

The advantage of getClass instead of instanceof is that you don't need to know the class type at compile time

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
分享
二维码
< <上一篇
下一篇>>