Java 数组反射:isArray vs. instanceof
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/219881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java array reflection: isArray vs. instanceof
提问by David Citron
Is there a preference or behavior difference between using:
使用以下内容之间是否存在偏好或行为差异:
if(obj.getClass().isArray()) {}
and
和
if(obj instanceof Object[]) {}
?
?
采纳答案by erickson
In most cases, you should use the instanceof
operator to test whether an object is an array.
大多数情况下,您应该使用instanceof
运算符来测试对象是否为数组。
Generally, you test an object's type before downcasting to a particular type which is known at compile time. For example, perhaps you wrote some code that can work with a Integer[]
or an int[]
. You'd want to guard your casts with instanceof
:
通常,您在向下转换为编译时已知的特定类型之前测试对象的类型。例如,也许您编写了一些可以与 aInteger[]
或int[]
. 你想保护你的演员阵容instanceof
:
if (obj instanceof Integer[]) {
Integer[] array = (Integer[]) obj;
/* Use the boxed array */
} else if (obj instanceof int[]) {
int[] array = (int[]) obj;
/* Use the primitive array */
} else ...
At the JVM level, the instanceof
operator translates to a specific "instanceof"byte code, which is optimized in most JVM implementations.
在 JVM 级别,instanceof
操作符转换为特定的“instanceof”字节码,在大多数 JVM 实现中进行了优化。
In rarer cases, you might be using reflection to traverse an object graph of unknown types. In cases like this, the isArray()
method can be helpful because you don't know the component type at compile time; you might, for example, be implementing some sort of serialization mechanism and be able to pass each component of the array to the same serialization method, regardless of type.
在极少数情况下,您可能会使用反射来遍历未知类型的对象图。在这种情况下,该isArray()
方法会很有帮助,因为您在编译时不知道组件类型;例如,您可能正在实现某种序列化机制,并且能够将数组的每个组件传递给相同的序列化方法,而不管类型如何。
There are two special cases: null references and references to primitive arrays.
有两种特殊情况:空引用和对原始数组的引用。
A null reference will cause instanceof
to result false
, while the isArray
throws a NullPointerException
.
空引用将导致instanceof
结果false
,而isArray
抛出一个NullPointerException
.
Applied to a primitive array, the instanceof
yields false
unless the component type on the right-hand operand exactly matches the component type. In contrast, isArray()
will return true
for any component type.
应用于原始数组,除非右侧操作数上的组件类型与组件类型完全匹配,否则将instanceof
产生收益false
。相反,isArray()
将为true
任何组件类型返回。
回答by Burkhard
In the latter case, if obj is null you won't get a NullPointerException but a false.
在后一种情况下,如果 obj 为 null,您将不会得到 NullPointerException 而是一个 false。
回答by hazzen
There is no difference in behavior that I can find between the two (other than the obvious null-case). As for which version to prefer, I would go with the second. It is the standard way of doing this in Java.
我发现两者之间的行为没有区别(明显的空情况除外)。至于更喜欢哪个版本,我会选择第二个。这是在 Java 中执行此操作的标准方法。
If it confuses readers of your code (because String[] instanceof Object[]
is true), you may want to use the first to be more explicit if code reviewers keep asking about it.
如果它让你的代码的读者感到困惑(因为这String[] instanceof Object[]
是真的),如果代码者不断询问它,你可能希望使用第一个更明确。
回答by Bill K
If you ever have a choice between a reflective solution and a non-reflective solution, never pick the reflective one (involving Class objects). It's not that it's "Wrong" or anything, but anything involving reflection is generally less obvious and less clear.
如果您在反射解决方案和非反射解决方案之间做出选择,请永远不要选择反射解决方案(涉及 Class 对象)。并不是说它是“错误的”或任何东西,而是任何涉及反思的东西通常都不那么明显和不那么清楚。
回答by Tom Hawtin - tackline
If obj
is of type int[]
say, then that will have an array Class
but not be an instance of Object[]
. So what do you want to do with obj
. If you are going to cast it, go with instanceof
. If you are going to use reflection, then use .getClass().isArray()
.
如果obj
是int[]
say类型,那么它将有一个数组Class
但不是Object[]
. 那么你想用obj
. 如果您要投射它,请使用instanceof
. 如果要使用反射,请使用.getClass().isArray()
.
回答by Sebastien Tardif
getClass().isArray()
is significantly slower on Sun Java 5 or 6 JRE than on IBM.
getClass().isArray()
Sun Java 5 或 6 JRE 上的速度明显慢于 IBM。
So much that using clazz.getName().charAt(0) == '['
is faster on Sun JVM.
clazz.getName().charAt(0) == '['
在 Sun JVM 上使用速度更快。
回答by Trenton D. Adams
Java array reflection is for cases where you don't have an instance of the Class available to do "instanceof" on. For example, if you're writing some sort of injection framework, that injects values into a new instance of a class, such as JPA does, then you need to use the isArray() functionality.
Java 数组反射适用于您没有可用于执行“instanceof”的类实例的情况。例如,如果您正在编写某种注入框架,将值注入到类的新实例中,例如 JPA,那么您需要使用 isArray() 功能。
I blogged about this earlier in December. http://blog.adamsbros.org/2010/12/08/java-array-reflection/
我在 12 月初写了一篇关于这个的博客。 http://blog.adamsbros.org/2010/12/08/java-array-reflection/
回答by dturanski
I recently ran into an issue upgrading a Groovy application from JDK 5 to JDK 6. Using isArray()
failed in JDK6:
我最近遇到了将 Groovy 应用程序从 JDK 5 升级到 JDK 6 的问题。isArray()
在 JDK6 中使用失败:
MissingMethodException:
No signature of sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl.isArray() ...
Changing to instanceof Object[]
fixed this.
更改以instanceof Object[]
解决此问题。