Java array reflection: isArray vs. instanceof
那个方法好呢?if(obj.getClass().isArray()) {}
and
if(obj instanceof Object[]) {}
In general, use the At the JVM level, the The reflective approach ( There are two special cases: null references and references to primitive arrays. A null reference will cause Applied to a primitive array, the |
Ifobj
is of typeint[]
say, then that will
have an arrayClass
but not be an instance ofObject[]
.
So what do you want to do withobj
. If you are going to
cast it, go withinstanceof
. If you are going to use
reflection, then use.getClass().isArray()
.
总结:
isArray的方位更加广泛。
instance Object[]不能确别int[]等数组。
所以,那个更好,要看自己的具体情况了