UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

48 lines (46 loc) 8.19 kB
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Arrays and the Class Hierarchy</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="Arrays and the Class Hierarchy"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-6-SECT-5"/>Arrays and the Class Hierarchy</h1></div></div></div><p><a id="idx10267" class="indexterm"/> <a id="idx10308" class="indexterm"/>Now we’re going to shift gears a bit and return to the topic of arrays, considering them from the object point of view. At the end of <a class="xref" href="ch04.html" title="Chapter 4. The Java Language">Chapter 4</a>, we mentioned that arrays have a place in the Java class hierarchy, but we didn’t give you any details. Now that we’ve discussed the object-oriented aspects of Java, we can give you the whole story.</p><p>Array classes live in a parallel Java class hierarchy under the <code class="literal">Object</code> class. If a class is a direct subclass of <code class="literal">Object</code>, an array class for that base type also exists as a direct subclass of <code class="literal">Object</code>. Arrays of more derived classes are subclasses of the corresponding array classes. For example, consider the following class types:</p><a id="I_6_tt327"/><pre class="programlisting"> <code class="kd">class</code> <code class="nc">Animal</code> <code class="o">{</code> <code class="o">...</code> <code class="o">}</code> <code class="kd">class</code> <code class="nc">Bird</code> <code class="kd">extends</code> <code class="n">Animal</code> <code class="o">{</code> <code class="o">...</code> <code class="o">}</code> <code class="kd">class</code> <code class="nc">Penguin</code> <code class="kd">extends</code> <code class="n">Bird</code> <code class="o">{</code> <code class="o">...</code> <code class="o">}</code></pre><p><a class="xref" href="ch06s05.html#learnjava3-CHP-6-FIG-8" title="Figure 6-8. Arrays in the Java class hierarchy">Figure 6-8</a> illustrates the class hierarchy for arrays of these classes. Arrays of the same dimension are related to one another in the same manner as their base type classes. In our example, <code class="literal">Bird</code> is a subclass of <code class="literal">Animal</code>, which means that the <code class="literal">Bird[]</code> type is a subtype of <code class="literal">Animal[]</code>. In the same way a <code class="literal">Bird</code> object can be used in place of an <code class="literal">Animal</code> object, a <code class="literal">Bird[]</code> array can be assigned to a variable of type <code class="literal">Animal[]</code>:</p><a id="I_6_tt328"/><pre class="programlisting"> <code class="n">Animal</code> <code class="o">[][]</code> <code class="n">animals</code><code class="o">;</code> <code class="n">Bird</code> <code class="o">[][]</code> <code class="n">birds</code> <code class="o">=</code> <code class="k">new</code> <code class="n">Bird</code> <code class="o">[</code><code class="mi">10</code><code class="o">][</code><code class="mi">10</code><code class="o">];</code> <code class="n">birds</code><code class="o">[</code><code class="mi">0</code><code class="o">][</code><code class="mi">0</code><code class="o">]</code> <code class="o">=</code> <code class="k">new</code> <code class="n">Bird</code><code class="o">();</code> <code class="c1">// make animals and birds reference the same array object</code> <code class="n">animals</code> <code class="o">=</code> <code class="n">birds</code><code class="o">;</code> <code class="n">observe</code><code class="o">(</code> <code class="n">animals</code><code class="o">[</code><code class="mi">0</code><code class="o">][</code><code class="mi">0</code><code class="o">]</code> <code class="o">);</code> <code class="c1">// processes Bird object</code></pre><p>Because arrays are part of the class hierarchy, we can use <code class="literal">instanceof</code> to check the type of an array:</p><a id="I_6_tt329"/><pre class="programlisting"> <code class="k">if</code> <code class="o">(</code> <code class="n">birds</code> <code class="k">instanceof</code> <code class="n">Animal</code><code class="o">[][]</code> <code class="o">)</code> <code class="c1">// true</code></pre><p>An array is a type of <code class="literal">Object</code> and thus can be assigned to <code class="literal">Object</code> type variables:</p><a id="I_6_tt330"/><pre class="programlisting"> <code class="n">Object</code> <code class="n">obj</code> <code class="o">=</code> <code class="n">animals</code><code class="o">;</code></pre><p>Because Java knows the actual type of all objects, you can also cast back if appropriate:</p><a id="I_6_tt331"/><pre class="programlisting"> <code class="n">animals</code> <code class="o">=</code> <code class="o">(</code><code class="n">Animal</code> <code class="o">[][])</code><code class="n">something</code><code class="o">;</code></pre><div class="figure"><a id="learnjava3-CHP-6-FIG-8"/><div class="figure-contents"><div class="mediaobject"><a id="I_6_tt332"/><img src="httpatomoreillycomsourceoreillyimages1707626.png" alt="Arrays in the Java class hierarchy"/></div></div><p class="title">Figure 6-8. Arrays in the Java class hierarchy</p></div><div class="sect2" title="ArrayStoreException"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-6-SECT-5.1"/>ArrayStoreException</h2></div></div></div><p><a id="idx10268" class="indexterm"/>Because arrays have the property that an array of one type is assignable to an array of its supertype, it is possible to play games with the compiler and try to trick it into storing the wrong kind of object in an array. Java may not be able to check the types of all objects that you place into arrays at compile time. In those cases, it’s possible to receive an <code class="literal">ArrayStoreException</code> at runtime if you try to assign the wrong type of object to an array element. For example:</p><a id="I_6_tt333"/><pre class="programlisting"> <code class="n">String</code> <code class="o">[]</code> <code class="n">strings</code> <code class="o">=</code> <code class="k">new</code> <code class="n">String</code> <code class="o">[</code><code class="mi">10</code><code class="o">];</code> <code class="n">Object</code> <code class="o">[]</code> <code class="n">objects</code> <code class="o">=</code> <code class="n">strings</code><code class="o">;</code> <code class="c1">// alias String [] as Object []</code> <code class="n">objects</code><code class="o">[</code><code class="mi">0</code><code class="o">]</code> <code class="o">=</code> <code class="k">new</code> <code class="n">Date</code><code class="o">();</code> <code class="c1">// Runtime ArrayStoreException!</code></pre><p>Here, we have “aliased” a <code class="literal">String []</code> by assigning it to an <code class="literal">Object []</code>. By the third line, the compiler no longer knows the actual type of array stored in the object’s variable and has no choice but to let us try whatever we want. Of course, at runtime the VM realizes that we are trying to put a <code class="literal">Date</code> object into an array of <code class="literal">String</code>s and throws the <code class="literal">Array</code><code class="literal">Store</code><code class="literal">Exception</code> for us. This type of problem shouldn’t happen often for you in straightforward array use. We mention it here because the concept will come up again when we talk about generics in <a class="xref" href="ch08.html" title="Chapter 8. Generics">Chapter 8</a>.<a id="I_indexterm6_id699776" class="indexterm"/><a id="I_indexterm6_id699784" class="indexterm"/></p></div></div></body></html>