Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
4859 lines (4661 sloc) 339 KB
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter&nbsp;4.&nbsp;Types, Values, and Variables</title>
<link rel="stylesheet" type="text/css" href="../../../javaspec.css">
<meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1">
<link rel="home" href="index.html" title="The Java&reg; Language Specification">
<link rel="up" href="index.html" title="The Java&reg; Language Specification">
<link rel="prev" href="jls-3.html" title="Chapter&nbsp;3.&nbsp;Lexical Structure">
<link rel="next" href="jls-5.html" title="Chapter&nbsp;5.&nbsp;Conversions and Contexts">
<link rel="copyright" href="jls-0-front.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:rx="http://www.renderx.com/XSL/Extensions" id="logo"><img src="http://www.oracleimg.com/us/assets/oralogo-small.gif" alt="Oracle Logo"><br><p><a target="_blank" href="http://www.oracle.com/us/technologies/java/">Oracle
Technology Network</a> &gt; <a target="_blank" href="http://docs.oracle.com/javase/">Java SE</a>
&gt; <a href="index.html">Java Language Specification</a></p>
</div>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Chapter&nbsp;4.&nbsp;Types, Values, and Variables</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="jls-3.html">Prev</a>&nbsp;
</td>
<th width="60%" align="center">&nbsp;</th>
<td width="20%" align="right">&nbsp;<a accesskey="n" href="jls-5.html">Next</a></td>
</tr>
</table>
<hr>
</div>
<div lang="en" class="chapter" title="Chapter&nbsp;4.&nbsp;Types, Values, and Variables">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="jls-4"></a>Chapter&nbsp;4.&nbsp;Types, Values, and Variables
</h2>
</div>
</div>
</div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="section"><a href="jls-4.html#jls-4.1">4.1. The Kinds of Types and Values</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.2">4.2. Primitive Types and Values</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-4.html#jls-4.2.1">4.2.1. Integral Types and Values</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.2.2">4.2.2. Integer Operations</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.2.3">4.2.3. Floating-Point Types, Formats, and Values</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.2.4">4.2.4. Floating-Point Operations</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.2.5">4.2.5. The <code class="literal">boolean</code> Type and boolean Values</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-4.html#jls-4.3">4.3. Reference Types and Values</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-4.html#jls-4.3.1">4.3.1. Objects</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.3.2">4.3.2. The Class <code class="literal">Object</code></a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.3.3">4.3.3. The Class <code class="literal">String</code></a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.3.4">4.3.4. When Reference Types Are the Same</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-4.html#jls-4.4">4.4. Type Variables</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.5">4.5. Parameterized Types</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-4.html#jls-4.5.1">4.5.1. Type Arguments of Parameterized Types</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.5.2">4.5.2. Members and Constructors of Parameterized Types</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-4.html#jls-4.6">4.6. Type Erasure</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.7">4.7. Reifiable Types</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.8">4.8. Raw Types</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.9">4.9. Intersection Types</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.10">4.10. Subtyping</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-4.html#jls-4.10.1">4.10.1. Subtyping among Primitive Types</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.10.2">4.10.2. Subtyping among Class and Interface Types</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.10.3">4.10.3. Subtyping among Array Types</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.10.4">4.10.4. Least Upper Bound</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-4.html#jls-4.11">4.11. Where Types Are Used</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.12">4.12. Variables</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-4.html#jls-4.12.1">4.12.1. Variables of Primitive Type</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.12.2">4.12.2. Variables of Reference Type</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.12.3">4.12.3. Kinds of Variables</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.12.4">4.12.4. <code class="literal">final</code> Variables</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.12.5">4.12.5. Initial Values of Variables</a></span></dt>
<dt><span class="section"><a href="jls-4.html#jls-4.12.6">4.12.6. Types, Classes, and Interfaces</a></span></dt>
</dl>
</dd>
</dl>
</div>
<p class="norm"><a name="jls-4-100"></a>The Java programming language is
a <span class="emphasis"><em>statically typed</em></span> language, which means that
every variable and every expression has a type that is known at
compile time.
</p>
<p class="norm"><a name="jls-4-110"></a>The Java programming language is also
a <span class="emphasis"><em>strongly typed</em></span> language, because types limit
the values that a variable (<a class="xref" href="jls-4.html#jls-4.12" title="4.12.&nbsp;Variables">&sect;4.12</a>) can hold or
that an expression can produce, limit the operations supported on
those values, and determine the meaning of the operations. Strong
static typing helps detect errors at compile time.
</p>
<p class="norm"><a name="jls-4-120"></a>The types of the Java programming language are
divided into two categories: primitive types and reference types. The
primitive types (<a class="xref" href="jls-4.html#jls-4.2" title="4.2.&nbsp;Primitive Types and Values">&sect;4.2</a>) are the <code class="literal">boolean</code> type and
the numeric types. The numeric types are the integral types <code class="literal">byte</code>,
<code class="literal">short</code>, <code class="literal">int</code>, <code class="literal">long</code>, and <code class="literal">char</code>, and the floating-point types
<code class="literal">float</code> and <code class="literal">double</code>. The reference types (<a class="xref" href="jls-4.html#jls-4.3" title="4.3.&nbsp;Reference Types and Values">&sect;4.3</a>)
are class types, interface types, and array types. There is also a
special null type. An object (<a class="xref" href="jls-4.html#jls-4.3.1" title="4.3.1.&nbsp;Objects">&sect;4.3.1</a>) is a
dynamically created instance of a class type or a dynamically created
array. The values of a reference type are references to objects. All
objects, including arrays, support the methods of class <code class="literal">Object</code>
(<a class="xref" href="jls-4.html#jls-4.3.2" title="4.3.2.&nbsp;The Class Object">&sect;4.3.2</a>). String literals are represented by
<code class="literal">String</code> objects (<a class="xref" href="jls-4.html#jls-4.3.3" title="4.3.3.&nbsp;The Class String">&sect;4.3.3</a>).
</p>
<div class="section" title="4.1.&nbsp;The Kinds of Types and Values">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-4.1"></a>4.1.&nbsp;The Kinds of Types and Values
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.1-100"></a>There are
two kinds of types in the Java programming language: primitive types
(<a class="xref" href="jls-4.html#jls-4.2" title="4.2.&nbsp;Primitive Types and Values">&sect;4.2</a>) and reference types
(<a class="xref" href="jls-4.html#jls-4.3" title="4.3.&nbsp;Reference Types and Values">&sect;4.3</a>). There are, correspondingly, two kinds of
data values that can be stored in variables, passed as arguments,
returned by methods, and operated on: primitive values
(<a class="xref" href="jls-4.html#jls-4.2" title="4.2.&nbsp;Primitive Types and Values">&sect;4.2</a>) and reference values
(<a class="xref" href="jls-4.html#jls-4.3" title="4.3.&nbsp;Reference Types and Values">&sect;4.3</a>).
</p>
<div id="jls-4.1-110" class="productionset"><a name="jls-4.1-110"></a>
<div class="production"><a name="jls-Type"></a>
<div class="lhs">Type:</div>
<div class="rhs">
<a href="jls-4.html#jls-PrimitiveType" title="PrimitiveType">PrimitiveType</a> <br>
<a href="jls-4.html#jls-ReferenceType" title="ReferenceType">ReferenceType</a>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.1-200"></a>There is
also a special <span class="emphasis"><em>null type</em></span>, the type of the
expression <code class="literal">null</code> (<a class="xref" href="jls-3.html#jls-3.10.7" title="3.10.7.&nbsp;The Null Literal">&sect;3.10.7</a>,
<a class="xref" href="jls-15.html#jls-15.8.1" title="15.8.1.&nbsp;Lexical Literals">&sect;15.8.1</a>), which has no name.
</p>
<p class="norm-static"><a name="jls-4.1-210"></a>Because the
null type has no name, it is impossible to declare a variable of the
null type or to cast to the null type.
</p>
<p class="norm-dynamic"><a name="jls-4.1-220"></a>The null
reference is the only possible value of an expression of null
type.
</p>
<p class="norm-dynamic"><a name="jls-4.1-230"></a>The null
reference can always be assigned or cast to any reference type
(<a class="xref" href="jls-5.html#jls-5.2" title="5.2.&nbsp;Assignment Contexts">&sect;5.2</a>, <a class="xref" href="jls-5.html#jls-5.3" title="5.3.&nbsp;Invocation Contexts">&sect;5.3</a>,
<a class="xref" href="jls-5.html#jls-5.5" title="5.5.&nbsp;Casting Contexts">&sect;5.5</a>).
</p>
<p class="note">In practice, the programmer can ignore the null type
and just pretend that <code class="literal">null</code> is merely a special literal that can be
of any reference type.
</p>
</div>
<div class="section" title="4.2.&nbsp;Primitive Types and Values">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-4.2"></a>4.2.&nbsp;Primitive Types and Values
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.2-100"></a>A primitive
type is predefined by the Java programming language and named by its reserved keyword
(<a class="xref" href="jls-3.html#jls-3.9" title="3.9.&nbsp;Keywords">&sect;3.9</a>):
</p>
<div id="jls-4.2-110" class="productionset"><a name="jls-4.2-110"></a>
<div class="production"><a name="jls-PrimitiveType"></a>
<div class="lhs">PrimitiveType:</div>
<div class="rhs">
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>} <a href="jls-4.html#jls-NumericType" title="NumericType">NumericType</a> <br>
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>} <code class="literal">boolean</code>
</div>
</div>
<div class="production"><a name="jls-NumericType"></a>
<div class="lhs">NumericType:</div>
<div class="rhs">
<a href="jls-4.html#jls-IntegralType" title="IntegralType">IntegralType</a> <br>
<a href="jls-4.html#jls-FloatingPointType" title="FloatingPointType">FloatingPointType</a>
</div>
</div>
<div class="production"><a name="jls-IntegralType"></a>
<div class="lhs">IntegralType:</div>
<div class="rhs">
<code class="literal">byte</code> <code class="literal">short</code> <code class="literal">int</code> <code class="literal">long</code> <code class="literal">char</code>
</div>
</div>
<div class="production"><a name="jls-FloatingPointType"></a>
<div class="lhs">FloatingPointType:</div>
<div class="rhs">
<code class="literal">float</code> <code class="literal">double</code>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-4.2-120"></a>Primitive
values do not share state with other primitive values.
</p>
<p class="norm-static"><a name="jls-4.2-200"></a>The <span class="emphasis"><em>numeric types</em></span> are the
integral types and the floating-point types.
</p>
<p class="norm-static"><a name="jls-4.2-210"></a>The <span class="emphasis"><em>integral types</em></span> are
<code class="literal">byte</code>, <code class="literal">short</code>, <code class="literal">int</code>, and <code class="literal">long</code>, whose values are 8-bit, 16-bit,
32-bit and 64-bit signed two's-complement integers, respectively, and
<code class="literal">char</code>, whose values are 16-bit unsigned integers representing UTF-16
code units (<a class="xref" href="jls-3.html#jls-3.1" title="3.1.&nbsp;Unicode">&sect;3.1</a>).
</p>
<p class="norm-static"><a name="jls-4.2-220"></a>The <span class="emphasis"><em>floating-point types</em></span> are
<code class="literal">float</code>, whose values include the 32-bit IEEE 754 floating-point
numbers, and <code class="literal">double</code>, whose values include the 64-bit IEEE 754
floating-point numbers.
</p>
<p class="norm-dynamic"><a name="jls-4.2-300"></a>The
<code class="literal">boolean</code> type has exactly two values: <code class="literal">true</code> and <code class="literal">false</code>.
</p>
<div class="section" title="4.2.1.&nbsp;Integral Types and Values">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-4.2.1"></a>4.2.1.&nbsp;Integral Types and Values
</h3>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-4.2.1-100"></a>The
values of the integral types are integers in the following
ranges:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-4.2.1-100-A"></a>For
<code class="literal">byte</code>, from -128 to 127, inclusive
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-4.2.1-100-B"></a>For
<code class="literal">short</code>, from -32768 to 32767, inclusive
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-4.2.1-100-C"></a>For
<code class="literal">int</code>, from -2147483648 to 2147483647, inclusive
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-4.2.1-100-D"></a>For
<code class="literal">long</code>, from -9223372036854775808 to 9223372036854775807,
inclusive
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-4.2.1-100-E"></a>For
<code class="literal">char</code>, from <code class="literal">'\u0000'</code>
to <code class="literal">'\uffff'</code> inclusive, that is, from 0 to
65535
</p>
</li>
</ul>
</div>
</div>
<div class="section" title="4.2.2.&nbsp;Integer Operations">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-4.2.2"></a>4.2.2.&nbsp;Integer Operations
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-4.2.2-100"></a>The Java programming language provides a
number of operators that act on integral values:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-A"></a>The comparison
operators, which result in a value of type <code class="literal">boolean</code>:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-A-A"></a>The numerical
comparison operators <code class="literal">&lt;</code>, <code class="literal">&lt;=</code>,
<code class="literal">&gt;</code>, and <code class="literal">&gt;=</code>
(<a class="xref" href="jls-15.html#jls-15.20.1" title="15.20.1.&nbsp;Numerical Comparison Operators <, <=, &gt;, and &gt;=">&sect;15.20.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-A-B"></a>The numerical
equality operators <code class="literal">==</code> and <code class="literal">!=</code>
(<a class="xref" href="jls-15.html#jls-15.21.1" title="15.21.1.&nbsp;Numerical Equality Operators == and !=">&sect;15.21.1</a>)
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-B"></a>The numerical
operators, which result in a value of type <code class="literal">int</code> or <code class="literal">long</code>:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-B-A"></a>The unary plus
and minus operators <code class="literal">+</code> and <code class="literal">-</code>
(<a class="xref" href="jls-15.html#jls-15.15.3" title="15.15.3.&nbsp;Unary Plus Operator +">&sect;15.15.3</a>,
<a class="xref" href="jls-15.html#jls-15.15.4" title="15.15.4.&nbsp;Unary Minus Operator -">&sect;15.15.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-B-B"></a>The
multiplicative operators <code class="literal">*</code>, <code class="literal">/</code>, and <code class="literal">%</code>
(<a class="xref" href="jls-15.html#jls-15.17" title="15.17.&nbsp;Multiplicative Operators">&sect;15.17</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-B-C"></a>The additive
operators <code class="literal">+</code> and <code class="literal">-</code>
(<a class="xref" href="jls-15.html#jls-15.18" title="15.18.&nbsp;Additive Operators">&sect;15.18</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-B-D"></a>The increment
operator <code class="literal">++</code>, both prefix
(<a class="xref" href="jls-15.html#jls-15.15.1" title="15.15.1.&nbsp;Prefix Increment Operator ++">&sect;15.15.1</a>) and postfix
(<a class="xref" href="jls-15.html#jls-15.14.2" title="15.14.2.&nbsp;Postfix Increment Operator ++">&sect;15.14.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-B-E"></a>The decrement
operator <code class="literal">--</code>, both prefix
(<a class="xref" href="jls-15.html#jls-15.15.2" title="15.15.2.&nbsp;Prefix Decrement Operator --">&sect;15.15.2</a>) and postfix
(<a class="xref" href="jls-15.html#jls-15.14.3" title="15.14.3.&nbsp;Postfix Decrement Operator --">&sect;15.14.3</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-B-F"></a>The signed and
unsigned shift operators <code class="literal">&lt;&lt;</code>, <code class="literal">&gt;&gt;</code>, and <code class="literal">&gt;&gt;&gt;</code>
(<a class="xref" href="jls-15.html#jls-15.19" title="15.19.&nbsp;Shift Operators">&sect;15.19</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-B-G"></a>The bitwise
complement operator <code class="literal">~</code>
(<a class="xref" href="jls-15.html#jls-15.15.5" title="15.15.5.&nbsp;Bitwise Complement Operator ~">&sect;15.15.5</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-B-H"></a>The integer
bitwise operators <code class="literal">&amp;</code>, <code class="literal">^</code>, and <code class="literal">|</code>
(<a class="xref" href="jls-15.html#jls-15.22.1" title="15.22.1.&nbsp;Integer Bitwise Operators &amp;, ^, and |">&sect;15.22.1</a>)
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-C"></a>The conditional
operator <code class="literal">? :</code> (<a class="xref" href="jls-15.html#jls-15.25" title="15.25.&nbsp;Conditional Operator ? :">&sect;15.25</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-D"></a>The cast operator
(<a class="xref" href="jls-15.html#jls-15.16" title="15.16.&nbsp;Cast Expressions">&sect;15.16</a>), which can convert from an integral
value to a value of any specified numeric type
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-100-E"></a>The string
concatenation operator <code class="literal">+</code> (<a class="xref" href="jls-15.html#jls-15.18.1" title="15.18.1.&nbsp;String Concatenation Operator +">&sect;15.18.1</a>),
which, when given a <code class="literal">String</code> operand and an integral operand, will
convert the integral operand to a <code class="literal">String</code> representing its value
in decimal form, and then produce a newly created <code class="literal">String</code> that is
the concatenation of the two strings
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-4.2.2-110"></a>Other useful constructors,
methods, and constants are predefined in the classes <code class="literal">Byte</code>, <code class="literal">Short</code>,
<code class="literal">Integer</code>, <code class="literal">Long</code>, and <code class="literal">Character</code>.
</p>
<p class="norm-dynamic"><a name="jls-4.2.2-200"></a>If an
integer operator other than a shift operator has at least one operand
of type <code class="literal">long</code>, then the operation is carried out using 64-bit
precision, and the result of the numerical operator is of type
<code class="literal">long</code>. If the other operand is not <code class="literal">long</code>, it is first widened
(<a class="xref" href="jls-5.html#jls-5.1.5" title="5.1.5.&nbsp;Widening Reference Conversion">&sect;5.1.5</a>) to type <code class="literal">long</code> by numeric promotion
(<a class="xref" href="jls-5.html#jls-5.6" title="5.6.&nbsp;Numeric Contexts">&sect;5.6</a>).
</p>
<p class="norm-dynamic"><a name="jls-4.2.2-210"></a>Otherwise, the operation is carried out using
32-bit precision, and the result of the numerical operator is of type
<code class="literal">int</code>. If either operand is not an <code class="literal">int</code>, it is first widened to type
<code class="literal">int</code> by numeric promotion.
</p>
<p class="norm"><a name="jls-4.2.2-220"></a>Any value of any integral
type may be cast to or from any numeric type. There are no casts
between integral types and the type <code class="literal">boolean</code>.
</p>
<p class="note">See <a class="xref" href="jls-4.html#jls-4.2.5" title="4.2.5.&nbsp;The boolean Type and boolean Values">&sect;4.2.5</a> for an idiom to
convert integer expressions to <code class="literal">boolean</code>.
</p>
<p class="norm-dynamic"><a name="jls-4.2.2-300"></a>The
integer operators do not indicate overflow or
underflow in any way.
</p>
<p class="norm"><a name="jls-4.2.2-310"></a>An integer operator can throw
an exception (<a class="xref" href="jls-11.html" title="Chapter&nbsp;11.&nbsp;Exceptions">&sect;11 (<i>Exceptions</i>)</a>) for the following
reasons:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-310-A"></a>Any integer operator
can throw a <code class="literal">NullPointerException</code> if unboxing conversion
(<a class="xref" href="jls-5.html#jls-5.1.8" title="5.1.8.&nbsp;Unboxing Conversion">&sect;5.1.8</a>) of a null reference is
required.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-310-B"></a>The integer divide
operator <code class="literal">/</code> (<a class="xref" href="jls-15.html#jls-15.17.2" title="15.17.2.&nbsp;Division Operator /">&sect;15.17.2</a>) and the
integer remainder operator <code class="literal">%</code>
(<a class="xref" href="jls-15.html#jls-15.17.3" title="15.17.3.&nbsp;Remainder Operator %">&sect;15.17.3</a>) can throw an
<code class="literal">ArithmeticException</code> if the right-hand operand is zero.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.2-310-C"></a>The increment and
decrement operators <code class="literal">++</code> (<a class="xref" href="jls-15.html#jls-15.14.2" title="15.14.2.&nbsp;Postfix Increment Operator ++">&sect;15.14.2</a>,
<a class="xref" href="jls-15.html#jls-15.15.1" title="15.15.1.&nbsp;Prefix Increment Operator ++">&sect;15.15.1</a>) and <code class="literal">--</code>
(<a class="xref" href="jls-15.html#jls-15.14.3" title="15.14.3.&nbsp;Postfix Decrement Operator --">&sect;15.14.3</a>, <a class="xref" href="jls-15.html#jls-15.15.2" title="15.15.2.&nbsp;Prefix Decrement Operator --">&sect;15.15.2</a>)
can throw an <code class="literal">OutOfMemoryError</code> if boxing conversion
(<a class="xref" href="jls-5.html#jls-5.1.7" title="5.1.7.&nbsp;Boxing Conversion">&sect;5.1.7</a>) is required and there is not
sufficient memory available to perform the conversion.
</p>
</li>
</ul>
</div>
<div class="example"><a name="d5e2024"></a><p class="title"><b>Example&nbsp;4.2.2-1.&nbsp;Integer Operations</b></p>
<div class="example-contents"><pre class="programlisting">
class Test {
public static void main(String[] args) {
int i = 1000000;
System.out.println(i * i);
long l = i;
System.out.println(l * l);
System.out.println(20296 / (l - i));
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
-727379968
1000000000000
</pre><p class="note">and then encounters an <code class="literal">ArithmeticException</code> in the
division by <code class="literal">l - i</code>, because <code class="literal">l - i</code>
is zero. The first multiplication is performed in 32-bit precision,
whereas the second multiplication is a <code class="literal">long</code> multiplication. The
value <code class="literal">-727379968</code> is the decimal value of the low 32
bits of the mathematical result, <code class="literal">1000000000000</code>,
which is a value too large for type <code class="literal">int</code>.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="4.2.3.&nbsp;Floating-Point Types, Formats, and Values">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-4.2.3"></a>4.2.3.&nbsp;Floating-Point Types, Formats, and Values
</h3>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-4.2.3-100"></a>The
floating-point types are <code class="literal">float</code> and <code class="literal">double</code>, which are conceptually
associated with the single-precision 32-bit and double-precision
64-bit format IEEE 754 values and operations as specified
in <em class="citetitle">IEEE Standard for Binary Floating-Point
Arithmetic</em>, ANSI/IEEE Standard 754-1985 (IEEE, New
York).
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-110"></a>The IEEE
754 standard includes not only positive and negative numbers that
consist of a sign and magnitude, but also positive and negative zeros,
positive and negative <span class="emphasis"><em>infinities</em></span>, and
special <span class="emphasis"><em>Not-a-Number</em></span> values (hereafter
abbreviated NaN). A NaN value is used to represent the result of
certain invalid operations such as dividing zero by zero. NaN
constants of both <code class="literal">float</code> and <code class="literal">double</code> type are predefined
as <code class="literal">Float.NaN</code>
and <code class="literal">Double.NaN</code>.
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-120"></a>Every
implementation of the Java programming language is required to support two standard
sets of floating-point values, called the <span class="emphasis"><em>float value
set</em></span> and the <span class="emphasis"><em>double value set</em></span>. In
addition, an implementation of the Java programming language may support either or
both of two extended-exponent floating-point value sets, called
the <span class="emphasis"><em>float-extended-exponent value set</em></span> and
the <span class="emphasis"><em>double-extended-exponent value set</em></span>. These
extended-exponent value sets may, under certain circumstances, be used
instead of the standard value sets to represent the values of
expressions of type <code class="literal">float</code> or <code class="literal">double</code> (<a class="xref" href="jls-5.html#jls-5.1.13" title="5.1.13.&nbsp;Value Set Conversion">&sect;5.1.13</a>,
<a class="xref" href="jls-15.html#jls-15.4" title="15.4.&nbsp;FP-strict Expressions">&sect;15.4</a>).
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-130"></a>The
finite nonzero values of any floating-point value set can all be
expressed in the form <span class="emphasis"><em>s</em></span> &middot; <span class="emphasis"><em>m</em></span>
&middot; 2<sup>(<span class="emphasis"><em>e</em></span> - <span class="emphasis"><em>N</em></span> +
1)</sup>, where <span class="emphasis"><em>s</em></span> is +1 or
-1, <span class="emphasis"><em>m</em></span> is a positive integer less than
2<sup><span class="emphasis"><em>N</em></span></sup>,
and <span class="emphasis"><em>e</em></span> is an integer
between <span class="emphasis"><em>E<sub>min</sub></em></span> =
-(2<sup><span class="emphasis"><em>K</em></span>-1</sup>-2)
and <span class="emphasis"><em>E<sub>max</sub></em></span> =
2<sup><span class="emphasis"><em>K</em></span>-1</sup>-1, inclusive, and
where <span class="emphasis"><em>N</em></span> and <span class="emphasis"><em>K</em></span> are parameters
that depend on the value set. Some values can be represented in this
form in more than one way; for example, supposing that a
value <span class="emphasis"><em>v</em></span> in a value set might be represented in
this form using certain values for
<span class="emphasis"><em>s</em></span>, <span class="emphasis"><em>m</em></span>,
and <span class="emphasis"><em>e</em></span>, then if it happened
that <span class="emphasis"><em>m</em></span> were even and <span class="emphasis"><em>e</em></span> were
less than 2<sup><span class="emphasis"><em>K</em></span>-1</sup>, one
could halve <span class="emphasis"><em>m</em></span> and increase <span class="emphasis"><em>e</em></span>
by 1 to produce a second representation for the same
value <span class="emphasis"><em>v</em></span>. A representation in this form is
called <span class="emphasis"><em>normalized</em></span> if <span class="emphasis"><em>m</em></span> <span class="symbol">&#8805;</span>
2<sup><span class="emphasis"><em>N</em></span>-1</sup>; otherwise the
representation is said to be <span class="emphasis"><em>denormalized</em></span>. If a
value in a value set cannot be represented in such a way
that <span class="emphasis"><em>m</em></span> <span class="symbol">&#8805;</span>
2<sup><span class="emphasis"><em>N</em></span>-1</sup>, then the value
is said to be a <span class="emphasis"><em>denormalized value</em></span>, because it
has no normalized representation.
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-140"></a>The
constraints on the parameters <span class="emphasis"><em>N</em></span>
and <span class="emphasis"><em>K</em></span> (and on the derived
parameters <span class="emphasis"><em>E<sub>min</sub></em></span>
and <span class="emphasis"><em>E<sub>max</sub></em></span>) for the two
required and two optional floating-point value sets are summarized in
<a class="xref" href="jls-4.html#jls-4.2.3-140-A" title="Table&nbsp;4.1.&nbsp;Floating-point value set parameters">Table&nbsp;4.1</a>.
</p>
<div class="table"><a name="jls-4.2.3-140-A"></a><p class="title"><b>Table&nbsp;4.1.&nbsp;Floating-point value set parameters</b></p>
<div class="table-contents">
<table summary="Floating-point value set parameters" border="1">
<colgroup>
<col>
<col>
<col>
<col>
<col>
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>float</th>
<th>float-extended-exponent</th>
<th>double</th>
<th>double-extended-exponent</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="emphasis"><em>N</em></span></td>
<td>24</td>
<td>24</td>
<td>53</td>
<td>53</td>
</tr>
<tr>
<td><span class="emphasis"><em>K</em></span></td>
<td>8</td>
<td><span class="symbol">&#8805;</span> 11
</td>
<td>11</td>
<td><span class="symbol">&#8805;</span> 15
</td>
</tr>
<tr>
<td><span class="emphasis"><em>E<sub>max</sub></em></span></td>
<td>+127</td>
<td><span class="symbol">&#8805;</span> +1023
</td>
<td>+1023</td>
<td><span class="symbol">&#8805;</span> +16383
</td>
</tr>
<tr>
<td><span class="emphasis"><em>E<sub>min</sub></em></span></td>
<td>-126</td>
<td><span class="symbol">&#8804;</span> -1022
</td>
<td>-1022</td>
<td><span class="symbol">&#8804;</span> -16382
</td>
</tr>
</tbody>
</table>
</div>
</div><br class="table-break"><p class="norm-dynamic"><a name="jls-4.2.3-150"></a>Where one
or both extended-exponent value sets are supported by an
implementation, then for each supported extended-exponent value set
there is a specific implementation-dependent
constant <span class="emphasis"><em>K</em></span>, whose value is constrained by
<a class="xref" href="jls-4.html#jls-4.2.3-140-A" title="Table&nbsp;4.1.&nbsp;Floating-point value set parameters">Table&nbsp;4.1</a>; this value <span class="emphasis"><em>K</em></span>
in turn dictates the values
for <span class="emphasis"><em>E<sub>min</sub></em></span>
and <span class="emphasis"><em>E<sub>max</sub></em></span>.
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-160"></a>Each of
the four value sets includes not only the finite nonzero values that
are ascribed to it above, but also NaN values and the four values
positive zero, negative zero, positive infinity, and negative
infinity.
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-170"></a>Note that
the constraints in <a class="xref" href="jls-4.html#jls-4.2.3-140-A" title="Table&nbsp;4.1.&nbsp;Floating-point value set parameters">Table&nbsp;4.1</a> are designed so
that every element of the float value set is necessarily also an
element of the float-extended-exponent value set, the double value
set, and the double-extended-exponent value set. Likewise, each
element of the double value set is necessarily also an element of the
double-extended-exponent value set. Each extended-exponent value set
has a larger range of exponent values than the corresponding standard
value set, but does not have more precision.
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-180"></a>The
elements of the float value set are exactly the values that can be
represented using the single floating-point format defined in the IEEE
754 standard. The elements of the double value set are exactly the
values that can be represented using the double floating-point format
defined in the IEEE 754 standard. Note, however, that the elements of
the float-extended-exponent and double-extended-exponent value sets
defined here do <span class="emphasis"><em>not</em></span> correspond to the values that
can be represented using IEEE 754 single extended and double extended
formats, respectively.
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-190"></a>The
float, float-extended-exponent, double, and double-extended-exponent
value sets are not types. It is always correct for an implementation
of the Java programming language to use an element of the float value set to
represent a value of type <code class="literal">float</code>; however, it may be permissible in
certain regions of code for an implementation to use an element of the
float-extended-exponent value set instead. Similarly, it is always
correct for an implementation to use an element of the double value
set to represent a value of type <code class="literal">double</code>; however, it may be
permissible in certain regions of code for an implementation to use an
element of the double-extended-exponent value set instead.
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-200"></a>Except
for NaN, floating-point values are <span class="emphasis"><em>ordered</em></span>;
arranged from smallest to largest, they are negative infinity,
negative finite nonzero values, positive and negative zero, positive
finite nonzero values, and positive infinity.
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-210"></a>IEEE 754
allows multiple distinct NaN values for each of its single and double
floating-point formats. While each hardware architecture returns a
particular bit pattern for NaN when a new NaN is generated, a
programmer can also create NaNs with different bit patterns to encode,
for example, retrospective diagnostic information.
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-220"></a>For the
most part, the Java SE platform treats NaN values of a given type as though
collapsed into a single canonical value, and hence this specification
normally refers to an arbitrary NaN as though to a canonical
value.
</p>
<p class="note">However, version 1.3 of the Java SE platform introduced
methods enabling the programmer to distinguish between NaN values:
the <code class="literal">Float.floatToRawIntBits</code>
and <code class="literal">Double.doubleToRawLongBits</code> methods. The
interested reader is referred to the specifications for the <code class="literal">Float</code>
and <code class="literal">Double</code> classes for more information.
</p>
<p class="norm-dynamic"><a name="jls-4.2.3-230"></a>Positive
zero and negative zero compare equal; thus the result of the
expression <code class="literal">0.0==-0.0</code> is <code class="literal">true</code> and the result
of <code class="literal">0.0&gt;-0.0</code> is false. But other operations can
distinguish positive and negative zero; for
example, <code class="literal">1.0/0.0</code> has the value positive infinity,
while the value of <code class="literal">1.0/-0.0</code> is negative
infinity.
</p>
<p class="norm"><a name="jls-4.2.3-240"></a>NaN
is <span class="emphasis"><em>unordered</em></span>, so:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-4.2.3-240-A"></a>The numerical
comparison
operators <code class="literal">&lt;</code>, <code class="literal">&lt;=</code>,
<code class="literal">&gt;</code>, and <code class="literal">&gt;=</code> return
<code class="literal">false</code> if either or both operands are NaN
(<a class="xref" href="jls-15.html#jls-15.20.1" title="15.20.1.&nbsp;Numerical Comparison Operators <, <=, &gt;, and &gt;=">&sect;15.20.1</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.3-240-B"></a>The equality
operator <code class="literal">==</code> returns <code class="literal">false</code> if either operand
is NaN.
</p>
<p class="norm"><a name="jls-4.2.3-240-B.1"></a>In
particular, <code class="literal">(x&lt;y) == !(x&gt;=y)</code> will be
<code class="literal">false</code> if <code class="literal">x</code> or <code class="literal">y</code> is
NaN.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.3-240-C"></a>The inequality
operator <code class="literal">!=</code> returns <code class="literal">true</code> if either operand
is NaN (<a class="xref" href="jls-15.html#jls-15.21.1" title="15.21.1.&nbsp;Numerical Equality Operators == and !=">&sect;15.21.1</a>).
</p>
<p class="norm"><a name="jls-4.2.3-240-C.1"></a>In
particular, <code class="literal">x!=x</code> is <code class="literal">true</code> if and only
if <code class="literal">x</code> is NaN.
</p>
</li>
</ul>
</div>
</div>
<div class="section" title="4.2.4.&nbsp;Floating-Point Operations">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-4.2.4"></a>4.2.4.&nbsp;Floating-Point Operations
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-4.2.4-100"></a>The Java programming language provides a
number of operators that act on floating-point values:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-A"></a>The comparison
operators, which result in a value of type <code class="literal">boolean</code>:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-A-A"></a>The numerical
comparison operators <code class="literal">&lt;</code>, <code class="literal">&lt;=</code>,
<code class="literal">&gt;</code>, and <code class="literal">&gt;=</code>
(<a class="xref" href="jls-15.html#jls-15.20.1" title="15.20.1.&nbsp;Numerical Comparison Operators <, <=, &gt;, and &gt;=">&sect;15.20.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-A-B"></a>The numerical
equality operators <code class="literal">==</code> and <code class="literal">!=</code>
(<a class="xref" href="jls-15.html#jls-15.21.1" title="15.21.1.&nbsp;Numerical Equality Operators == and !=">&sect;15.21.1</a>)
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-B"></a>The numerical
operators, which result in a value of type <code class="literal">float</code> or
<code class="literal">double</code>:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-B-A"></a>The unary plus
and minus operators <code class="literal">+</code> and <code class="literal">-</code>
(<a class="xref" href="jls-15.html#jls-15.15.3" title="15.15.3.&nbsp;Unary Plus Operator +">&sect;15.15.3</a>,
<a class="xref" href="jls-15.html#jls-15.15.4" title="15.15.4.&nbsp;Unary Minus Operator -">&sect;15.15.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-B-B"></a>The
multiplicative operators <code class="literal">*</code>, <code class="literal">/</code>, and <code class="literal">%</code>
(<a class="xref" href="jls-15.html#jls-15.17" title="15.17.&nbsp;Multiplicative Operators">&sect;15.17</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-B-C"></a>The additive
operators <code class="literal">+</code> and <code class="literal">-</code>
(<a class="xref" href="jls-15.html#jls-15.18.2" title="15.18.2.&nbsp;Additive Operators (+ and -) for Numeric Types">&sect;15.18.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-B-D"></a>The increment
operator <code class="literal">++</code>, both prefix
(<a class="xref" href="jls-15.html#jls-15.15.1" title="15.15.1.&nbsp;Prefix Increment Operator ++">&sect;15.15.1</a>) and postfix
(<a class="xref" href="jls-15.html#jls-15.14.2" title="15.14.2.&nbsp;Postfix Increment Operator ++">&sect;15.14.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-B-E"></a>The decrement
operator <code class="literal">--</code>, both prefix
(<a class="xref" href="jls-15.html#jls-15.15.2" title="15.15.2.&nbsp;Prefix Decrement Operator --">&sect;15.15.2</a>) and postfix
(<a class="xref" href="jls-15.html#jls-15.14.3" title="15.14.3.&nbsp;Postfix Decrement Operator --">&sect;15.14.3</a>)
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-C"></a>The conditional
operator <code class="literal">? :</code> (<a class="xref" href="jls-15.html#jls-15.25" title="15.25.&nbsp;Conditional Operator ? :">&sect;15.25</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-D"></a>The cast operator
(<a class="xref" href="jls-15.html#jls-15.16" title="15.16.&nbsp;Cast Expressions">&sect;15.16</a>), which can convert from a
floating-point value to a value of any specified numeric
type
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-100-E"></a>The string
concatenation operator <code class="literal">+</code> (<a class="xref" href="jls-15.html#jls-15.18.1" title="15.18.1.&nbsp;String Concatenation Operator +">&sect;15.18.1</a>),
which, when given a <code class="literal">String</code> operand and a floating-point operand,
will convert the floating-point operand to a <code class="literal">String</code> representing
its value in decimal form (without information loss), and then
produce a newly created <code class="literal">String</code> by concatenating the two
strings
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-4.2.4-110"></a>Other useful constructors,
methods, and constants are predefined in the classes <code class="literal">Float</code>,
<code class="literal">Double</code>, and <code class="literal">Math</code>.
</p>
<p class="norm-dynamic"><a name="jls-4.2.4-200"></a>If at
least one of the operands to a binary operator is of floating-point
type, then the operation is a floating-point operation, even if the
other is integral.
</p>
<p class="norm-dynamic"><a name="jls-4.2.4-210"></a>If at
least one of the operands to a numerical operator is of type <code class="literal">double</code>,
then the operation is carried out using 64-bit floating-point
arithmetic, and the result of the numerical operator is a value of
type <code class="literal">double</code>. If the other operand is not a <code class="literal">double</code>, it is first
widened (<a class="xref" href="jls-5.html#jls-5.1.5" title="5.1.5.&nbsp;Widening Reference Conversion">&sect;5.1.5</a>) to type <code class="literal">double</code> by numeric
promotion (<a class="xref" href="jls-5.html#jls-5.6" title="5.6.&nbsp;Numeric Contexts">&sect;5.6</a>).
</p>
<p class="norm-dynamic"><a name="jls-4.2.4-220"></a>Otherwise, the operation is carried out using
32-bit floating-point arithmetic, and the result of the numerical
operator is a value of type <code class="literal">float</code>. (If the other operand is not a
<code class="literal">float</code>, it is first widened to type <code class="literal">float</code> by numeric
promotion.)
</p>
<p class="norm"><a name="jls-4.2.4-230"></a>Any value of a floating-point
type may be cast to or from any numeric type. There are no casts
between floating-point types and the type <code class="literal">boolean</code>.
</p>
<p class="note">See <a class="xref" href="jls-4.html#jls-4.2.5" title="4.2.5.&nbsp;The boolean Type and boolean Values">&sect;4.2.5</a> for an idiom to
convert floating-point expressions to <code class="literal">boolean</code>.
</p>
<p class="norm-dynamic"><a name="jls-4.2.4-300"></a>Operators
on floating-point numbers behave as specified by IEEE 754 (with the
exception of the remainder operator
(<a class="xref" href="jls-15.html#jls-15.17.3" title="15.17.3.&nbsp;Remainder Operator %">&sect;15.17.3</a>)). In particular, the Java programming language
requires support of IEEE 754 <span class="emphasis"><em>denormalized</em></span>
floating-point numbers and <span class="emphasis"><em>gradual underflow</em></span>,
which make it easier to prove desirable properties of particular
numerical algorithms. Floating-point operations do not "flush to zero"
if the calculated result is a denormalized number.
</p>
<p class="norm-dynamic"><a name="jls-4.2.4-310"></a>The
Java programming language requires that floating-point arithmetic behave as if every
floating-point operator rounded its floating-point result to the
result precision. <span class="emphasis"><em>Inexact</em></span> results must be rounded
to the representable value nearest to the infinitely precise result;
if the two nearest representable values are equally near, the one with
its least significant bit zero is chosen. This is the IEEE 754
standard's default rounding mode known as <span class="emphasis"><em>round to
nearest</em></span>.
</p>
<p class="norm-dynamic"><a name="jls-4.2.4-320"></a>The
Java programming language uses <span class="emphasis"><em>round toward zero</em></span> when converting
a floating value to an integer (<a class="xref" href="jls-5.html#jls-5.1.3" title="5.1.3.&nbsp;Narrowing Primitive Conversion">&sect;5.1.3</a>), which
acts, in this case, as though the number were truncated, discarding
the mantissa bits. Rounding toward zero chooses at its result the
format's value closest to and no greater in magnitude than the
infinitely precise result.
</p>
<p class="norm-dynamic"><a name="jls-4.2.4-330"></a>A
floating-point operation that overflows produces a signed
infinity.
</p>
<p class="norm-dynamic"><a name="jls-4.2.4-340"></a>A
floating-point operation that underflows produces a denormalized value
or a signed zero.
</p>
<p class="norm-dynamic"><a name="jls-4.2.4-350"></a>A
floating-point operation that has no mathematically definite result
produces NaN.
</p>
<p class="norm"><a name="jls-4.2.4-360"></a>All numeric operations with
NaN as an operand produce NaN as a result.
</p>
<p class="norm"><a name="jls-4.2.4-370"></a>A floating-point operator can
throw an exception (<a class="xref" href="jls-11.html" title="Chapter&nbsp;11.&nbsp;Exceptions">&sect;11 (<i>Exceptions</i>)</a>) for the following
reasons:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-370-A"></a>Any floating-point
operator can throw a <code class="literal">NullPointerException</code> if unboxing conversion
(<a class="xref" href="jls-5.html#jls-5.1.8" title="5.1.8.&nbsp;Unboxing Conversion">&sect;5.1.8</a>) of a null reference is
required.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.4-370-B"></a>The increment and
decrement operators <code class="literal">++</code> (<a class="xref" href="jls-15.html#jls-15.14.2" title="15.14.2.&nbsp;Postfix Increment Operator ++">&sect;15.14.2</a>,
<a class="xref" href="jls-15.html#jls-15.15.1" title="15.15.1.&nbsp;Prefix Increment Operator ++">&sect;15.15.1</a>) and <code class="literal">--</code>
(<a class="xref" href="jls-15.html#jls-15.14.3" title="15.14.3.&nbsp;Postfix Decrement Operator --">&sect;15.14.3</a>, <a class="xref" href="jls-15.html#jls-15.15.2" title="15.15.2.&nbsp;Prefix Decrement Operator --">&sect;15.15.2</a>)
can throw an <code class="literal">OutOfMemoryError</code> if boxing conversion
(<a class="xref" href="jls-5.html#jls-5.1.7" title="5.1.7.&nbsp;Boxing Conversion">&sect;5.1.7</a>) is required and there is not
sufficient memory available to perform the conversion.
</p>
</li>
</ul>
</div>
<div class="example"><a name="d5e2335"></a><p class="title"><b>Example&nbsp;4.2.4-1.&nbsp;Floating-point Operations</b></p>
<div class="example-contents"><pre class="programlisting">
class Test {
public static void main(String[] args) {
// An example of overflow:
double d = 1e308;
System.out.print("overflow produces infinity: ");
System.out.println(d + "*10==" + d*10);
// An example of gradual underflow:
d = 1e-305 * Math.PI;
System.out.print("gradual underflow: " + d + "\n ");
for (int i = 0; i &lt; 4; i++)
System.out.print(" " + (d /= 100000));
System.out.println();
// An example of NaN:
System.out.print("0.0/0.0 is Not-a-Number: ");
d = 0.0/0.0;
System.out.println(d);
// An example of inexact results and rounding:
System.out.print("inexact results with float:");
for (int i = 0; i &lt; 100; i++) {
float z = 1.0f / i;
if (z * i != 1.0f)
System.out.print(" " + i);
}
System.out.println();
// Another example of inexact results and rounding:
System.out.print("inexact results with double:");
for (int i = 0; i &lt; 100; i++) {
double z = 1.0 / i;
if (z * i != 1.0)
System.out.print(" " + i);
}
System.out.println();
// An example of cast to integer rounding:
System.out.print("cast to int rounds toward 0: ");
d = 12345.6;
System.out.println((int)d + " " + (int)(-d));
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
overflow produces infinity: 1.0e+308*10==Infinity
gradual underflow: 3.141592653589793E-305
3.1415926535898E-310 3.141592653E-315 3.142E-320 0.0
0.0/0.0 is Not-a-Number: NaN
inexact results with float: 0 41 47 55 61 82 83 94 97
inexact results with double: 0 49 98
cast to int rounds toward 0: 12345 -12345
</pre><p class="note">This example demonstrates, among other things, that
gradual underflow can result in a gradual loss of precision.
</p>
<p class="note">The results when <code class="literal">i</code> is <code class="literal">0</code>
involve division by zero, so that <code class="literal">z</code> becomes
positive infinity, and <code class="literal">z * 0</code> is NaN, which is not
equal to <code class="literal">1.0</code>.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="4.2.5.&nbsp;The boolean Type and boolean Values">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-4.2.5"></a>4.2.5.&nbsp;The <code class="literal">boolean</code> Type and boolean Values
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.2.5-100"></a>The
<code class="literal">boolean</code> type represents a logical quantity with two possible values,
indicated by the literals <code class="literal">true</code> and <code class="literal">false</code>
(<a class="xref" href="jls-3.html#jls-3.10.3" title="3.10.3.&nbsp;Boolean Literals">&sect;3.10.3</a>).
</p>
<p class="norm"><a name="jls-4.2.5-110"></a>The boolean operators
are:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-4.2.5-110-A"></a>The relational
operators <code class="literal">==</code> and <code class="literal">!=</code>
(<a class="xref" href="jls-15.html#jls-15.21.2" title="15.21.2.&nbsp;Boolean Equality Operators == and !=">&sect;15.21.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.5-110-B"></a>The logical complement
operator <code class="literal">!</code> (<a class="xref" href="jls-15.html#jls-15.15.6" title="15.15.6.&nbsp;Logical Complement Operator !">&sect;15.15.6</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.5-110-C"></a>The logical operators
<code class="literal">&amp;</code>, <code class="literal">^</code>, and <code class="literal">|</code> (<a class="xref" href="jls-15.html#jls-15.22.2" title="15.22.2.&nbsp;Boolean Logical Operators &amp;, ^, and |">&sect;15.22.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.5-110-D"></a>The conditional-and and
conditional-or operators <code class="literal">&amp;&amp;</code> (<a class="xref" href="jls-15.html#jls-15.23" title="15.23.&nbsp;Conditional-And Operator &amp;&amp;">&sect;15.23</a>)
and <code class="literal">||</code> (<a class="xref" href="jls-15.html#jls-15.24" title="15.24.&nbsp;Conditional-Or Operator ||">&sect;15.24</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.5-110-E"></a>The conditional
operator <code class="literal">? :</code> (<a class="xref" href="jls-15.html#jls-15.25" title="15.25.&nbsp;Conditional Operator ? :">&sect;15.25</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.5-110-F"></a>The string
concatenation operator <code class="literal">+</code> (<a class="xref" href="jls-15.html#jls-15.18.1" title="15.18.1.&nbsp;String Concatenation Operator +">&sect;15.18.1</a>),
which, when given a <code class="literal">String</code> operand and a <code class="literal">boolean</code> operand, will
convert the <code class="literal">boolean</code> operand to a <code class="literal">String</code>
(either <code class="literal">"true"</code> or <code class="literal">"false"</code>),
and then produce a newly created <code class="literal">String</code> that is the
concatenation of the two strings
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-4.2.5-120"></a>Boolean expressions determine
the control flow in several kinds of statements:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-4.2.5-120-A"></a>The <code class="literal">if</code> statement
(<a class="xref" href="jls-14.html#jls-14.9" title="14.9.&nbsp;The if Statement">&sect;14.9</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.5-120-B"></a>The <code class="literal">while</code> statement
(<a class="xref" href="jls-14.html#jls-14.12" title="14.12.&nbsp;The while Statement">&sect;14.12</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.5-120-C"></a>The <code class="literal">do</code> statement
(<a class="xref" href="jls-14.html#jls-14.13" title="14.13.&nbsp;The do Statement">&sect;14.13</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.2.5-120-D"></a>The <code class="literal">for</code> statement
(<a class="xref" href="jls-14.html#jls-14.14" title="14.14.&nbsp;The for Statement">&sect;14.14</a>)
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-4.2.5-130"></a>A <code class="literal">boolean</code> expression also
determines which subexpression is evaluated in the conditional
<code class="literal">? :</code> operator (<a class="xref" href="jls-15.html#jls-15.25" title="15.25.&nbsp;Conditional Operator ? :">&sect;15.25</a>).
</p>
<p class="norm"><a name="jls-4.2.5-140"></a>Only <code class="literal">boolean</code> and <code class="literal">Boolean</code>
expressions can be used in control flow statements and as the first
operand of the conditional operator <code class="literal">? :</code>.
</p>
<p class="norm-static"><a name="jls-4.2.5-200"></a>An integer
or floating-point expression <code class="literal">x</code>
can be converted to a <code class="literal">boolean</code> value, following the C language
convention that any nonzero value is <code class="literal">true</code>, by the
expression <code class="literal">x!=0</code>.
</p>
<p class="norm-static"><a name="jls-4.2.5-210"></a>An object
reference <code class="literal">obj</code> can be converted to a <code class="literal">boolean</code>
value, following the C language convention that any reference other
than <code class="literal">null</code> is <code class="literal">true</code>, by the
expression <code class="literal">obj!=null</code>.
</p>
<p class="norm"><a name="jls-4.2.5-220"></a>A <code class="literal">boolean</code> value can be
converted to a <code class="literal">String</code> by string conversion
(<a class="xref" href="jls-5.html#jls-5.4" title="5.4.&nbsp;String Contexts">&sect;5.4</a>).
</p>
<p class="norm"><a name="jls-4.2.5-230"></a>A <code class="literal">boolean</code> value may be cast
to type <code class="literal">boolean</code>, <code class="literal">Boolean</code>, or <code class="literal">Object</code> (<a class="xref" href="jls-5.html#jls-5.5" title="5.5.&nbsp;Casting Contexts">&sect;5.5</a>).
No other casts on type <code class="literal">boolean</code> are allowed.
</p>
</div>
</div>
<div class="section" title="4.3.&nbsp;Reference Types and Values">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-4.3"></a>4.3.&nbsp;Reference Types and Values
</h2>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-4.3-100"></a>There are
four kinds of <span class="emphasis"><em>reference types</em></span>: class types
(<a class="xref" href="jls-8.html#jls-8.1" title="8.1.&nbsp;Class Declarations">&sect;8.1</a>), interface types
(<a class="xref" href="jls-9.html#jls-9.1" title="9.1.&nbsp;Interface Declarations">&sect;9.1</a>), type variables
(<a class="xref" href="jls-4.html#jls-4.4" title="4.4.&nbsp;Type Variables">&sect;4.4</a>), and array types
(<a class="xref" href="jls-10.html#jls-10.1" title="10.1.&nbsp;Array Types">&sect;10.1</a>).
</p>
<div id="jls-4.3-110a" class="productionset"><a name="jls-4.3-110a"></a>
<div class="production"><a name="jls-ReferenceType"></a>
<div class="lhs">ReferenceType:</div>
<div class="rhs">
<a href="jls-4.html#jls-ClassOrInterfaceType" title="ClassOrInterfaceType">ClassOrInterfaceType</a> <br>
<a href="jls-4.html#jls-TypeVariable" title="TypeVariable">TypeVariable</a> <br>
<a href="jls-4.html#jls-ArrayType" title="ArrayType">ArrayType</a>
</div>
</div>
<div class="production"><a name="jls-ClassOrInterfaceType"></a>
<div class="lhs">ClassOrInterfaceType:</div>
<div class="rhs">
<a href="jls-4.html#jls-ClassType" title="ClassType">ClassType</a> <br>
<a href="jls-4.html#jls-InterfaceType" title="InterfaceType">InterfaceType</a>
</div>
</div>
<div class="production"><a name="jls-ClassType"></a>
<div class="lhs">ClassType:</div>
<div class="rhs">
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>}
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
[<a href="jls-4.html#jls-TypeArguments" title="TypeArguments">TypeArguments</a>] <br>
<a href="jls-4.html#jls-ClassOrInterfaceType" title="ClassOrInterfaceType">ClassOrInterfaceType</a> <code class="literal">.</code>
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>}
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
[<a href="jls-4.html#jls-TypeArguments" title="TypeArguments">TypeArguments</a>]
</div>
</div>
<div class="production"><a name="jls-InterfaceType"></a>
<div class="lhs">InterfaceType:</div>
<div class="rhs">
<a href="jls-4.html#jls-ClassType" title="ClassType">ClassType</a>
</div>
</div>
<div class="production"><a name="jls-TypeVariable"></a>
<div class="lhs">TypeVariable:</div>
<div class="rhs">
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>} <a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
</div>
</div>
<div class="production"><a name="jls-ArrayType"></a>
<div class="lhs">ArrayType:</div>
<div class="rhs">
<a href="jls-4.html#jls-PrimitiveType" title="PrimitiveType">PrimitiveType</a> <a href="jls-4.html#jls-Dims" title="Dims">Dims</a> <br>
<a href="jls-4.html#jls-ClassOrInterfaceType" title="ClassOrInterfaceType">ClassOrInterfaceType</a> <a href="jls-4.html#jls-Dims" title="Dims">Dims</a> <br>
<a href="jls-4.html#jls-TypeVariable" title="TypeVariable">TypeVariable</a> <a href="jls-4.html#jls-Dims" title="Dims">Dims</a>
</div>
</div>
<div class="production"><a name="jls-Dims"></a>
<div class="lhs">Dims:</div>
<div class="rhs">
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>} <code class="literal">[</code> <code class="literal">]</code> {{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>} <code class="literal">[</code> <code class="literal">]</code>}
</div>
</div>
</div>
<div class="informalexample">
<p class="note">The sample code:</p><pre class="programlisting">
class Point { int[] metrics; }
interface Move { void move(int deltax, int deltay); }
</pre><p class="note">declares a class type
<code class="literal">Point</code>, an interface type <code class="literal">Move</code>,
and uses an array type <code class="literal">int</code><code class="literal">[]</code> (an array of <code class="literal">int</code>) to declare
the field <code class="literal">metrics</code> of the
class <code class="literal">Point</code>.
</p>
</div>
<p class="norm-static"><a name="jls-4.3-200"></a>A class or
interface type consists of an identifier or a dotted sequence of
identifiers, where each identifier is optionally followed by type
arguments (<a class="xref" href="jls-4.html#jls-4.5.1" title="4.5.1.&nbsp;Type Arguments of Parameterized Types">&sect;4.5.1</a>). If type arguments appear
anywhere in a class or interface type, it is a parameterized type
(<a class="xref" href="jls-4.html#jls-4.5" title="4.5.&nbsp;Parameterized Types">&sect;4.5</a>).
</p>
<p class="norm-static"><a name="jls-4.3-210"></a>Each identifier in
a class or interface type is classified as a package name or a type
name (<a class="xref" href="jls-6.html#jls-6.5.1" title="6.5.1.&nbsp;Syntactic Classification of a Name According to Context">&sect;6.5.1</a>). Identifiers which are classified
as type names may be annotated. If a class or interface type has the
form <code class="varname">T.id</code> (optionally followed by type arguments),
then <code class="varname">id</code> must be the simple name of an accessible
member type of <code class="varname">T</code> (<a class="xref" href="jls-6.html#jls-6.6" title="6.6.&nbsp;Access Control">&sect;6.6</a>,
<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>, <a class="xref" href="jls-9.html#jls-9.5" title="9.5.&nbsp;Member Type Declarations">&sect;9.5</a>), or a
compile-time error occurs. The class or interface type denotes that
member type.
</p>
<div class="section" title="4.3.1.&nbsp;Objects">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-4.3.1"></a>4.3.1.&nbsp;Objects
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-4.3.1-100"></a>An
<span class="emphasis"><em>object</em></span> is a <span class="emphasis"><em>class instance</em></span>
or an <span class="emphasis"><em>array</em></span>.
</p>
<p class="norm"><a name="jls-4.3.1-110"></a>The reference values (often
just <span class="emphasis"><em>references</em></span>) are pointers to these objects,
and a special null reference, which refers to no object.
</p>
<p class="norm"><a name="jls-4.3.1-120"></a>A class instance is
explicitly created by a class instance creation expression
(<a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>).
</p>
<p class="norm"><a name="jls-4.3.1-130"></a>An array is explicitly
created by an array creation expression
(<a class="xref" href="jls-15.html#jls-15.10.1" title="15.10.1.&nbsp;Array Creation Expressions">&sect;15.10.1</a>).
</p>
<p class="norm"><a name="jls-4.3.1-140"></a>A new class instance is
implicitly created when the string concatenation operator <code class="literal">+</code>
(<a class="xref" href="jls-15.html#jls-15.18.1" title="15.18.1.&nbsp;String Concatenation Operator +">&sect;15.18.1</a>) is used in a non-constant expression
(<a class="xref" href="jls-15.html#jls-15.28" title="15.28.&nbsp;Constant Expressions">&sect;15.28</a>), resulting in a new object of type
<code class="literal">String</code> (<a class="xref" href="jls-4.html#jls-4.3.3" title="4.3.3.&nbsp;The Class String">&sect;4.3.3</a>).
</p>
<p class="norm"><a name="jls-4.3.1-150"></a>A new array object is
implicitly created when an array initializer expression
(<a class="xref" href="jls-10.html#jls-10.6" title="10.6.&nbsp;Array Initializers">&sect;10.6</a>) is evaluated; this can occur when a class
or interface is initialized (<a class="xref" href="jls-12.html#jls-12.4" title="12.4.&nbsp;Initialization of Classes and Interfaces">&sect;12.4</a>), when a new
instance of a class is created (<a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>), or when a
local variable declaration statement is executed
(<a class="xref" href="jls-14.html#jls-14.4" title="14.4.&nbsp;Local Variable Declaration Statements">&sect;14.4</a>).
</p>
<p class="norm"><a name="jls-4.3.1-160"></a>New objects of the types
<code class="literal">Boolean</code>, <code class="literal">Byte</code>, <code class="literal">Short</code>, <code class="literal">Character</code>, <code class="literal">Integer</code>, <code class="literal">Long</code>, <code class="literal">Float</code>,
and <code class="literal">Double</code> may be implicitly created by boxing conversion
(<a class="xref" href="jls-5.html#jls-5.1.7" title="5.1.7.&nbsp;Boxing Conversion">&sect;5.1.7</a>).
</p>
<div class="example"><a name="d5e2554"></a><p class="title"><b>Example&nbsp;4.3.1-1.&nbsp;Object Creation</b></p>
<div class="example-contents"><pre class="programlisting">
class Point {
int x, y;
Point() { System.out.println("default"); }
Point(int x, int y) { this.x = x; this.y = y; }
/* A Point instance is explicitly created at
class initialization time: */
static Point origin = new Point(0,0);
/* A String can be implicitly created
by a + operator: */
public String toString() { return "(" + x + "," + y + ")"; }
}
class Test {
public static void main(String[] args) {
/* A Point is explicitly created
using newInstance: */
Point p = null;
try {
p = (Point)Class.forName("Point").newInstance();
} catch (Exception e) {
System.out.println(e);
}
/* An array is implicitly created
by an array constructor: */
Point a[] = { new Point(0,0), new Point(1,1) };
/* Strings are implicitly created
by + operators: */
System.out.println("p: " + p);
System.out.println("a: { " + a[0] + ", " + a[1] + " }");
/* An array is explicitly created
by an array creation expression: */
String sa[] = new String[2];
sa[0] = "he"; sa[1] = "llo";
System.out.println(sa[0] + sa[1]);
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
default
p: (0,0)
a: { (0,0), (1,1) }
hello
</pre></div>
</div><br class="example-break"><p class="norm"><a name="jls-4.3.1-200"></a>The operators on references
to objects are:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-4.3.1-200-A"></a>Field access, using
either a qualified name (<a class="xref" href="jls-6.html#jls-6.6" title="6.6.&nbsp;Access Control">&sect;6.6</a>) or a field
access expression (<a class="xref" href="jls-15.html#jls-15.11" title="15.11.&nbsp;Field Access Expressions">&sect;15.11</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.1-200-B"></a>Method invocation
(<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.1-200-C"></a>The cast operator
(<a class="xref" href="jls-5.html#jls-5.5" title="5.5.&nbsp;Casting Contexts">&sect;5.5</a>, <a class="xref" href="jls-15.html#jls-15.16" title="15.16.&nbsp;Cast Expressions">&sect;15.16</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.1-200-D"></a>The string
concatenation operator <code class="literal">+</code> (<a class="xref" href="jls-15.html#jls-15.18.1" title="15.18.1.&nbsp;String Concatenation Operator +">&sect;15.18.1</a>),
which, when given a <code class="literal">String</code> operand and a reference, will convert
the reference to a <code class="literal">String</code> by invoking the <code class="literal">toString</code> method of
the referenced object (using <code class="literal">"null"</code> if either
the reference or the result of <code class="literal">toString</code> is a null reference),
and then will produce a newly created <code class="literal">String</code> that is the
concatenation of the two strings
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.1-200-E"></a>The <code class="literal">instanceof</code>
operator (<a class="xref" href="jls-15.html#jls-15.20.2" title="15.20.2.&nbsp;Type Comparison Operator instanceof">&sect;15.20.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.1-200-F"></a>The reference equality
operators <code class="literal">==</code> and <code class="literal">!=</code>
(<a class="xref" href="jls-15.html#jls-15.21.3" title="15.21.3.&nbsp;Reference Equality Operators == and !=">&sect;15.21.3</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.1-200-G"></a>The conditional
operator <code class="literal">? :</code> (<a class="xref" href="jls-15.html#jls-15.25" title="15.25.&nbsp;Conditional Operator ? :">&sect;15.25</a>).
</p>
</li>
</ul>
</div>
<p class="norm-dynamic"><a name="jls-4.3.1-300"></a>There may
be many references to the same object. Most objects have state, stored
in the fields of objects that are instances of classes or in the
variables that are the components of an array object. If two variables
contain references to the same object, the state of the object can be
modified using one variable's reference to the object, and then the
altered state can be observed through the reference in the other
variable.
</p>
<div class="example"><a name="d5e2596"></a><p class="title"><b>Example&nbsp;4.3.1-2.&nbsp;Primitive and Reference Identity</b></p>
<div class="example-contents"><pre class="programlisting">
class Value { int val; }
class Test {
public static void main(String[] args) {
int i1 = 3;
int i2 = i1;
i2 = 4;
System.out.print("i1==" + i1);
System.out.println(" but i2==" + i2);
Value v1 = new Value();
v1.val = 5;
Value v2 = v1;
v2.val = 6;
System.out.print("v1.val==" + v1.val);
System.out.println(" and v2.val==" + v2.val);
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
i1==3 but i2==4
v1.val==6 and v2.val==6
</pre><p class="note">because <code class="literal">v1.val</code>
and <code class="literal">v2.val</code> reference the same instance variable
(<a class="xref" href="jls-4.html#jls-4.12.3" title="4.12.3.&nbsp;Kinds of Variables">&sect;4.12.3</a>) in the one <code class="literal">Value</code>
object created by the only <code class="literal">new</code> expression,
while <code class="literal">i1</code> and <code class="literal">i2</code> are different
variables.
</p>
</div>
</div><br class="example-break"><p class="norm"><a name="jls-4.3.1-400"></a>Each object is associated
with a monitor (<a class="xref" href="jls-17.html#jls-17.1" title="17.1.&nbsp;Synchronization">&sect;17.1</a>), which is used
by <code class="literal">synchronized</code> methods (<a class="xref" href="jls-8.html#jls-8.4.3" title="8.4.3.&nbsp;Method Modifiers">&sect;8.4.3</a>) and the
<code class="literal">synchronized</code> statement (<a class="xref" href="jls-14.html#jls-14.19" title="14.19.&nbsp;The synchronized Statement">&sect;14.19</a>) to provide
control over concurrent access to state by multiple threads
(<a class="xref" href="jls-17.html" title="Chapter&nbsp;17.&nbsp;Threads and Locks">&sect;17 (<i>Threads and Locks</i>)</a>).
</p>
</div>
<div class="section" title="4.3.2.&nbsp;The Class Object">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-4.3.2"></a>4.3.2.&nbsp;The Class <code class="literal">Object</code></h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.3.2-100"></a>The class
<code class="literal">Object</code> is a superclass (<a class="xref" href="jls-8.html#jls-8.1.4" title="8.1.4.&nbsp;Superclasses and Subclasses">&sect;8.1.4</a>) of all other
classes.
</p>
<p class="norm"><a name="jls-4.3.2-200"></a>All class and array types
inherit (<a class="xref" href="jls-8.html#jls-8.4.8" title="8.4.8.&nbsp;Inheritance, Overriding, and Hiding">&sect;8.4.8</a>) the methods of class <code class="literal">Object</code>,
which are summarized as follows:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-4.3.2-200-A"></a>The
method <code class="literal">clone</code> is used to make a duplicate of
an object.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.2-200-B"></a>The
method <code class="literal">equals</code> defines a notion of object
equality, which is based on value, not reference,
comparison.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.2-200-C"></a>The
method <code class="literal">finalize</code> is run just before an object
is destroyed (<a class="xref" href="jls-12.html#jls-12.6" title="12.6.&nbsp;Finalization of Class Instances">&sect;12.6</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.2-200-D"></a>The
method <code class="literal">getClass</code> returns the <code class="literal">Class</code> object
that represents the class of the object.
</p>
<p class="norm"><a name="jls-4.3.2-200-D.1"></a>A <code class="literal">Class</code> object
exists for each reference type. It can be used, for example, to
discover the fully qualified name of a class, its members, its
immediate superclass, and any interfaces that it
implements.
</p>
<p class="norm-static"><a name="jls-4.3.2-200-D.2"></a>The type of a method
invocation expression of <code class="literal">getClass</code> is
<code class="literal">Class</code><code class="literal">&lt;</code><code class="literal">?</code> <code class="literal">extends</code> |<span class="type">T</span>|<code class="literal">&gt;</code> where <span class="type">T</span> is the
class or interface searched (<a class="xref" href="jls-15.html#jls-15.12.1" title="15.12.1.&nbsp;Compile-Time Step 1: Determine Class or Interface to Search">&sect;15.12.1</a>)
for <code class="literal">getClass</code>.
</p>
<p class="norm"><a name="jls-4.3.2-200-D.3"></a>A class method that
is declared <code class="literal">synchronized</code> (<a class="xref" href="jls-8.html#jls-8.4.3.6" title="8.4.3.6.&nbsp;synchronized Methods">&sect;8.4.3.6</a>)
synchronizes on the monitor associated with the <code class="literal">Class</code> object
of the class.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.2-200-E"></a>The
method <code class="literal">hashCode</code> is very useful, together with
the method <code class="literal">equals</code>, in hashtables such
as <code class="literal">java.util.Hashmap</code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.2-200-F"></a>The
methods <code class="literal">wait</code>, <code class="literal">notify</code>,
and <code class="literal">notifyAll</code> are used in concurrent
programming using threads (<a class="xref" href="jls-17.html#jls-17.2" title="17.2.&nbsp;Wait Sets and Notification">&sect;17.2</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-4.3.2-200-G"></a>The method <code class="literal">toString</code>
returns a <code class="literal">String</code> representation of the object.
</p>
</li>
</ul>
</div>
</div>
<div class="section" title="4.3.3.&nbsp;The Class String">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-4.3.3"></a>4.3.3.&nbsp;The Class <code class="literal">String</code></h3>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-4.3.3-100"></a>Instances
of class <code class="literal">String</code> represent sequences of Unicode
code points.
</p>
<p class="norm-dynamic"><a name="jls-4.3.3-110"></a>A
<code class="literal">String</code> object has a constant (unchanging) value.
</p>
<p class="norm-dynamic"><a name="jls-4.3.3-120"></a>String
literals (<a class="xref" href="jls-3.html#jls-3.10.5" title="3.10.5.&nbsp;String Literals">&sect;3.10.5</a>) are references to instances of
class <code class="literal">String</code>.
</p>
<p class="norm"><a name="jls-4.3.3-130"></a>The string concatenation
operator <code class="literal">+</code> (<a class="xref" href="jls-15.html#jls-15.18.1" title="15.18.1.&nbsp;String Concatenation Operator +">&sect;15.18.1</a>) implicitly creates a
new <code class="literal">String</code> object when the result is not a constant expression
(<a class="xref" href="jls-15.html#jls-15.28" title="15.28.&nbsp;Constant Expressions">&sect;15.28</a>).
</p>
</div>
<div class="section" title="4.3.4.&nbsp;When Reference Types Are the Same">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-4.3.4"></a>4.3.4.&nbsp;When Reference Types Are the Same
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.3.4-100"></a>Two
reference types are the <span class="emphasis"><em>same compile-time type</em></span> if
they have the same binary name (<a class="xref" href="jls-13.html#jls-13.1" title="13.1.&nbsp;The Form of a Binary">&sect;13.1</a>) and their
type arguments, if any, are the
same, applying this definition recursively.
</p>
<p class="norm"><a name="jls-4.3.4-110"></a>When two reference types are
the same, they are sometimes said to be the <span class="emphasis"><em>same
class</em></span> or the <span class="emphasis"><em>same interface</em></span>.
</p>
<p class="norm-dynamic"><a name="jls-4.3.4-200"></a>At run
time, several reference types with the same binary name may be loaded
simultaneously by different class loaders. These types may or may not
represent the same type declaration. Even if two such types do
represent the same type declaration, they are considered
distinct.
</p>
<p class="norm-dynamic"><a name="jls-4.3.4-300"></a>Two
reference types are the <span class="emphasis"><em>same run-time type</em></span>
if:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-4.3.4-310"></a>They
are both class or both interface types, are defined by the same
class loader, and have the same binary name
(<a class="xref" href="jls-13.html#jls-13.1" title="13.1.&nbsp;The Form of a Binary">&sect;13.1</a>), in which case they are sometimes
said to be the <span class="emphasis"><em>same run-time class</em></span> or
the <span class="emphasis"><em>same run-time interface</em></span>.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-4.3.4-320"></a>They
are both array types, and their component types are the same
run-time type (<a class="xref" href="jls-10.html" title="Chapter&nbsp;10.&nbsp;Arrays">&sect;10 (<i>Arrays</i>)</a>).
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="section" title="4.4.&nbsp;Type Variables">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-4.4"></a>4.4.&nbsp;Type Variables
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-4.4-100"></a>A <span class="emphasis"><em>type
variable</em></span> is an unqualified identifier used as a type in
class, interface, method, and constructor bodies.
</p>
<p class="norm-static"><a name="jls-4.4-110"></a>A type
variable is introduced by the declaration of a <span class="emphasis"><em>type
parameter</em></span> of a generic class, interface, method, or
constructor (<a class="xref" href="jls-8.html#jls-8.1.2" title="8.1.2.&nbsp;Generic Classes and Type Parameters">&sect;8.1.2</a>, <a class="xref" href="jls-9.html#jls-9.1.2" title="9.1.2.&nbsp;Generic Interfaces and Type Parameters">&sect;9.1.2</a>,
<a class="xref" href="jls-8.html#jls-8.4.4" title="8.4.4.&nbsp;Generic Methods">&sect;8.4.4</a>, <a class="xref" href="jls-8.html#jls-8.8.4" title="8.8.4.&nbsp;Generic Constructors">&sect;8.8.4</a>).
</p>
<div id="jls-4.4-120" class="productionset"><a name="jls-4.4-120"></a>
<div class="production"><a name="jls-TypeParameter"></a>
<div class="lhs">TypeParameter:</div>
<div class="rhs">
{<a href="jls-4.html#jls-TypeParameterModifier" title="TypeParameterModifier">TypeParameterModifier</a>}
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
[<a href="jls-4.html#jls-TypeBound" title="TypeBound">TypeBound</a>]
</div>
</div>
<div class="production"><a name="jls-TypeParameterModifier"></a>
<div class="lhs">TypeParameterModifier:</div>
<div class="rhs">
<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>
</div>
</div>
<div class="production"><a name="jls-TypeBound"></a>
<div class="lhs">TypeBound:</div>
<div class="rhs">
<code class="literal">extends</code> <a href="jls-4.html#jls-TypeVariable" title="TypeVariable">TypeVariable</a> <br>
<code class="literal">extends</code> <a href="jls-4.html#jls-ClassOrInterfaceType" title="ClassOrInterfaceType">ClassOrInterfaceType</a> {<a href="jls-4.html#jls-AdditionalBound" title="AdditionalBound">AdditionalBound</a>}
</div>
</div>
<div class="production"><a name="jls-AdditionalBound"></a>
<div class="lhs">AdditionalBound:</div>
<div class="rhs">
<code class="literal">&amp;</code> <a href="jls-4.html#jls-InterfaceType" title="InterfaceType">InterfaceType</a>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.4-130"></a>The scope of
a type variable declared as a type parameter is specified in
<a class="xref" href="jls-6.html#jls-6.3" title="6.3.&nbsp;Scope of a Declaration">&sect;6.3</a>.
</p>
<p class="norm-static"><a name="jls-4.4-200"></a>Every type variable declared as a
type parameter has a <span class="emphasis"><em>bound</em></span>. If no bound is
declared for a type variable, <code class="literal">Object</code> is assumed. If a bound is
declared, it consists of either:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-4.4-200-A"></a>a
single type variable <span class="type">T</span>, or
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.4-200-B"></a>a
class or interface type <span class="type">T</span> possibly followed by interface types
<span class="type">I<sub>1</sub></span> <code class="literal">&amp;</code> ... <code class="literal">&amp;</code> <span class="type">I<sub>n</sub></span>.
</p>
</li>
</ul>
</div>
<p class="norm-error"><a name="jls-4.4-210"></a>It is a
compile-time error if any of the types <span class="type">I<sub>1</sub></span> ... <span class="type">I<sub>n</sub></span> is a class type
or type variable.
</p>
<p class="norm-error"><a name="jls-4.4-220"></a>The erasures
(<a class="xref" href="jls-4.html#jls-4.6" title="4.6.&nbsp;Type Erasure">&sect;4.6</a>) of all constituent types of a bound must
be pairwise different, or a compile-time error occurs.
</p>
<p class="norm-error"><a name="jls-4.4-230"></a>A type
variable must not at the same time be a subtype of two interface types
which are different parameterizations of the same generic interface,
or a compile-time error occurs.
</p>
<p class="norm-static"><a name="jls-4.4-240"></a>The order of
types in a bound is only significant in that the erasure of a type
variable is determined by the first type in its bound, and that a
class type or type variable may only appear in the first
position.
</p>
<p class="norm-static"><a name="jls-4.4-300"></a>The members
of a type variable <span class="type">X</span> with bound <span class="type">T</span> <code class="literal">&amp;</code> <span class="type">I<sub>1</sub></span> <code class="literal">&amp;</code> ... <code class="literal">&amp;</code> <span class="type">I<sub>n</sub></span>
are the members of the intersection type (<a class="xref" href="jls-4.html#jls-4.9" title="4.9.&nbsp;Intersection Types">&sect;4.9</a>)
<span class="type">T</span> <code class="literal">&amp;</code> <span class="type">I<sub>1</sub></span> <code class="literal">&amp;</code> ... <code class="literal">&amp;</code> <span class="type">I<sub>n</sub></span> appearing at the point where the
type variable is declared.
</p>
<div class="example"><a name="d5e2787"></a><p class="title"><b>Example&nbsp;4.4-1.&nbsp;Members of a Type Variable</b></p>
<div class="example-contents"><pre class="programlisting">
package TypeVarMembers;
class C {
public void mCPublic() {}
protected void mCProtected() {}
void mCPackage() {}
private void mCPrivate() {}
}
interface I {
void mI();
}
class CT extends C implements I {
public void mI() {}
}
class Test {
&lt;T extends C &amp; I&gt; void test(T t) {
t.mI(); // OK
t.mCPublic(); // OK
t.mCProtected(); // OK
t.mCPackage(); // OK
t.mCPrivate(); // Compile-time error
}
}
</pre><p class="note">The type variable <code class="literal">T</code> has the same
members as the intersection type <code class="literal">C &amp; I</code>, which
in turn has the same members as the empty class <code class="literal">CT</code>,
defined in the same scope with equivalent supertypes. The members of
an interface are always <code class="literal">public</code>, and therefore always inherited
(unless overridden). Hence <code class="literal">mI</code> is a member
of <code class="literal">CT</code> and of <code class="literal">T</code>. Among the
members of <code class="literal">C</code>, all but <code class="literal">mCPrivate</code>
are inherited by <code class="literal">CT</code>, and are therefore members of
both <code class="literal">CT</code> and <code class="literal">T</code>.
</p>
<p class="note">If <code class="literal">C</code> had been declared in a
different package than <code class="literal">T</code>, then the call
to <code class="literal">mCPackage</code> would give rise to a compile-time
error, as that member would not be accessible at the point
where <code class="literal">T</code> is declared.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="4.5.&nbsp;Parameterized Types">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-4.5"></a>4.5.&nbsp;Parameterized Types
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.5-100"></a>A class or
interface declaration that is generic (<a class="xref" href="jls-8.html#jls-8.1.2" title="8.1.2.&nbsp;Generic Classes and Type Parameters">&sect;8.1.2</a>,
<a class="xref" href="jls-9.html#jls-9.1.2" title="9.1.2.&nbsp;Generic Interfaces and Type Parameters">&sect;9.1.2</a>) defines a set of <span class="emphasis"><em>parameterized
types</em></span>.
</p>
<p class="norm-static"><a name="jls-4.5-110"></a>A
parameterized type is a class or interface type of the form
<span class="type">C</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code>, where <span class="type">C</span> is the name of a generic
type and <code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code> is a list of type arguments
that denote a particular <span class="emphasis"><em>parameterization</em></span> of the
generic type.
</p>
<p class="norm-static"><a name="jls-4.5-120"></a>A generic
type has type parameters <span class="type">F<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span> with corresponding bounds
<span class="type">B<sub>1</sub></span>,...,<span class="type">B<sub>n</sub></span>. Each type argument <span class="type">T<sub>i</sub></span> of a parameterized type ranges
over all types that are subtypes of all types listed in the
corresponding bound. That is, for each bound type <span class="type">S</span> in <span class="type">B<sub>i</sub></span>, <span class="type">T<sub>i</sub></span> is
a subtype of <span class="type">S</span><code class="literal">[<span class="type">F<sub>1</sub></span>:=<span class="type">T<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span>:=<span class="type">T<sub>n</sub></span>]</code>
(<a class="xref" href="jls-4.html#jls-4.10" title="4.10.&nbsp;Subtyping">&sect;4.10</a>).
</p>
<p class="norm-static"><a name="jls-4.5-200"></a>A
parameterized type <span class="type">C</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code>
is <span class="emphasis"><em>well-formed</em></span> if all of the following are
true:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-4.5-200-A"></a>
<span class="type">C</span> is the name of a generic type.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5-200-B"></a>
The number of type arguments is the same as the number of type
parameters in the generic declaration of <span class="type">C</span>.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5-200-C"></a>
When subjected to capture conversion
(<a class="xref" href="jls-5.html#jls-5.1.10" title="5.1.10.&nbsp;Capture Conversion">&sect;5.1.10</a>) resulting in the type
<span class="type">C</span><code class="literal">&lt;</code><span class="type">X<sub>1</sub></span>,...,<span class="type">X<sub>n</sub></span><code class="literal">&gt;</code>, each type argument <span class="type">X<sub>i</sub></span> is a
subtype of <span class="type">S</span><code class="literal">[<span class="type">F<sub>1</sub></span>:=<span class="type">X<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span>:=<span class="type">X<sub>n</sub></span>]</code> for
each bound type <span class="type">S</span> in <span class="type">B<sub>i</sub></span>.
</p>
</li>
</ul>
</div>
<p class="norm-error"><a name="jls-4.5-210"></a>It is a
compile-time error if a parameterized type is not well-formed.
</p>
<p class="norm"><a name="jls-4.5-220"></a>In this specification, whenever
we speak of a class or interface type, we include the generic version
as well, unless explicitly excluded.
</p>
<p class="norm-static"><a name="jls-4.5-400"></a>Two
parameterized types are <span class="emphasis"><em>provably distinct</em></span> if
either of the following is true:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-4.5-400-A"></a>
They are parameterizations of distinct generic type
declarations.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5-400-B"></a>
Any of their type arguments are provably distinct.
</p>
</li>
</ul>
</div>
<div class="informalexample">
<p class="note">Given the generic types in the examples of
<a class="xref" href="jls-8.html#jls-8.1.2" title="8.1.2.&nbsp;Generic Classes and Type Parameters">&sect;8.1.2</a>, here are some well-formed parameterized
types:
</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note"><code class="literal">Seq&lt;String&gt;</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">Seq&lt;Seq&lt;String&gt;&gt;</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">Seq&lt;String&gt;.Zipper&lt;Integer&gt;</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">Pair&lt;String,Integer&gt;</code></p>
</li>
</ul>
</div>
<p class="note">Here are some incorrect parameterizations of those generic
types:
</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note"><code class="literal">Seq&lt;int&gt;</code> is illegal, as
primitive types cannot be type arguments.
</p>
</li>
<li class="listitem">
<p class="note"><code class="literal">Pair&lt;String&gt;</code> is
illegal, as there are not enough type arguments.
</p>
</li>
<li class="listitem">
<p class="note"><code class="literal">Pair&lt;String,String,String&gt;</code>
is illegal, as there are too many type arguments.
</p>
</li>
</ul>
</div>
</div>
<p class="note">A parameterized type may be an parameterization of a
generic class or interface which is nested. For example, if a
non-generic class <span class="type">C</span> has a generic member class
<span class="type">D</span><code class="literal">&lt;</code><span class="type">T</span><code class="literal">&gt;</code>, then <span class="type">C</span><code class="literal">.</code><span class="type">D</span><code class="literal">&lt;</code><code class="literal">Object</code><code class="literal">&gt;</code> is a
parameterized type. And if a generic class <span class="type">C</span><code class="literal">&lt;</code><span class="type">T</span><code class="literal">&gt;</code> has
a non-generic member class <span class="type">D</span>, then the member type
<span class="type">C</span><code class="literal">&lt;</code><code class="literal">String</code><code class="literal">&gt;</code><code class="literal">.</code><span class="type">D</span> is a parameterized type, even
though the class <span class="type">D</span> is not generic.
</p>
<div class="section" title="4.5.1.&nbsp;Type Arguments of Parameterized Types">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-4.5.1"></a>4.5.1.&nbsp;Type Arguments of Parameterized Types
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.5.1-100"></a>Type
arguments may be either reference types or wildcards. Wildcards are
useful in situations where only partial knowledge about the type
parameter is required.
</p>
<div id="jls-4.5.1-110" class="productionset"><a name="jls-4.5.1-110"></a>
<div class="production"><a name="jls-TypeArguments"></a>
<div class="lhs">TypeArguments:</div>
<div class="rhs">
<code class="literal">&lt;</code> <a href="jls-4.html#jls-TypeArgumentList" title="TypeArgumentList">TypeArgumentList</a> <code class="literal">&gt;</code>
</div>
</div>
<div class="production"><a name="jls-TypeArgumentList"></a>
<div class="lhs">TypeArgumentList:</div>
<div class="rhs">
<a href="jls-4.html#jls-TypeArgument" title="TypeArgument">TypeArgument</a> {<code class="literal">,</code> <a href="jls-4.html#jls-TypeArgument" title="TypeArgument">TypeArgument</a>}
</div>
</div>
<div class="production"><a name="jls-TypeArgument"></a>
<div class="lhs">TypeArgument:</div>
<div class="rhs">
<a href="jls-4.html#jls-ReferenceType" title="ReferenceType">ReferenceType</a> <br>
<a href="jls-4.html#jls-Wildcard" title="Wildcard">Wildcard</a>
</div>
</div>
<div class="production"><a name="jls-Wildcard"></a>
<div class="lhs">Wildcard:</div>
<div class="rhs">
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>} <code class="literal">?</code> [<a href="jls-4.html#jls-WildcardBounds" title="WildcardBounds">WildcardBounds</a>]
</div>
</div>
<div class="production"><a name="jls-WildcardBounds"></a>
<div class="lhs">WildcardBounds:</div>
<div class="rhs">
<code class="literal">extends</code> <a href="jls-4.html#jls-ReferenceType" title="ReferenceType">ReferenceType</a> <br>
<code class="literal">super</code> <a href="jls-4.html#jls-ReferenceType" title="ReferenceType">ReferenceType</a>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.5.1-200"></a>Wildcards
may be given explicit bounds, just like regular type variable
declarations. An upper bound is signified by the following syntax,
where <span class="type">B</span> is the bound:
</p><pre class="screen">? extends <span class="type">B</span></pre><p class="norm-static"><a name="jls-4.5.1-210"></a>Unlike
ordinary type variables declared in a method signature, no type
inference is required when using a wildcard. Consequently, it is
permissible to declare lower bounds on a wildcard, using the following
syntax, where <span class="type">B</span> is a lower bound:
</p><pre class="screen">? super <span class="type">B</span></pre><p class="norm-static"><a name="jls-4.5.1-220"></a>The wildcard
<code class="literal">?</code> <code class="literal">extends</code> <code class="literal">Object</code> is equivalent to the unbounded wildcard
<code class="literal">?</code>.
</p>
<p class="norm-static"><a name="jls-4.5.1-300"></a>Two type
arguments are <span class="emphasis"><em>provably distinct</em></span> if one of the
following is true:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.1-300-A"></a>Neither argument is a type variable or
wildcard, and the two arguments are not the same type.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.1-300-B"></a>One type argument is a type
variable or wildcard, with an upper bound (from capture conversion
(<a class="xref" href="jls-5.html#jls-5.1.10" title="5.1.10.&nbsp;Capture Conversion">&sect;5.1.10</a>), if necessary) of <span class="type">S</span>; and the
other type argument <span class="type">T</span> is not a type variable or wildcard; and
neither |<span class="type">S</span>| <code class="literal">&lt;:</code> |<span class="type">T</span>| nor |<span class="type">T</span>| <code class="literal">&lt;:</code> |<span class="type">S</span>|
(<a class="xref" href="jls-4.html#jls-4.8" title="4.8.&nbsp;Raw Types">&sect;4.8</a>, <a class="xref" href="jls-4.html#jls-4.10" title="4.10.&nbsp;Subtyping">&sect;4.10</a>).
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.1-300-C"></a>Each type argument is a type
variable or wildcard, with upper bounds (from capture conversion,
if necessary) of <span class="type">S</span> and <span class="type">T</span>; and neither |<span class="type">S</span>| <code class="literal">&lt;:</code> |<span class="type">T</span>|
nor |<span class="type">T</span>| <code class="literal">&lt;:</code> |<span class="type">S</span>|.
</p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-4.5.1-400"></a>A type argument
<span class="type">T<sub>1</sub></span> is said to <span class="emphasis"><em>contain</em></span> another type argument
<span class="type">T<sub>2</sub></span>, written <span class="type">T<sub>2</sub></span> <code class="literal">&lt;=</code> <span class="type">T<sub>1</sub></span>, if the set of types denoted by
<span class="type">T<sub>2</sub></span> is provably a subset of the set of types denoted by <span class="type">T<sub>1</sub></span>
under the reflexive and transitive closure of the
following rules (where <code class="literal">&lt;:</code> denotes subtyping
(<a class="xref" href="jls-4.html#jls-4.10" title="4.10.&nbsp;Subtyping">&sect;4.10</a>)):
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.1-400-A"></a>
<code class="literal">?</code> <code class="literal">extends</code> <span class="type">T</span> <code class="literal">&lt;=</code> <code class="literal">?</code> <code class="literal">extends</code> <span class="type">S</span> if <span class="type">T</span> <code class="literal">&lt;:</code> <span class="type">S</span></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.1-400-B"></a>
<code class="literal">?</code> <code class="literal">extends</code> <span class="type">T</span> <code class="literal">&lt;=</code> <code class="literal">?</code></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.1-400-C"></a>
<code class="literal">?</code> <code class="literal">super</code> <span class="type">T</span> <code class="literal">&lt;=</code> <code class="literal">?</code> <code class="literal">super</code> <span class="type">S</span> if <span class="type">S</span> <code class="literal">&lt;:</code> <span class="type">T</span></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.1-400-D"></a>
<code class="literal">?</code> <code class="literal">super</code> <span class="type">T</span> <code class="literal">&lt;=</code> <code class="literal">?</code></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.1-400-E"></a>
<code class="literal">?</code> <code class="literal">super</code> <span class="type">T</span> <code class="literal">&lt;=</code> <code class="literal">?</code> <code class="literal">extends</code> <code class="literal">Object</code></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.1-400-F"></a>
<span class="type">T</span> <code class="literal">&lt;=</code> <span class="type">T</span></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.1-400-G"></a>
<span class="type">T</span> <code class="literal">&lt;=</code> <code class="literal">?</code> <code class="literal">extends</code> <span class="type">T</span></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.1-400-H"></a>
<span class="type">T</span> <code class="literal">&lt;=</code> <code class="literal">?</code> <code class="literal">super</code> <span class="type">T</span></p>
</li>
</ul>
</div>
<p class="note">The relationship of wildcards to established type
theory is an interesting one, which we briefly allude to
here. Wildcards are a restricted form of existential types. Given a
generic type declaration <span class="type">G</span><code class="literal">&lt;</code><span class="type">T</span> <code class="literal">extends</code> <span class="type">B</span><code class="literal">&gt;</code>,
<span class="type">G</span><code class="literal">&lt;</code><code class="literal">?</code><code class="literal">&gt;</code> is roughly analogous to <span class="type">Some <span class="type">X</span>
<code class="literal">&lt;:</code> <span class="type">B</span>. <span class="type">G</span><code class="literal">&lt;</code><span class="type">X</span><code class="literal">&gt;</code></span>.
</p>
<p class="note">Historically, wildcards are a direct descendant of
the work by Atsushi Igarashi and Mirko Viroli. Readers interested in a
more comprehensive discussion should refer to <em class="citetitle">On
Variance-Based Subtyping for Parametric Types</em> by Atsushi
Igarashi and Mirko Viroli, in the
<em class="citetitle">Proceedings of the 16th European Conference on Object
Oriented Programming (ECOOP 2002)</em>. This work itself builds
upon earlier work by Kresten Thorup and Mads Torgersen
(<em class="citetitle">Unifying Genericity</em>, ECOOP 99), as well as a
long tradition of work on declaration based variance that goes back to
Pierre America's work on POOL (OOPSLA 89).
</p>
<p class="note">Wildcards differ in certain details from the
constructs described in the aforementioned paper, in particular in the
use of capture conversion (<a class="xref" href="jls-5.html#jls-5.1.10" title="5.1.10.&nbsp;Capture Conversion">&sect;5.1.10</a>) rather than
the <code class="literal">close</code> operation described by Igarashi and
Viroli. For a formal account of wildcards, see <em class="citetitle">Wild
FJ</em> by Mads Torgersen, Erik Ernst and Christian Plesner
Hansen, in the 12th workshop on Foundations of Object Oriented
Programming (FOOL 2005).
</p>
<div class="example"><a name="d5e3144"></a><p class="title"><b>Example&nbsp;4.5.1-1.&nbsp;Unbounded Wildcards</b></p>
<div class="example-contents"><pre class="programlisting">
import java.util.Collection;
import java.util.ArrayList;
class Test {
static void printCollection(Collection&lt;?&gt; c) {
// a wildcard collection
for (Object o : c) {
System.out.println(o);
}
}
public static void main(String[] args) {
Collection&lt;String&gt; cs = new ArrayList&lt;String&gt;();
cs.add("hello");
cs.add("world");
printCollection(cs);
}
}
</pre><p class="note">Note that using
<code class="literal">Collection&lt;Object&gt;</code> as the type of the
incoming parameter, <code class="literal">c</code>, would not be nearly as
useful; the method could only be used with an argument expression that
had type <code class="literal">Collection&lt;Object&gt;</code>, which would be
quite rare. In contrast, the use of an unbounded wildcard allows any
kind of collection to be passed as an argument.
</p>
<p class="note">Here is an example where the element type of an
array is parameterized by a wildcard:
</p><pre class="programlisting">
public Method getMethod(Class&lt;?&gt;[] parameterTypes) { ... }
</pre></div>
</div><br class="example-break"><div class="example"><a name="d5e3153"></a><p class="title"><b>Example&nbsp;4.5.1-2.&nbsp;Bounded Wildcards</b></p>
<div class="example-contents"><pre class="screen">boolean addAll(Collection&lt;? extends E&gt; c)</pre><p class="note">Here, the method is declared within the
interface <code class="literal">Collection&lt;E&gt;</code>, and is designed to
add all the elements of its incoming argument to the collection upon
which it is invoked. A natural tendency would be to use
<code class="literal">Collection&lt;E&gt;</code> as the type
of <code class="literal">c</code>, but this is unnecessarily restrictive. An
alternative would be to declare the method itself to be
generic:
</p><pre class="screen">&lt;T&gt; boolean addAll(Collection&lt;T&gt; c)</pre><p class="note">This version is sufficiently flexible, but note that
the type parameter is used only once in the signature. This reflects
the fact that the type parameter is not being used to express any kind
of interdependency between the type(s) of the argument(s), the return
type and/or throws type. In the absence of such interdependency,
generic methods are considered bad style, and wildcards are
preferred.
</p><pre class="screen">Reference(T referent, ReferenceQueue&lt;? super T&gt; queue)</pre><p class="note">Here, the referent can be inserted into any queue
whose element type is a supertype of the type <code class="literal">T</code> of
the referent; <code class="literal">T</code> is the lower bound for the
wildcard.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="4.5.2.&nbsp;Members and Constructors of Parameterized Types">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-4.5.2"></a>4.5.2.&nbsp;Members and Constructors of Parameterized Types
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.5.2-100"></a>Let <span class="type">C</span> be
a generic class or interface declaration with type parameters
<span class="type">A<sub>1</sub></span>,...,<span class="type">A<sub>n</sub></span>, and let <span class="type">C</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code> be a
parameterization of <span class="type">C</span> where, for 1 <span class="symbol">&#8804;</span> <span class="emphasis"><em>i</em></span> <span class="symbol">&#8804;</span> <span class="emphasis"><em>n</em></span>, <span class="type">T<sub>i</sub></span>
is a type (rather than a wildcard). Then:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.2-100-A"></a>Let
<code class="varname">m</code> be a member or constructor declaration in <span class="type">C</span>, whose type as
declared is <span class="type">T</span> (<a class="xref" href="jls-8.html#jls-8.2" title="8.2.&nbsp;Class Members">&sect;8.2</a>,
<a class="xref" href="jls-8.html#jls-8.8.6" title="8.8.6.&nbsp;The Type of a Constructor">&sect;8.8.6</a>).
</p>
<p class="norm-static"><a name="jls-4.5.2-100-A.1"></a>The type of <code class="varname">m</code> in
<span class="type">C</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code> is
<span class="type">T</span><code class="literal">[<span class="type">A<sub>1</sub></span>:=<span class="type">T<sub>1</sub></span>,...,<span class="type">A<sub>n</sub></span>:=<span class="type">T<sub>n</sub></span>]</code>.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.2-100-B"></a>Let <code class="varname">m</code> be a member or
constructor declaration in <span class="type">D</span>, where <span class="type">D</span> is a class extended by
<span class="type">C</span> or an interface implemented by <span class="type">C</span>. Let
<span class="type">D</span><code class="literal">&lt;</code><span class="type">U<sub>1</sub></span>,...,<span class="type">U<sub>k</sub></span><code class="literal">&gt;</code> be the supertype of
<span class="type">C</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code> that corresponds to <span class="type">D</span>.
</p>
<p class="norm-static"><a name="jls-4.5.2-100-B.1"></a>The type of <code class="varname">m</code> in
<span class="type">C</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code> is the type of <code class="varname">m</code> in
<span class="type">D</span><code class="literal">&lt;</code><span class="type">U<sub>1</sub></span>,...,<span class="type">U<sub>k</sub></span><code class="literal">&gt;</code>.
</p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-4.5.2-200"></a>If any of
the type arguments in the parameterization of <span class="type">C</span> are wildcards,
then:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.2-200-A"></a>The
types of the fields, methods, and constructors in
<span class="type">C</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code> are
the types of the fields, methods, and constructors in the
capture conversion of <span class="type">C</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code>
(<a class="xref" href="jls-5.html#jls-5.1.10" title="5.1.10.&nbsp;Capture Conversion">&sect;5.1.10</a>).
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.5.2-200-B"></a>Let <span class="type">D</span> be a (possibly
generic) class or interface declaration in <span class="type">C</span>. Then the type of
<span class="type">D</span> in <span class="type">C</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code> is <span class="type">D</span> where, if <span class="type">D</span> is
generic, all type arguments are unbounded wildcards.
</p>
</li>
</ul>
</div>
<p class="note">This is of no consequence, as it is impossible to
access a member of a parameterized type without performing capture
conversion, and it is impossible to use a wildcard after the keyword
<code class="literal">new</code> in a class instance creation expression
(<a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>).
</p>
<p class="note">The sole exception
to the previous paragraph is when a nested parameterized type is used
as the expression in an <code class="literal">instanceof</code> operator
(<a class="xref" href="jls-15.html#jls-15.20.2" title="15.20.2.&nbsp;Type Comparison Operator instanceof">&sect;15.20.2</a>), where capture conversion is not
applied.
</p>
<p class="norm-error"><a name="jls-4.5.2-300"></a>A <code class="literal">static</code>
member that is declared in a generic type declaration must be referred
to using the non-generic type that corresponds to the generic type
(<a class="xref" href="jls-6.html#jls-6.1" title="6.1.&nbsp;Declarations">&sect;6.1</a>, <a class="xref" href="jls-6.html#jls-6.5.5.2" title="6.5.5.2.&nbsp;Qualified Type Names">&sect;6.5.5.2</a>,
<a class="xref" href="jls-6.html#jls-6.5.6.2" title="6.5.6.2.&nbsp;Qualified Expression Names">&sect;6.5.6.2</a>), or a compile-time error occurs.
</p>
<p class="note">In other words, it is illegal to refer to a <code class="literal">static</code>
member declared in a generic type declaration by using a parameterized
type.
</p>
</div>
</div>
<div class="section" title="4.6.&nbsp;Type Erasure">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-4.6"></a>4.6.&nbsp;Type Erasure
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-4.6-100"></a>Type erasure
is a mapping from types (possibly including parameterized types and
type variables) to types (that are never parameterized types or type
variables). We write |<span class="type">T</span>| for the erasure of type <span class="type">T</span>. The erasure
mapping is defined as follows:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-4.6-100-A"></a>The
erasure of a parameterized type (<a class="xref" href="jls-4.html#jls-4.5" title="4.5.&nbsp;Parameterized Types">&sect;4.5</a>)
<span class="type">G</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code> is |<span class="type">G</span>|.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.6-100-B"></a>The
erasure of a nested type <span class="type">T</span><code class="literal">.</code><span class="type">C</span> is |<span class="type">T</span>|.<span class="type">C</span>.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.6-100-C"></a>The
erasure of an array type <span class="type">T</span><code class="literal">[]</code> is |<span class="type">T</span>|<code class="literal">[]</code>.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-4.6-100-D"></a>The
erasure of a type variable (<a class="xref" href="jls-4.html#jls-4.4" title="4.4.&nbsp;Type Variables">&sect;4.4</a>) is the
erasure of its leftmost bound.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a