相当于instanceof

Som*_*ing 1 java if-statement instanceof

不使用instanceof 相当于什么?使用 true、false 或 else 语句时也许更简单?

public static void p (Object...ar)
{
        for (int i=0; i < ar.length; i++)
        {
                if (ar[i] instanceof int[])
                {
Run Code Online (Sandbox Code Playgroud)

acd*_*ior 5

对于原始数组,您可以代替:

if (ar[i] instanceof int[])
Run Code Online (Sandbox Code Playgroud)

使用:

if (ar[i].getClass() == int[].class)
Run Code Online (Sandbox Code Playgroud)



注意: 上面的代码适用于基本类型的数组。但请注意,这instanceof还会检查左侧部分中的对象是否是右侧部分中类的子类型,而==不会。
对于对象,相当于(myInstance instanceof MyClass[])(检查子类型)是:

(   MyClass[].class.isAssignableFrom(  myInstance.getClass()  )   )
Run Code Online (Sandbox Code Playgroud)