Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In most cases, you should use the <code>instanceof</code> operator to test whether an object is an array.</p> <p>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 <code>Integer[]</code> or an <code>int[]</code>. You'd want to guard your casts with <code>instanceof</code>:</p> <pre><code>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 ... </code></pre> <p>At the JVM level, the <code>instanceof</code> operator translates to a specific <a href="http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc6.html#instanceof" rel="nofollow noreferrer">"instanceof"</a> byte code, which is optimized in most JVM implementations.</p> <p>In rarer cases, you might be using reflection to traverse an object graph of unknown types. In cases like this, the <code>isArray()</code> 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.</p> <p>There are two special cases: null references and references to primitive arrays.</p> <p>A null reference will cause <code>instanceof</code> to result <code>false</code>, while the <code>isArray</code> throws a <code>NullPointerException</code>.</p> <p>Applied to a primitive array, the <code>instanceof</code> yields <code>false</code> unless the component type on the right-hand operand exactly matches the component type. In contrast, <code>isArray()</code> will return <code>true</code> for any component type.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload