UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

94 lines (92 loc) 13.2 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 of Parameterized Types</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 of Parameterized Types"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-8-SECT-10"/>Arrays of Parameterized Types</h1></div></div></div><p><a id="idx10437" class="indexterm"/>There is one place where we haven’t yet considered how generic types affect the Java language: array types. After everything we’ve seen, it would seem natural to expect that arrays of generic types would come along for the ride. But as we’ll see, Java has a schizophrenic relationship with arrays of parameterized types.</p><p>The first thing we need to do is recall how arrays work for regular Java types. An array is a kind of built-in collection of some base type of element. Furthermore, array types (including all multidimensional variations of the array) are true types in the Java language and are represented at runtime by unique class types. This is where the trouble begins. Although arrays in Java act a lot like generic collections (they change their APIs to adopt a particular type for “reading” and “writing”), they do not behave like Java generics with respect to their type relationships. As we saw in <a class="xref" href="ch06.html" title="Chapter 6. Relationships Among Classes">Chapter 6</a>, arrays exist in the Java class hierarchy stemming from <code class="literal">Object</code> and extending down parallel branches with the plain Java objects.</p><p><a id="I_indexterm8_id713015" class="indexterm"/>Arrays are <span class="emphasis"><em>covariant subtypes</em></span> of other types of arrays, which means that, unlike concrete generic types, although they change their method signatures, they are still related to their parents. This means that <code class="literal">Strings []</code> in Java is a subtype of <code class="literal">Object []</code>. This brings up the aliasing problem that we mentioned earlier. An array of <code class="literal">String</code>s can be aliased as an array of <code class="literal">Object</code>s and we can attempt to put things into it illegally that won’t be noticed until runtime:</p><a id="I_8_tt469"/><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">5</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="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>To prevent disaster, Java must check every array assignment for the correct type at runtime. But recall that generic types do not have real representations at runtime; there is only the raw type. So Java would have no way to know the difference between a <code class="literal">Trap&lt;Mouse&gt;</code> and a <code class="literal">Trap&lt;Bear&gt;</code> element in an array once the array was aliased as, say, an <code class="literal">Object []</code>. For this reason, Java does not allow you to create arrays of generic types—at least not concrete ones. (More on that later in this chapter.)</p><div class="sect2" title="Using Array Types"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-8-SECT-10.1"/>Using Array Types</h2></div></div></div><p><a id="idx10388" class="indexterm"/> <a id="idx10401" class="indexterm"/> <a id="idx10432" class="indexterm"/>Now, because we just said that Java won’t let you make any of these arrays, you’d expect that would be pretty much the end of the story. But no! Even though we don’t have real array implementations that perform the needed runtime behavior, Java allows us to declare the array type anyway. The catch is that you must break type safety in order to use them by using an array of the raw type as their implementation:</p><a id="I_8_tt470"/><pre class="programlisting"> <code class="n">Trap</code><code class="o">&lt;</code><code class="n">Mouse</code><code class="o">&gt;</code> <code class="o">[]</code> <code class="n">tma</code> <code class="o">=</code> <code class="k">new</code> <code class="n">Trap</code><code class="o">[</code><code class="mi">10</code><code class="o">];</code> <code class="c1">// unchecked warning</code> <code class="n">Trap</code><code class="o">&lt;</code><code class="n">Mouse</code><code class="o">&gt;</code> <code class="n">tm</code> <code class="o">=</code> <code class="k">new</code> <code class="n">Trap</code><code class="o">&lt;</code><code class="n">Mouse</code><code class="o">&gt;();</code> <code class="n">tma</code><code class="o">[</code><code class="mi">0</code><code class="o">]</code> <code class="o">=</code> <code class="n">tm</code><code class="o">;</code> <code class="n">Trap</code><code class="o">&lt;</code><code class="n">Mouse</code><code class="o">&gt;</code> <code class="n">again</code> <code class="o">=</code> <code class="n">tma</code><code class="o">[</code><code class="mi">0</code><code class="o">];</code></pre><p>Here, we declared an array of a generic type, <code class="literal">Trap&lt;Mouse&gt;</code>. Assigning any value (other than <code class="literal">null</code>) to this variable, <code class="literal">tma</code>, results in an unchecked warning from the compiler at the point of the assignment.</p><p>What we are effectively telling the compiler here is to trust us to make sure that the array contains only the correct generic types and asking it to allow us to use it thereafter as if it were checked. We do not get warnings at each usage as we would with a raw type, only at the point where we assign the array. The catch is that the compiler can’t prevent us from abusing the array. The unchecked warning at the point where we assign the array is just a representative warning that reminds us that it’s possible to abuse the array later.<a id="I_indexterm8_id713192" class="indexterm"/><a id="I_indexterm8_id713199" class="indexterm"/><a id="I_indexterm8_id713206" class="indexterm"/></p></div><div class="sect2" title="What Good Are Arrays of Generic Types?"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-8-SECT-10.2"/>What Good Are Arrays of Generic Types?</h2></div></div></div><p><a id="I_indexterm8_id713220" class="indexterm"/> <a id="I_indexterm8_id713233" class="indexterm"/> <a id="I_indexterm8_id713244" class="indexterm"/>Why does Java even let us declare arrays of generic types? One important usage is that it allows generic types to be used in variable-length argument methods. For example:</p><a id="I_8_tt471"/><pre class="programlisting"> <code class="kt">void</code> <code class="nf">useLists</code><code class="o">(</code> <code class="n">List</code><code class="o">&lt;</code><code class="n">String</code><code class="o">&gt;</code> <code class="o">...</code> <code class="n">lists</code> <code class="o">)</code> <code class="o">{</code> <code class="n">List</code><code class="o">&lt;</code><code class="n">String</code><code class="o">&gt;</code> <code class="n">ls0</code> <code class="o">=</code> <code class="n">lists</code><code class="o">[</code><code class="mi">0</code><code class="o">];</code> <code class="o">}</code></pre><p>Another answer is that it’s an escape hatch to preserve our ability to use arrays when necessary. You might want to do this for at least two reasons. First, arrays are faster than collections in many cases. The Java runtime is very good at optimizing array access, and sometimes it just might be worth it to you to eat the compiler warning to get the benefits. Second, there is the issue of interfacing generic code to legacy code in which only the Javadoc and your faith in the developer are your guarantees as to the contents. By assigning raw arrays to generic instantiations, we can at least ensure that in simple usage we don’t abuse the types in the new code.</p></div><div class="sect2" title="Wildcards in Array Types"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-8-SECT-10.3"/>Wildcards in Array Types</h2></div></div></div><p><a id="idx10402" class="indexterm"/>In general, wildcard instantiations of generics can be used as the base type for arrays in the same way that concrete instantiations can. Let’s look at an example:</p><a id="I_8_tt472"/><pre class="programlisting"> <code class="n">ArrayList</code><code class="o">&lt;?&gt;[]</code> <code class="n">arrayOfArrayLists</code> <code class="o">=</code> <code class="o">...;</code></pre><p>This type declaration is an array of unbounded wildcard instantiations of <code class="literal">ArrayList</code>. Each element of the array can hold an instance of the wildcard type, meaning in this case that each element of the array could hold a different instantiation of <code class="literal">ArrayList</code>. For example:</p><a id="I_8_tt473"/><pre class="programlisting"> <code class="n">arrayOfArrayLists</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">ArrayList</code><code class="o">&lt;</code><code class="n">Date</code><code class="o">&gt;();</code> <code class="n">arrayOfArrayLists</code><code class="o">[</code><code class="mi">1</code><code class="o">]</code> <code class="o">=</code> <code class="k">new</code> <code class="n">ArrayList</code><code class="o">&lt;</code><code class="n">String</code><code class="o">&gt;();</code></pre><p>There is also a secret surprise that we are going to spring on you relating to wildcard types in arrays. Although we said that Java won’t let us create arrays of generic types, there is an exception to the rule. Java does allow us to create arrays of unbounded wildcard instantiations. Here are two examples:</p><a id="I_8_tt474"/><pre class="programlisting"> <code class="n">ArrayList</code><code class="o">&lt;?&gt;[]</code> <code class="n">arrayOfArrayLists</code> <code class="o">=</code> <code class="k">new</code> <code class="n">ArrayList</code><code class="o">&lt;?&gt;[</code><code class="mi">10</code><code class="o">];</code> <code class="n">arrayOfArrayLists</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">ArrayList</code><code class="o">&lt;</code><code class="n">Date</code><code class="o">&gt;();</code> <code class="n">Trap</code><code class="o">&lt;?&gt;</code> <code class="o">[]</code> <code class="n">arrayOfTraps</code> <code class="o">=</code> <code class="k">new</code> <code class="n">Trap</code><code class="o">&lt;?&gt;[</code><code class="mi">10</code><code class="o">];</code> <code class="n">arrayOfTraps</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">Trap</code><code class="o">&lt;</code><code class="n">Mouse</code><code class="o">&gt;();</code></pre><p>Here, we not only declared two arrays of wildcard instantiations, but we allocated the arrays as well! The trick is that the arrays must be of the unbounded wildcard type. Why does this work? Because each element in the unbounded wildcard instantiation of the array can hold any instantiation, no runtime check of the generic portion of the type is necessary at runtime. Any instantiation of <code class="literal">ArrayList</code> is assignable to the element of type <code class="literal">ArrayList&lt;?&gt;</code>, so only the check of the raw type is required.</p><p><a id="I_indexterm8_id713379" class="indexterm"/>The term <span class="emphasis"><em>reifiable type</em></span> is used to refer to any type that is unchanged by erasure. This includes plain Java concrete types, primitives, and unbounded wildcard instantiations. Reifiable types are kind of like the real people in <span class="emphasis"><em>The Matrix</em></span>: they still exist when unplugged from the simulation.<a id="I_indexterm8_id713394" class="indexterm"/><a id="I_indexterm8_id713401" class="indexterm"/></p></div></div></body></html>