epubjs
Version:
Render ePub documents in the browser, across many devices
249 lines (248 loc) • 35.2 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>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="Types"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-4-SECT-3"/>Types</h1></div></div></div><p><a id="I_indexterm4_id647838" class="indexterm"/> <a id="I_indexterm4_id647844" class="indexterm"/> <a id="I_indexterm4_id647850" class="indexterm"/> The type system of a programming language describes how its
data elements (variables and constants) are associated with storage in
memory and how they are related to one another. In a statically typed
language, such as C or C++, the type of a data element is a simple,
unchanging attribute that often corresponds directly to some underlying
hardware phenomenon, such as a register or a pointer value. In a more
dynamic language such as Smalltalk or Lisp, variables can be assigned
arbitrary elements and can effectively change their type throughout their
lifetime. A considerable amount of overhead goes into validating what
happens in these languages at runtime. Scripting languages such as Perl
achieve ease of use by providing drastically simplified type systems in
which only certain data elements can be stored in variables, and values
are unified into a common representation, such as strings.</p><p>Java combines many of the best features of both statically and
dynamically typed languages. As in a statically typed language, every
variable and programming element in Java has a type that is known at
compile time, so the runtime system doesn’t normally have to check the
validity of assignments between types while the code is executing. Unlike
traditional C or C++, Java also maintains runtime information about
objects and uses this to allow truly dynamic behavior. Java code may load
new types at runtime and use them in fully object-oriented ways, allowing
casting and full polymorphism (extending of types). Java code may also
“reflect” upon or examine its own types at runtime, allowing advanced
kinds of application behavior such as interpreters that can interact with
compiled programs dynamically.</p><p>Java data types fall into two categories. <span class="emphasis"><em>Primitive
types</em></span> represent simple values that have built-in functionality
in the language; they are fixed elements, such as literal constants and
numbers. <span class="emphasis"><em>Reference types</em></span> (or class types) include
objects and arrays; they are called reference types because they “refer
to” a large data type that is passed “by reference,” as we’ll explain
shortly. <span class="emphasis"><em>Generic types</em></span> are really just a kind of
composition (combination) of class types and are therefore reference types
as well.</p><div class="sect2" title="Primitive Types"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-4-SECT-3.1"/>Primitive Types</h2></div></div></div><p><a id="idx10208" class="indexterm"/> Numbers, characters, and Boolean values are fundamental
elements in Java. Unlike some other (perhaps more pure) object-oriented
languages, they are not objects. For those situations where it’s
desirable to treat a primitive value as an object, Java provides
“wrapper” classes. The major advantage of treating primitive values as
special is that the Java compiler and runtime can more readily optimize
their implementation. Primitive values and computations can still be
mapped down to hardware as they always have been in lower-level
languages. Later we’ll see how Java can automatically convert between
primitive values and their object wrappers as needed to partially mask
the difference between the two. We’ll explain what that means in more
detail in the next chapter when we discuss <a id="I_indexterm4_id647924" class="indexterm"/><span class="emphasis"><em>boxing</em></span> and <a id="I_indexterm4_id647934" class="indexterm"/><span class="emphasis"><em>unboxing</em></span> of primitive values.</p><p>An important portability feature of Java is that primitive types
are precisely defined. For example, you never have to worry about the
size of an <code class="literal">int</code> on a particular
platform; it’s always a 32-bit, signed, two’s complement number. <a class="xref" href="ch04s03.html#learnjava3-CHP-4-TABLE-2" title="Table 4-2. Java primitive data types">Table 4-2</a> summarizes Java’s primitive
types.</p><div class="table"><a id="learnjava3-CHP-4-TABLE-2"/><p class="title">Table 4-2. Java primitive data types</p><div class="table-contents"><table summary="Java primitive data types" style="border-collapse: collapse;border-top: 0.5pt solid ; border-bottom: 0.5pt solid ; "><colgroup><col/><col/></colgroup><thead><tr><th style="text-align: left"><p>Type</p></th><th style="text-align: left"><p>Definition</p></th></tr></thead><tbody><tr><td style="text-align: left"><p> <a id="I_indexterm4_id648004" class="indexterm"/> <code class="literal">boolean</code>
</p></td><td style="text-align: left"><p> <code class="literal">true</code> or <code class="literal">false</code></p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm4_id648037" class="indexterm"/> <code class="literal">char</code>
</p></td><td style="text-align: left"><p>16-bit, Unicode
character</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm4_id648060" class="indexterm"/> <code class="literal">byte</code>
</p></td><td style="text-align: left"><p>8-bit, signed, two’s complement
integer</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm4_id648084" class="indexterm"/> <code class="literal">short</code>
</p></td><td style="text-align: left"><p>16-bit, signed, two’s complement
integer</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm4_id648108" class="indexterm"/> <code class="literal">int</code>
</p></td><td style="text-align: left"><p>32-bit, signed, two’s complement
integer</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm4_id648132" class="indexterm"/> <code class="literal">long</code>
</p></td><td style="text-align: left"><p>64-bit, signed, two’s complement
integer</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm4_id648156" class="indexterm"/> <code class="literal">float</code>
</p></td><td style="text-align: left"><p>32-bit, IEEE 754, floating-point
value</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm4_id648179" class="indexterm"/> <code class="literal">double</code>
</p></td><td style="text-align: left"><p>64-bit, IEEE 754</p></td></tr></tbody></table></div></div><div class="note" title="Note"><h3 class="title">Note</h3><p>Those of you with a C background may notice that the primitive
types look like an idealization of C scalar types on a 32-bit
machine, and you’re absolutely right. That’s how they’re supposed to
look. The 16-bit characters were forced by Unicode, and ad hoc
pointers were deleted for other reasons. But overall, the syntax and
semantics of Java primitive types derive from C.</p></div><div class="sect3" title="Floating-point precision"><div class="titlepage"><div><div><h3 class="title"><a id="learnjava3-CHP-4-SECT-3.1.1"/>Floating-point precision</h3></div></div></div><p><a id="I_indexterm4_id648215" class="indexterm"/> <a id="I_indexterm4_id648222" class="indexterm"/> <a id="I_indexterm4_id648231" class="indexterm"/>Floating-point operations in Java follow the IEEE 754
international specification, which means that the result of
floating-point calculations is normally the same on different Java
platforms. However, Java allows for extended precision on platforms
that support it. This can introduce extremely small-valued and arcane
differences in the results of high-precision operations. Most
applications would never notice this, but if you want to ensure that
your application produces exactly the same results on different
platforms, you can use the special keyword <a id="I_indexterm4_id648247" class="indexterm"/><code class="literal">strictfp</code> as a class
modifier on the class containing the floating-point manipulation (we
cover classes in the next chapter). The compiler then prohibits these
platform-specific optimizations.</p></div><div class="sect3" title="Variable declaration and initialization"><div class="titlepage"><div><div><h3 class="title"><a id="learnjava3-CHP-4-SECT-3.1.2"/>Variable declaration and initialization</h3></div></div></div><p><a id="idx10170" class="indexterm"/> <a id="idx10215" class="indexterm"/>Variables are declared inside of methods and classes
with a type name followed by one or more comma-separated variable
names. For example:</p><a id="I_4_tt116"/><pre class="programlisting"> <code class="kt">int</code> <code class="n">foo</code><code class="o">;</code>
<code class="kt">double</code> <code class="n">d1</code><code class="o">,</code> <code class="n">d2</code><code class="o">;</code>
<code class="kt">boolean</code> <code class="n">isFun</code><code class="o">;</code></pre><p>Variables can optionally be initialized with an expression of
the appropriate type when they are declared:</p><a id="I_4_tt117"/><pre class="programlisting"> <code class="kt">int</code> <code class="n">foo</code> <code class="o">=</code> <code class="mi">42</code><code class="o">;</code>
<code class="kt">double</code> <code class="n">d1</code> <code class="o">=</code> <code class="mf">3.14</code><code class="o">,</code> <code class="n">d2</code> <code class="o">=</code> <code class="mi">2</code> <code class="o">*</code> <code class="mf">3.14</code><code class="o">;</code>
<code class="kt">boolean</code> <code class="n">isFun</code> <code class="o">=</code> <code class="kc">true</code><code class="o">;</code></pre><p>Variables that are declared as members of a class are set to
default values if they aren’t initialized (see <a class="xref" href="ch05.html" title="Chapter 5. Objects in Java">Chapter 5</a>). In this case, numeric types default to
the appropriate flavor of zero, characters are set to the null
character (<code class="literal">\0)</code>, and Boolean
variables have the value <code class="literal">false</code>.
Local variables, which are declared inside a method and live only for
the duration of a method call, on the other hand, must be explicitly
initialized before they can be used. As we’ll see, the compiler
enforces this rule so there is no danger of forgetting.<a id="I_indexterm4_id648347" class="indexterm"/><a id="I_indexterm4_id648354" class="indexterm"/></p></div><div class="sect3" title="Integer literals"><div class="titlepage"><div><div><h3 class="title"><a id="learnjava3-CHP-4-SECT-3.1.3"/>Integer literals</h3></div></div></div><p><a id="idx10169" class="indexterm"/> <a id="idx10214" class="indexterm"/>Integer literals can be specified in octal (base 8),
decimal (base 10), or hexadecimal (base 16). A decimal integer is
specified by a sequence of digits beginning with one of the characters
1–9:</p><a id="I_4_tt118"/><pre class="programlisting"> <code class="kt">int</code> <code class="n">i</code> <code class="o">=</code> <code class="mi">1230</code><code class="o">;</code></pre><p><a id="I_indexterm4_id648409" class="indexterm"/>Octal numbers are distinguished from decimal numbers by
a leading zero:</p><a id="I_4_tt119"/><pre class="programlisting"> <code class="kt">int</code> <code class="n">i</code> <code class="o">=</code> <code class="mi">01230</code><code class="o">;</code> <code class="c1">// i = 664 decimal</code></pre><p><a id="I_indexterm4_id648426" class="indexterm"/>A hexadecimal number is denoted by the leading
characters <code class="literal">0x</code> or <code class="literal">0X</code> (zero “x”), followed by a combination of
digits and the characters a–f or A–F, which represent the decimal
values 10–15:</p><a id="I_4_tt120"/><pre class="programlisting"> <code class="kt">int</code> <code class="n">i</code> <code class="o">=</code> <code class="mi">0</code><code class="n">xFFFF</code><code class="o">;</code> <code class="c1">// i = 65535 decimal</code></pre><p>Integer literals are of type <a id="I_indexterm4_id648457" class="indexterm"/><code class="literal">int</code> unless they are
suffixed with an <a id="I_indexterm4_id648468" class="indexterm"/><code class="literal">L</code>, denoting that they
are to be produced as a <a id="I_indexterm4_id648479" class="indexterm"/><code class="literal">long</code> value:</p><a id="I_4_tt121"/><pre class="programlisting"> <code class="kt">long</code> <code class="n">l</code> <code class="o">=</code> <code class="mi">13L</code><code class="o">;</code>
<code class="kt">long</code> <code class="n">l</code> <code class="o">=</code> <code class="mi">13</code><code class="o">;</code> <code class="c1">// equivalent: 13 is converted from type int</code></pre><p>(The lowercase letter <code class="literal">l</code> is
also acceptable but should be avoided because it often looks like the
number <code class="literal">1</code>.)</p><p>When a numeric type is used in an assignment or an expression
involving a “larger” type with a greater range, it can be <a id="I_indexterm4_id648517" class="indexterm"/><span class="emphasis"><em>promoted</em></span> to the bigger type. In the
second line of the previous example, the number <code class="literal">13</code> has the default type of <code class="literal">int</code>, but it’s promoted to type <code class="literal">long</code> for assignment to the <code class="literal">long</code> variable. Certain other numeric and
comparison operations also cause this kind of arithmetic promotion, as
do mathematical expressions involving more than one type. For example,
when multiplying a <code class="literal">byte</code> value by an
<code class="literal">int</code> value, the compiler promotes
the <code class="literal">byte</code> to an <code class="literal">int</code> first:</p><a id="I_4_tt122"/><pre class="programlisting"> <code class="kt">byte</code> <code class="n">b</code> <code class="o">=</code> <code class="mi">42</code><code class="o">;</code>
<code class="kt">int</code> <code class="n">i</code> <code class="o">=</code> <code class="mi">43</code><code class="o">;</code>
<code class="kt">int</code> <code class="n">result</code> <code class="o">=</code> <code class="n">b</code> <code class="o">*</code> <code class="n">i</code><code class="o">;</code> <code class="c1">// b is promoted to int before multiplication</code></pre><p>A numeric value can never go the other way and be assigned to a
type with a smaller range without an explicit cast, however:</p><a id="I_4_tt123"/><pre class="programlisting"> <code class="kt">int</code> <code class="n">i</code> <code class="o">=</code> <code class="mi">13</code><code class="o">;</code>
<code class="kt">byte</code> <code class="n">b</code> <code class="o">=</code> <code class="n">i</code><code class="o">;</code> <code class="c1">// Compile-time error, explicit cast needed</code>
<code class="kt">byte</code> <code class="n">b</code> <code class="o">=</code> <code class="o">(</code><code class="kt">byte</code><code class="o">)</code> <code class="n">i</code><code class="o">;</code> <code class="c1">// OK</code></pre><p>Conversions from floating-point to integer types always require
an explicit cast because of the potential loss of precision.</p><p>Finally, we should note that if you are using Java 7 or later,
you can add a bit of formatting to your numeric literals by utilizing
the “_” underscore character between digits. So if you have
particularly large strings of digits, you can break them up as in the
following examples:<a id="I_programlisting4_id648605"/></p><pre class="programlisting"> <code class="kt">int</code> <code class="n">RICHARD_NIXONS_SSN</code> <code class="o">=</code> <code class="mi">567</code><code class="n">_68_0515</code><code class="o">;</code>
<code class="kt">int</code> <code class="n">for_no_reason</code> <code class="o">=</code> <code class="mi">1</code><code class="n">___2___3</code><code class="o">;</code>
<code class="kt">int</code> <code class="n">JAVA_ID</code> <code class="o">=</code> <code class="mi">0</code><code class="n">xCAFE_BABE</code><code class="o">;</code></pre><p>Underscores may only appear between digits, not at the beginning
or end of a number or next to the “L” long integer
signifier.<a id="I_indexterm4_id648616" class="indexterm"/><a id="I_indexterm4_id648624" class="indexterm"/></p></div><div class="sect3" title="Floating-point literals"><div class="titlepage"><div><div><h3 class="title"><a id="learnjava3-CHP-4-SECT-3.1.4"/>Floating-point literals</h3></div></div></div><p><a id="I_indexterm4_id648638" class="indexterm"/> <a id="I_indexterm4_id648646" class="indexterm"/>Floating-point values can be specified in decimal or
scientific notation. Floating-point literals are of type <a id="I_indexterm4_id648658" class="indexterm"/><code class="literal">double</code> unless they
are suffixed with an <code class="literal">f</code> or
<a id="I_indexterm4_id648674" class="indexterm"/><code class="literal">F</code> denoting that they
are to be produced as a <a id="I_indexterm4_id648685" class="indexterm"/><code class="literal">float</code> value. And just
as with integer literals, in Java 7 you may use “_” underscore
characters to format floating-point numbers—but only between digits,
not at the beginning, end, or next to the decimal point or “F”
signifier of the number.</p><a id="I_4_tt124"/><pre class="programlisting"> <code class="kt">double</code> <code class="n">d</code> <code class="o">=</code> <code class="mf">8.31</code><code class="o">;</code>
<code class="kt">double</code> <code class="n">e</code> <code class="o">=</code> <code class="mf">3.00</code><code class="n">e</code><code class="o">+</code><code class="mi">8</code><code class="o">;</code>
<code class="kt">float</code> <code class="n">f</code> <code class="o">=</code> <code class="mf">8.31</code><code class="n">F</code><code class="o">;</code>
<code class="kt">float</code> <code class="n">g</code> <code class="o">=</code> <code class="mf">3.00</code><code class="n">e</code><code class="o">+</code><code class="mi">8</code><code class="n">F</code><code class="o">;</code>
<code class="kt">float</code> <code class="n">pi</code> <code class="o">=</code> <code class="mf">3.14</code><code class="n">_159_265_358</code><code class="o">;</code></pre></div><div class="sect3" title="Binary literals"><div class="titlepage"><div><div><h3 class="title"><a id="id1712658"/>Binary literals</h3></div></div></div><p><a id="I_indexterm4_id648714" class="indexterm"/> <a id="I_indexterm4_id648723" class="indexterm"/>A new feature of Java 7 is the introduction of binary
literal values. This allows you to write out binary values directly by
prefixing the number with a “0b” or “0B” (zero B).<a id="I_programlisting4_id648737"/></p><pre class="programlisting"> <code class="kt">byte</code> <code class="n">one</code> <code class="o">=</code> <code class="o">(</code><code class="kt">byte</code><code class="o">)</code><code class="mi">0</code><code class="n">b00000001</code><code class="o">;</code>
<code class="kt">byte</code> <code class="n">two</code> <code class="o">=</code> <code class="o">(</code><code class="kt">byte</code><code class="o">)</code><code class="mi">0</code><code class="n">b00000010</code><code class="o">;</code>
<code class="kt">byte</code> <code class="n">four</code> <code class="o">=</code> <code class="o">(</code><code class="kt">byte</code><code class="o">)</code><code class="mi">0</code><code class="n">b00000100</code><code class="o">;</code>
<code class="kt">byte</code> <code class="n">sixteen</code> <code class="o">=</code> <code class="o">(</code><code class="kt">byte</code><code class="o">)</code><code class="mi">0</code><code class="n">b00001000</code><code class="o">;</code>
<code class="kt">int</code> <code class="n">cafebabe</code> <code class="o">=</code> <code class="mi">0</code><code class="n">b11001010111111101011101010111110</code><code class="o">;</code>
<code class="kt">long</code> <code class="n">lots_o_ones</code> <code class="o">=</code> <code class="o">(</code><code class="kt">long</code><code class="o">)</code><code class="mi">0</code><code class="n">b11111111111111111111111111111111111111111111111L</code><code class="o">;</code></pre></div><div class="sect3" title="Character literals"><div class="titlepage"><div><div><h3 class="title"><a id="learnjava3-CHP-4-SECT-3.1.5"/>Character literals</h3></div></div></div><p><a id="I_indexterm4_id648752" class="indexterm"/> <a id="I_indexterm4_id648761" class="indexterm"/>A literal character value can be specified either as a
single-quoted character or as an escaped ASCII or Unicode
sequence:<a id="I_indexterm4_id648773" class="indexterm"/></p><a id="I_4_tt125"/><pre class="programlisting"> <code class="kt">char</code> <code class="n">a</code> <code class="o">=</code> <code class="sc">'a'</code><code class="o">;</code>
<code class="kt">char</code> <code class="n">newline</code> <code class="o">=</code> <code class="sc">'\n'</code><code class="o">;</code>
<code class="kt">char</code> <code class="n">smiley</code> <code class="o">=</code> <code class="sc">'\u263a'</code><code class="o">;</code></pre></div></div><div class="sect2" title="Reference Types"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-4-SECT-3.2"/>Reference Types</h2></div></div></div><p><a id="idx10171" class="indexterm"/> <a id="idx10209" class="indexterm"/> <a id="idx10216" class="indexterm"/>In an object-oriented language like Java, you create new,
complex data types from simple primitives by creating a <code class="literal">class</code>. Each class then serves as a new type in
the language. For example, if we create a new class called <code class="literal">Foo</code> in Java, we are also implicitly creating a
new type called <code class="literal">Foo</code>. The type of an
item governs how it’s used and where it can be assigned. As with
primitives, an item of type <code class="literal">Foo</code> can,
in general, be assigned to a variable of type <code class="literal">Foo</code> or passed as an argument to a method that
accepts a <code class="literal">Foo</code> value.</p><p>A type is not just a simple attribute. Classes can have
relationships with other classes and so do the types that they
represent. All classes in Java exist in a parent-child hierarchy, where
a child class or <span class="emphasis"><em>subclass</em></span> is a specialized kind of
its parent class. The corresponding types have the same relationship,
where the type of the child class is considered a subtype of the parent
class. Because child classes inherit all of the functionality of their
parent classes, an object of the child’s type is in some sense
equivalent to or an extension of the parent type. An object of the child
type can be used in place of an object of the parent’s type. For
example, if you create a new class, <code class="literal">Cat</code>, that extends <code class="literal">Animal</code>, the new type, <code class="literal">Cat</code>, is considered a subtype of <code class="literal">Animal</code>. Objects of type <code class="literal">Cat</code> can then be used anywhere an object of
type <code class="literal">Animal</code> can be used; an object of
type <code class="literal">Cat</code> is said to be assignable to
a variable of type <a id="I_indexterm4_id648925" class="indexterm"/><code class="literal">Animal</code>. This is called
<span class="emphasis"><em>subtype polymorphism</em></span> and is one of the primary
features of an object-oriented language. We’ll look more closely at
classes and objects in <a class="xref" href="ch05.html" title="Chapter 5. Objects in Java">Chapter 5</a>.</p><p>Primitive types in Java are used and passed “by value.” In other
words, when a primitive value like an <code class="literal">int</code> is assigned to a variable or passed as an
argument to a method, it’s simply copied. Reference types (class types),
on the other hand, are always accessed “by reference.” A <a id="I_indexterm4_id648959" class="indexterm"/><span class="emphasis"><em>reference</em></span> is simply a handle or a
name for an object. What a variable of a reference type holds is a
“pointer” to an object of its type (or of a subtype, as described
earlier). When the reference is assigned to a variable or passed to a
method, only the reference is copied, not the object to which it’s
pointing. A reference is like a pointer in C or C++, except that its
type is so strictly enforced. The reference value itself can’t be
explicitly created or changed. A variable acquires a reference value
only through assignment to an appropriate object.</p><p>Let’s run through an example. We declare a variable of type
<code class="literal">Foo</code>, called <code class="literal">myFoo</code>, and assign it an appropriate
object:<sup>[<a id="learnjava3-CHP-4-FNOTE-2" href="#ftn.learnjava3-CHP-4-FNOTE-2" class="footnote">7</a>]</sup></p><a id="I_4_tt127"/><pre class="programlisting"> <code class="n">Foo</code> <code class="n">myFoo</code> <code class="o">=</code> <code class="k">new</code> <code class="n">Foo</code><code class="o">();</code>
<code class="n">Foo</code> <code class="n">anotherFoo</code> <code class="o">=</code> <code class="n">myFoo</code><code class="o">;</code></pre><p><code class="literal">myFoo</code> is a reference-type
variable that holds a reference to the newly constructed <code class="literal">Foo</code> object. (For now, don’t worry about the
details of creating an object; we’ll cover that in <a class="xref" href="ch05.html" title="Chapter 5. Objects in Java">Chapter 5</a>.) We declare a second <code class="literal">Foo</code> type variable, <code class="literal">anotherFoo</code>, and assign it to the same object.
There are now two identical references : <code class="literal">myFoo</code> and <code class="literal">anotherFoo</code>, but only one actual <code class="literal">Foo</code> object instance. If we change things in
the state of the <code class="literal">Foo</code> object itself,
we see the same effect by looking at it with either reference.</p><p>Object references are passed to methods in the same way. In this
case, either <code class="literal">myFoo</code> or <code class="literal">anotherFoo</code> would serve as equivalent
arguments:</p><a id="I_4_tt128"/><pre class="programlisting"> <code class="n">myMethod</code><code class="o">(</code> <code class="n">myFoo</code> <code class="o">);</code></pre><p>An important, but sometimes confusing, distinction to make at this
point is that the reference itself is a value and that value is copied
when it is assigned to a variable or passed in a method call. Given our
previous example, the argument passed to a method (a local variable from
the method’s point of view) is actually a third reference to the Foo
object, in addition to <code class="literal">myFoo</code> and
<code class="literal">anotherFoo</code>. The method can alter the
state of the <code class="literal">Foo</code> object through that
reference (calling its methods or altering its variables), but it can’t
change the caller’s notion of the reference to <code class="literal">myFoo</code>: that is, the method can’t change the
caller’s <code class="literal">myFoo</code> to point to a
different <code class="literal">Foo</code> object; it can change
only its own reference. This will be more obvious when we talk about
methods later. Java differs from C++ in this respect. If you need to
change a caller’s reference to an object in Java, you need an additional
level of indirection. The caller would have to wrap the reference in
another object so that both could share the reference to it.</p><p>Reference types always point to objects, and objects are always
defined by classes. However, two special kinds of reference types—arrays
and interfaces—specify the type of object they point to in a slightly
different way.</p><p>Arrays in Java have a special place in the type system. They are a
special kind of object automatically created to hold a collection of
some other type of object, known as the <span class="emphasis"><em>base type</em></span>.
Declaring an array type reference implicitly creates the new class type
designed as a container for its base type, as you’ll see in the next
chapter.</p><p>Interfaces are a bit sneakier. An interface defines a set of
methods and gives it a corresponding type. An object that implements the
methods of the interface can be referred to by that interface type, as
well as its own type. Variables and method arguments can be declared to
be of interface types, just like other class types, and any object that
implements the interface can be assigned to them. This adds flexibility
in the type system and allows Java to cross the lines of the class
hierarchy and make objects that effectively have many types. We’ll cover
interfaces in the next chapter as well.</p><p><a id="I_indexterm4_id649170" class="indexterm"/> <a id="I_indexterm4_id649178" class="indexterm"/> <span class="emphasis"><em>Generic types</em></span> or
<span class="emphasis"><em>parameterized types</em></span>, as we mentioned earlier, are
an extension of the Java class syntax that allows for additional
abstraction in the way classes work with other Java types. Generics
allow for specialization of classes by the user without changing any of
the original class’s code. We cover generics in detail in <a class="xref" href="ch08.html" title="Chapter 8. Generics">Chapter 8</a>.<a id="I_indexterm4_id649204" class="indexterm"/><a id="I_indexterm4_id649211" class="indexterm"/><a id="I_indexterm4_id649218" class="indexterm"/></p></div><div class="sect2" title="A Word About Strings"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-4-SECT-3.3"/>A Word About Strings</h2></div></div></div><p><a id="I_indexterm4_id649232" class="indexterm"/> <a id="idx10210" class="indexterm"/> <a id="idx10217" class="indexterm"/>Strings in Java are objects; they are therefore a
reference type. <a id="I_indexterm4_id649266" class="indexterm"/><code class="literal">String</code> objects do,
however, have some special help from the Java compiler that makes them
look more like primitive types. Literal string values in Java source
code are turned into <code class="literal">String</code> objects
by the compiler. They can be used directly, passed as arguments to
methods, or assigned to <code class="literal">String</code> type
variables:</p><a id="I_4_tt129"/><pre class="programlisting"> <code class="n">System</code><code class="o">.</code><code class="na">out</code><code class="o">.</code><code class="na">println</code><code class="o">(</code> <code class="s">"Hello, World..."</code> <code class="o">);</code>
<code class="n">String</code> <code class="n">s</code> <code class="o">=</code> <code class="s">"I am the walrus..."</code><code class="o">;</code>
<code class="n">String</code> <code class="n">t</code> <code class="o">=</code> <code class="s">"John said: \"I am the walrus...\""</code><code class="o">;</code></pre><p>The <a id="I_indexterm4_id649301" class="indexterm"/><a id="I_indexterm4_id649306" class="indexterm"/><code class="literal">+</code> symbol in Java is
“overloaded” to perform string concatenation as well as regular numeric
addition. Along with its sister <a id="I_indexterm4_id649318" class="indexterm"/><code class="literal">+=</code>, this is the only
overloaded operator in Java:</p><a id="I_4_tt130"/><pre class="programlisting"> <code class="n">String</code> <code class="n">quote</code> <code class="o">=</code> <code class="s">"Four score and "</code> <code class="o">+</code> <code class="s">"seven years ago,"</code><code class="o">;</code>
<code class="n">String</code> <code class="n">more</code> <code class="o">=</code> <code class="n">quote</code> <code class="o">+</code> <code class="s">" our"</code> <code class="o">+</code> <code class="s">" fathers"</code> <code class="o">+</code> <code class="s">" brought..."</code><code class="o">;</code></pre><p>Java builds a single <code class="literal">String</code>
object from the concatenated strings and provides it as the result of
the expression. We discuss the <code class="literal">String</code>
class and all things text-related in great detail in <a class="xref" href="ch10.html" title="Chapter 10. Working with Text">Chapter 10</a>.<a id="I_indexterm4_id649358" class="indexterm"/></p></div><div class="footnotes"><br/><hr/><div class="footnote"><p><sup>[<a id="ftn.learnjava3-CHP-4-FNOTE-2" href="#learnjava3-CHP-4-FNOTE-2" class="para">7</a>] </sup>The comparable code in C++ would be:</p><a id="I_4_tt126"/><pre class="programlisting"> <code class="n">Foo</code><code class="o">&</code> <code class="n">myFoo</code> <code class="o">=</code> <code class="o">*(</code><code class="k">new</code> <code class="n">Foo</code><code class="o">());</code>
<code class="n">Foo</code><code class="o">&</code> <code class="n">anotherFoo</code> <code class="o">=</code> <code class="n">myFoo</code><code class="o">;</code></pre></div></div></div></body></html>