Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
5089 lines (4673 sloc) 324 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;14.&nbsp;Blocks and Statements</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-13.html" title="Chapter&nbsp;13.&nbsp;Binary Compatibility">
<link rel="next" href="jls-15.html" title="Chapter&nbsp;15.&nbsp;Expressions">
<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;14.&nbsp;Blocks and Statements</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="jls-13.html">Prev</a>&nbsp;
</td>
<th width="60%" align="center">&nbsp;</th>
<td width="20%" align="right">&nbsp;<a accesskey="n" href="jls-15.html">Next</a></td>
</tr>
</table>
<hr>
</div>
<div lang="en" class="chapter" title="Chapter&nbsp;14.&nbsp;Blocks and Statements">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="jls-14"></a>Chapter&nbsp;14.&nbsp;Blocks and Statements
</h2>
</div>
</div>
</div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="section"><a href="jls-14.html#jls-14.1">14.1. Normal and Abrupt Completion of Statements</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.2">14.2. Blocks</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.3">14.3. Local Class Declarations</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.4">14.4. Local Variable Declaration Statements</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-14.html#jls-14.4.1">14.4.1. Local Variable Declarators and Types</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.4.2">14.4.2. Execution of Local Variable Declarations</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-14.html#jls-14.5">14.5. Statements</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.6">14.6. The Empty Statement</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.7">14.7. Labeled Statements</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.8">14.8. Expression Statements</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.9">14.9. The <code class="literal">if</code> Statement</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-14.html#jls-14.9.1">14.9.1. The <code class="literal">if</code>-<code class="literal">then</code> Statement</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.9.2">14.9.2. The <code class="literal">if</code>-<code class="literal">then</code>-<code class="literal">else</code> Statement</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-14.html#jls-14.10">14.10. The <code class="literal">assert</code> Statement</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.11">14.11. The <code class="literal">switch</code> Statement</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.12">14.12. The <code class="literal">while</code> Statement</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-14.html#jls-14.12.1">14.12.1. Abrupt Completion of <code class="literal">while</code> Statement</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-14.html#jls-14.13">14.13. The <code class="literal">do</code> Statement</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-14.html#jls-14.13.1">14.13.1. Abrupt Completion of <code class="literal">do</code> Statement</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-14.html#jls-14.14">14.14. The <code class="literal">for</code> Statement</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-14.html#jls-14.14.1">14.14.1. The basic <code class="literal">for</code> Statement</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-14.html#jls-14.14.1.1">14.14.1.1. Initialization of <code class="literal">for</code> Statement</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.14.1.2">14.14.1.2. Iteration of <code class="literal">for</code> Statement</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.14.1.3">14.14.1.3. Abrupt Completion of <code class="literal">for</code> Statement</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-14.html#jls-14.14.2">14.14.2. The enhanced <code class="literal">for</code> statement</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-14.html#jls-14.15">14.15. The <code class="literal">break</code> Statement</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.16">14.16. The <code class="literal">continue</code> Statement</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.17">14.17. The <code class="literal">return</code> Statement</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.18">14.18. The <code class="literal">throw</code> Statement</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.19">14.19. The <code class="literal">synchronized</code> Statement</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.20">14.20. The <code class="literal">try</code> statement</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-14.html#jls-14.20.1">14.20.1. Execution of <code class="literal">try</code>-<code class="literal">catch</code></a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.20.2">14.20.2. Execution of <code class="literal">try</code>-<code class="literal">finally</code> and <code class="literal">try</code>-<code class="literal">catch</code>-<code class="literal">finally</code></a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.20.3">14.20.3. <code class="literal">try</code>-with-resources</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-14.html#jls-14.20.3.1">14.20.3.1. Basic <code class="literal">try</code>-with-resources</a></span></dt>
<dt><span class="section"><a href="jls-14.html#jls-14.20.3.2">14.20.3.2. Extended <code class="literal">try</code>-with-resources</a></span></dt>
</dl>
</dd>
</dl>
</dd>
<dt><span class="section"><a href="jls-14.html#jls-14.21">14.21. Unreachable Statements</a></span></dt>
</dl>
</div>
<p class="norm"><a name="jls-14-100"></a>The sequence of execution of a
program is controlled by <span class="emphasis"><em>statements</em></span>, which are
executed for their effect and do not have values.
</p>
<p class="norm"><a name="jls-14-110"></a>Some
statements <span class="emphasis"><em>contain</em></span> other statements as part of
their structure; such other statements are substatements of the
statement. We say that
statement <code class="varname">S</code> <span class="emphasis"><em>immediately
contains</em></span> statement <code class="varname">U</code> if there is no
statement <code class="varname">T</code> different from <code class="varname">S</code>
and <code class="varname">U</code> such that <code class="varname">S</code>
contains <code class="varname">T</code> and <code class="varname">T</code>
contains <code class="varname">U</code>. In the same manner, some statements
contain expressions (<a class="xref" href="jls-15.html" title="Chapter&nbsp;15.&nbsp;Expressions">&sect;15 (<i>Expressions</i>)</a>) as part of their
structure.
</p>
<p class="norm"><a name="jls-14-200"></a>The first section of this
chapter discusses the distinction between normal and abrupt completion
of statements (<a class="xref" href="jls-14.html#jls-14.1" title="14.1.&nbsp;Normal and Abrupt Completion of Statements">&sect;14.1</a>). Most of the remaining
sections explain the various kinds of statements, describing in detail
both their normal behavior and any special treatment of abrupt
completion.
</p>
<p class="norm"><a name="jls-14-210"></a>Blocks are explained first
(<a class="xref" href="jls-14.html#jls-14.2" title="14.2.&nbsp;Blocks">&sect;14.2</a>), followed by local class declarations
(<a class="xref" href="jls-14.html#jls-14.3" title="14.3.&nbsp;Local Class Declarations">&sect;14.3</a>) and local variable declaration statements
(<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-14-220"></a>Next a grammatical maneuver that
sidesteps the familiar "dangling else" problem
(<a class="xref" href="jls-14.html#jls-14.5" title="14.5.&nbsp;Statements">&sect;14.5</a>) is explained.
</p>
<p class="norm"><a name="jls-14-230"></a>The last section
(<a class="xref" href="jls-14.html#jls-14.21" title="14.21.&nbsp;Unreachable Statements">&sect;14.21</a>) of this chapter addresses the
requirement that every statement be <span class="emphasis"><em>reachable</em></span> in
a certain technical sense.
</p>
<div class="section" title="14.1.&nbsp;Normal and Abrupt Completion of Statements">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.1"></a>14.1.&nbsp;Normal and Abrupt Completion of Statements
</h2>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.1-100"></a>Every
statement has a normal mode of execution in which certain
computational steps are carried out. The following sections describe
the normal mode of execution for each kind of statement.
</p>
<p class="norm-dynamic"><a name="jls-14.1-110"></a>If all the
steps are carried out as described, with no indication of abrupt
completion, the statement is said to <span class="emphasis"><em>complete
normally</em></span>. However, certain events may prevent a statement
from completing normally:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.1-110-A"></a>The
<code class="literal">break</code> (<a class="xref" href="jls-14.html#jls-14.15" title="14.15.&nbsp;The break Statement">&sect;14.15</a>), <code class="literal">continue</code>
(<a class="xref" href="jls-14.html#jls-14.16" title="14.16.&nbsp;The continue Statement">&sect;14.16</a>), and <code class="literal">return</code>
(<a class="xref" href="jls-14.html#jls-14.17" title="14.17.&nbsp;The return Statement">&sect;14.17</a>) statements cause a transfer of
control that may prevent normal completion of statements that
contain them.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.1-110-B"></a>Evaluation of certain expressions may
throw exceptions from the Java Virtual Machine (<a class="xref" href="jls-15.html#jls-15.6" title="15.6.&nbsp;Normal and Abrupt Completion of Evaluation">&sect;15.6</a>). An
explicit <code class="literal">throw</code> (<a class="xref" href="jls-14.html#jls-14.18" title="14.18.&nbsp;The throw Statement">&sect;14.18</a>) statement also
results in an exception. An exception causes a transfer of control
that may prevent normal completion of statements.
</p>
</li>
</ul>
</div>
<p class="norm-dynamic"><a name="jls-14.1-120"></a>If such an
event occurs, then execution of one or more statements may be
terminated before all steps of their normal mode of execution have
completed; such statements are said to <span class="emphasis"><em>complete
abruptly</em></span>.
</p>
<p class="norm-dynamic"><a name="jls-14.1-130"></a>An abrupt
completion always has an associated <span class="emphasis"><em>reason</em></span>, which
is one of the following:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.1-130-A"></a>A
<code class="literal">break</code> with no label
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.1-130-B"></a>A
<code class="literal">break</code> with a given label
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.1-130-C"></a>A
<code class="literal">continue</code> with no label
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.1-130-D"></a>A
<code class="literal">continue</code> with a given label
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.1-130-E"></a>A
<code class="literal">return</code> with no value
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.1-130-F"></a>A
<code class="literal">return</code> with a given value
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.1-130-G"></a>A
<code class="literal">throw</code> with a given value, including exceptions thrown by the
Java Virtual Machine
</p>
</li>
</ul>
</div>
<p class="norm-dynamic"><a name="jls-14.1-140"></a>The terms
"complete normally" and "complete abruptly" also apply to the
evaluation of expressions (<a class="xref" href="jls-15.html#jls-15.6" title="15.6.&nbsp;Normal and Abrupt Completion of Evaluation">&sect;15.6</a>). The only
reason an expression can complete abruptly is that an exception is
thrown, because of either a <code class="literal">throw</code> with a given value
(<a class="xref" href="jls-14.html#jls-14.18" title="14.18.&nbsp;The throw Statement">&sect;14.18</a>) or a run-time exception or error
(<a class="xref" href="jls-11.html" title="Chapter&nbsp;11.&nbsp;Exceptions">&sect;11 (<i>Exceptions</i>)</a>, <a class="xref" href="jls-15.html#jls-15.6" title="15.6.&nbsp;Normal and Abrupt Completion of Evaluation">&sect;15.6</a>).
</p>
<p class="norm-dynamic"><a name="jls-14.1-150"></a>If a
statement evaluates an expression, abrupt completion of the expression
always causes the immediate abrupt completion of the statement, with
the same reason. All succeeding steps in the normal mode of execution
are not performed.
</p>
<p class="norm-dynamic"><a name="jls-14.1-160"></a>Unless
otherwise specified in this chapter, abrupt completion of a
substatement causes the immediate abrupt completion of the statement
itself, with the same reason, and all succeeding steps in the normal
mode of execution of the statement are not performed.
</p>
<p class="norm-dynamic"><a name="jls-14.1-170"></a>Unless
otherwise specified, a statement completes normally if all expressions
it evaluates and all substatements it executes complete
normally.
</p>
</div>
<div class="section" title="14.2.&nbsp;Blocks">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.2"></a>14.2.&nbsp;Blocks
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-14.2-100"></a>A <span class="emphasis"><em>block</em></span> is a sequence of
statements, local class declarations, and local variable declaration
statements within braces.
</p>
<div id="jls-14.2-110" class="productionset"><a name="jls-14.2-110"></a>
<div class="production"><a name="jls-Block"></a>
<div class="lhs">Block:</div>
<div class="rhs">
<code class="literal">{</code> [<a href="jls-14.html#jls-BlockStatements" title="BlockStatements">BlockStatements</a>] <code class="literal">}</code>
</div>
</div>
<div class="production"><a name="jls-BlockStatements"></a>
<div class="lhs">BlockStatements:</div>
<div class="rhs">
<a href="jls-14.html#jls-BlockStatement" title="BlockStatement">BlockStatement</a> {<a href="jls-14.html#jls-BlockStatement" title="BlockStatement">BlockStatement</a>}
</div>
</div>
<div class="production"><a name="jls-BlockStatement"></a>
<div class="lhs">BlockStatement:</div>
<div class="rhs">
<a href="jls-14.html#jls-LocalVariableDeclarationStatement" title="LocalVariableDeclarationStatement">LocalVariableDeclarationStatement</a> <br>
<a href="jls-8.html#jls-ClassDeclaration" title="ClassDeclaration">ClassDeclaration</a> <br>
<a href="jls-14.html#jls-Statement" title="Statement">Statement</a>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.2-200"></a>A block is
executed by executing each of the local variable declaration
statements and other statements in order from first to last (left to
right). If all of these block statements complete normally, then the
block completes normally. If any of these block statements complete
abruptly for any reason, then the block completes abruptly for the
same reason.
</p>
</div>
<div class="section" title="14.3.&nbsp;Local Class Declarations">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.3"></a>14.3.&nbsp;Local Class Declarations
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-14.3-100"></a>
A <span class="emphasis"><em>local class</em></span> is a nested class
(<a class="xref" href="jls-8.html" title="Chapter&nbsp;8.&nbsp;Classes">&sect;8 (<i>Classes</i>)</a>) that is not a member of any class and that
has a name (<a class="xref" href="jls-6.html#jls-6.2" title="6.2.&nbsp;Names and Identifiers">&sect;6.2</a>,
<a class="xref" href="jls-6.html#jls-6.7" title="6.7.&nbsp;Fully Qualified Names and Canonical Names">&sect;6.7</a>).
</p>
<p class="norm-static"><a name="jls-14.3-110"></a>All local
classes are inner classes (<a class="xref" href="jls-8.html#jls-8.1.3" title="8.1.3.&nbsp;Inner Classes and Enclosing Instances">&sect;8.1.3</a>).
</p>
<p class="norm-static"><a name="jls-14.3-120"></a>Every local
class declaration statement is immediately contained by a block
(<a class="xref" href="jls-14.html#jls-14.2" title="14.2.&nbsp;Blocks">&sect;14.2</a>). Local class declaration statements may
be intermixed freely with other kinds of statements in the
block.
</p>
<p class="norm-error"><a name="jls-14.3-200"></a>It is a
compile-time error if a local class declaration contains any of the
access modifiers <code class="literal">public</code>, <code class="literal">protected</code>, or <code class="literal">private</code>
(<a class="xref" href="jls-6.html#jls-6.6" title="6.6.&nbsp;Access Control">&sect;6.6</a>), or the modifier <code class="literal">static</code>
(<a class="xref" href="jls-8.html#jls-8.1.1" title="8.1.1.&nbsp;Class Modifiers">&sect;8.1.1</a>).
</p>
<p class="norm-static"><a name="jls-14.3-300"></a>The scope and shadowing of a local
class declaration 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> and
<a class="xref" href="jls-6.html#jls-6.4" title="6.4.&nbsp;Shadowing and Obscuring">&sect;6.4</a>.
</p>
<div class="example"><a name="d5e19913"></a><p class="title"><b>Example&nbsp;14.3-1.&nbsp;Local Class Declarations</b></p>
<div class="example-contents">
<p class="note">Here is an example that illustrates several aspects
of the rules given above:
</p><pre class="programlisting">
class Global {
class Cyclic {}
void foo() {
new Cyclic(); // create a Global.Cyclic
class Cyclic extends Cyclic {} // circular definition
{
class Local {}
{
class Local {} // compile-time error
}
class Local {} // compile-time error
class AnotherLocal {
void bar() {
class Local {} // ok
}
}
}
class Local {} // ok, not in scope of prior Local
}
}
</pre><p class="note">The first statement of method <code class="literal">foo</code>
creates an instance of the member
class <code class="literal">Global.Cyclic</code> rather than an instance of the
local class <code class="literal">Cyclic</code>, because the statement appears
prior to the scope of the local class declaration.
</p>
<p class="note">The fact that the scope of a local class declaration
encompasses its whole declaration (not only its body) means that the
definition of the local class <code class="literal">Cyclic</code> is indeed
cyclic because it extends itself rather
than <code class="literal">Global.Cyclic</code>. Consequently, the declaration
of the local class <code class="literal">Cyclic</code> is rejected at compile
time.
</p>
<p class="note">Since local class names cannot be redeclared within
the same method (or constructor or initializer, as the case may be),
the second and third declarations of <code class="literal">Local</code> result
in compile-time errors. However, <code class="literal">Local</code> can be
redeclared in the context of another, more deeply nested, class such
as <code class="literal">AnotherLocal</code>.
</p>
<p class="note">The final declaration of <code class="literal">Local</code> is
legal, since it occurs outside the scope of any prior declaration
of <code class="literal">Local</code>.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="14.4.&nbsp;Local Variable Declaration Statements">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.4"></a>14.4.&nbsp;Local Variable Declaration Statements
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-14.4-100"></a>A <span class="emphasis"><em>local variable declaration
statement</em></span> declares one or more local variable names.
</p>
<div id="jls-14.4-110" class="productionset"><a name="jls-14.4-110"></a>
<div class="production"><a name="jls-LocalVariableDeclarationStatement"></a>
<div class="lhs">LocalVariableDeclarationStatement:</div>
<div class="rhs">
<a href="jls-14.html#jls-LocalVariableDeclaration" title="LocalVariableDeclaration">LocalVariableDeclaration</a> <code class="literal">;</code>
</div>
</div>
<div class="production"><a name="jls-LocalVariableDeclaration"></a>
<div class="lhs">LocalVariableDeclaration:</div>
<div class="rhs">
{<a href="jls-8.html#jls-VariableModifier" title="VariableModifier">VariableModifier</a>}
<a href="jls-8.html#jls-UnannType" title="UnannType">UnannType</a>
<a href="jls-8.html#jls-VariableDeclaratorList" title="VariableDeclaratorList">VariableDeclaratorList</a>
</div>
</div>
</div>
<p class="note">See <a class="xref" href="jls-8.html#jls-8.3" title="8.3.&nbsp;Field Declarations">&sect;8.3</a> for
<span class="emphasis"><em>UnannType</em></span>. The following productions from
<a class="xref" href="jls-4.html#jls-4.3" title="4.3.&nbsp;Reference Types and Values">&sect;4.3</a>, <a class="xref" href="jls-8.html#jls-8.4.1" title="8.4.1.&nbsp;Formal Parameters">&sect;8.4.1</a>, and
<a class="xref" href="jls-8.html#jls-8.3" title="8.3.&nbsp;Field Declarations">&sect;8.3</a> are shown here for convenience:
</p>
<div id="d5e19954" class="productionset"><a name="d5e19954"></a>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">VariableModifier:</div>
<div class="rhs">
<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a> <code class="literal">final</code>
</div>
</div>
</div>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">VariableDeclaratorList:</div>
<div class="rhs">
<a href="jls-8.html#jls-VariableDeclarator" title="VariableDeclarator">VariableDeclarator</a> {<code class="literal">,</code> <a href="jls-8.html#jls-VariableDeclarator" title="VariableDeclarator">VariableDeclarator</a>}
</div>
</div>
</div>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">VariableDeclarator:</div>
<div class="rhs">
<a href="jls-8.html#jls-VariableDeclaratorId" title="VariableDeclaratorId">VariableDeclaratorId</a> [<code class="literal">=</code> <a href="jls-8.html#jls-VariableInitializer" title="VariableInitializer">VariableInitializer</a>]
</div>
</div>
</div>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">VariableDeclaratorId:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a> [<a href="jls-4.html#jls-Dims" title="Dims">Dims</a>]
</div>
</div>
</div>
<div class="productionrecap-note">
<div class="production">
<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="productionrecap-note">
<div class="production">
<div class="lhs">VariableInitializer:</div>
<div class="rhs">
<a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <br>
<a href="jls-10.html#jls-ArrayInitializer" title="ArrayInitializer">ArrayInitializer</a>
</div>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-14.4-200"></a>Every local
variable declaration statement is immediately contained by a
block. Local variable declaration statements may be intermixed freely
with other kinds of statements in the block.
</p>
<p class="norm-static"><a name="jls-14.4-210"></a>Apart from
local variable declaration statements, a local variable declaration
can appear in the header of a <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>) or <code class="literal">try</code>-with-resources statement
(<a class="xref" href="jls-14.html#jls-14.20.3" title="14.20.3.&nbsp;try-with-resources">&sect;14.20.3</a>). In these cases, it is executed in the
same manner as if it were part of a local variable declaration
statement.
</p>
<p class="norm-error"><a name="jls-14.4-300"></a>The rules for
annotation modifiers on a local variable declaration are specified in
<a class="xref" href="jls-9.html#jls-9.7.4" title="9.7.4.&nbsp;Where Annotations May Appear">&sect;9.7.4</a> and <a class="xref" href="jls-9.html#jls-9.7.5" title="9.7.5.&nbsp;Multiple Annotations Of The Same Type">&sect;9.7.5</a>.
</p>
<p class="norm-error"><a name="jls-14.4-310"></a>It is a
compile-time error if <code class="literal">final</code> appears more than once as a modifier for
a local variable declaration.
</p>
<div class="section" title="14.4.1.&nbsp;Local Variable Declarators and Types">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-14.4.1"></a>14.4.1.&nbsp;Local Variable Declarators and Types
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-14.4.1-100"></a>Each
<span class="emphasis"><em>declarator</em></span> in a local variable declaration
declares one local variable, whose name is the <span class="emphasis"><em>Identifier</em></span> that
appears in the declarator.
</p>
<p class="norm-static"><a name="jls-14.4.1-110"></a>If the
optional keyword <code class="literal">final</code> appears at the start of the declaration, the
variable being declared is a final variable
(<a class="xref" href="jls-4.html#jls-4.12.4" title="4.12.4.&nbsp;final Variables">&sect;4.12.4</a>).
</p>
<p class="norm-static"><a name="jls-14.4.1-200"></a>The declared
type of a local variable is denoted by
the <span class="emphasis"><em>UnannType</em></span> that appears in the local variable
declaration, followed by any bracket pairs that follow the
<span class="emphasis"><em>Identifier</em></span> in the declarator.
</p>
<p class="norm-dynamic"><a name="jls-14.4.1-210"></a>A local
variable of type <code class="literal">float</code> always contains a value that is an element of
the float value set (<a class="xref" href="jls-4.html#jls-4.2.3" title="4.2.3.&nbsp;Floating-Point Types, Formats, and Values">&sect;4.2.3</a>); similarly, a local
variable of type <code class="literal">double</code> always contains a value that is an element
of the double value set. It is not permitted for a local variable of
type <code class="literal">float</code> to contain an element of the float-extended-exponent
value set that is not also an element of the float value set, nor for
a local variable of type <code class="literal">double</code> to contain an element of the
double-extended-exponent value set that is not also an element of the
double value set.
</p>
<p class="norm-static"><a name="jls-14.4.1-220"></a>The scope and shadowing of a local
variable 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> and
<a class="xref" href="jls-6.html#jls-6.4" title="6.4.&nbsp;Shadowing and Obscuring">&sect;6.4</a>.
</p>
</div>
<div class="section" title="14.4.2.&nbsp;Execution of Local Variable Declarations">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-14.4.2"></a>14.4.2.&nbsp;Execution of Local Variable Declarations
</h3>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.4.2-100"></a>A local
variable declaration statement is an executable statement. Every time
it is executed, the declarators are processed in order from left to
right. If a declarator has an initializer, the initializer is
evaluated and its value is assigned to the variable.
</p>
<p class="note">If a declarator does not have an initializer, then
every reference to the variable must be preceded by execution of an
assignment to the variable, or a compile-time error occurs by the
rules of <a class="xref" href="jls-16.html" title="Chapter&nbsp;16.&nbsp;Definite Assignment">&sect;16 (<i>Definite Assignment</i>)</a>.
</p>
<p class="norm-dynamic"><a name="jls-14.4.2-110"></a>Each
initializer (except the first) is evaluated only if evaluation of the
preceding initializer completes normally.
</p>
<p class="norm-dynamic"><a name="jls-14.4.2-120"></a>Execution of the local variable declaration
completes normally only if evaluation of the last initializer
completes normally.
</p>
<p class="norm-dynamic"><a name="jls-14.4.2-130"></a>If the
local variable declaration contains no initializers, then executing it
always completes normally.
</p>
</div>
</div>
<div class="section" title="14.5.&nbsp;Statements">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.5"></a>14.5.&nbsp;Statements
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-14.5-100"></a>There are many kinds of
statements in the Java programming language. Most correspond to statements in the C
and C++ languages, but some are unique.
</p>
<p class="norm"><a name="jls-14.5-110"></a>As in C and C++, the <code class="literal">if</code>
statement of the Java programming language suffers from the so-called "dangling
<code class="literal">else</code> problem," illustrated by this misleadingly formatted
example:
</p><pre class="screen">
if (door.isOpen())
if (resident.isVisible())
resident.greet("Hello!");
else door.bell.ring(); // A "dangling else"
</pre><p class="norm"><a name="jls-14.5-120"></a>The problem is that both the
outer <code class="literal">if</code> statement and the inner <code class="literal">if</code> statement might conceivably
own the <code class="literal">else</code> clause. In this example, one might surmise that the
programmer intended the <code class="literal">else</code> clause to belong to the outer <code class="literal">if</code>
statement.
</p>
<p class="norm"><a name="jls-14.5-130"></a>The Java programming language, like C and C++
and many programming languages before them, arbitrarily decrees that
an <code class="literal">else</code> clause belongs to the innermost <code class="literal">if</code> to which it might
possibly belong. This rule is captured by the following
grammar:
</p>
<div id="jls-14.5-140" class="productionset"><a name="jls-14.5-140"></a>
<div class="production"><a name="jls-Statement"></a>
<div class="lhs">Statement:</div>
<div class="rhs">
<a href="jls-14.html#jls-StatementWithoutTrailingSubstatement" title="StatementWithoutTrailingSubstatement">StatementWithoutTrailingSubstatement</a> <br>
<a href="jls-14.html#jls-LabeledStatement" title="LabeledStatement">LabeledStatement</a> <br>
<a href="jls-14.html#jls-IfThenStatement" title="IfThenStatement">IfThenStatement</a> <br>
<a href="jls-14.html#jls-IfThenElseStatement" title="IfThenElseStatement">IfThenElseStatement</a> <br>
<a href="jls-14.html#jls-WhileStatement" title="WhileStatement">WhileStatement</a> <br>
<a href="jls-14.html#jls-ForStatement" title="ForStatement">ForStatement</a>
</div>
</div>
<div class="production"><a name="jls-StatementNoShortIf"></a>
<div class="lhs">StatementNoShortIf:</div>
<div class="rhs">
<a href="jls-14.html#jls-StatementWithoutTrailingSubstatement" title="StatementWithoutTrailingSubstatement">StatementWithoutTrailingSubstatement</a> <br>
<a href="jls-14.html#jls-LabeledStatementNoShortIf" title="LabeledStatementNoShortIf">LabeledStatementNoShortIf</a> <br>
<a href="jls-14.html#jls-IfThenElseStatementNoShortIf" title="IfThenElseStatementNoShortIf">IfThenElseStatementNoShortIf</a> <br>
<a href="jls-14.html#jls-WhileStatementNoShortIf" title="WhileStatementNoShortIf">WhileStatementNoShortIf</a> <br>
<a href="jls-14.html#jls-ForStatementNoShortIf" title="ForStatementNoShortIf">ForStatementNoShortIf</a>
</div>
</div>
<div class="production"><a name="jls-StatementWithoutTrailingSubstatement"></a>
<div class="lhs">StatementWithoutTrailingSubstatement:</div>
<div class="rhs">
<a href="jls-14.html#jls-Block" title="Block">Block</a> <br>
<a href="jls-14.html#jls-EmptyStatement" title="EmptyStatement">EmptyStatement</a> <br>
<a href="jls-14.html#jls-ExpressionStatement" title="ExpressionStatement">ExpressionStatement</a> <br>
<a href="jls-14.html#jls-AssertStatement" title="AssertStatement">AssertStatement</a> <br>
<a href="jls-14.html#jls-SwitchStatement" title="SwitchStatement">SwitchStatement</a> <br>
<a href="jls-14.html#jls-DoStatement" title="DoStatement">DoStatement</a> <br>
<a href="jls-14.html#jls-BreakStatement" title="BreakStatement">BreakStatement</a> <br>
<a href="jls-14.html#jls-ContinueStatement" title="ContinueStatement">ContinueStatement</a> <br>
<a href="jls-14.html#jls-ReturnStatement" title="ReturnStatement">ReturnStatement</a> <br>
<a href="jls-14.html#jls-SynchronizedStatement" title="SynchronizedStatement">SynchronizedStatement</a> <br>
<a href="jls-14.html#jls-ThrowStatement" title="ThrowStatement">ThrowStatement</a> <br>
<a href="jls-14.html#jls-TryStatement" title="TryStatement">TryStatement</a>
</div>
</div>
</div>
<p class="note">The following productions from
<a class="xref" href="jls-14.html#jls-14.9" title="14.9.&nbsp;The if Statement">&sect;14.9</a> are shown here for convenience:
</p>
<div id="d5e20051" class="productionset"><a name="d5e20051"></a>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">IfThenStatement:</div>
<div class="rhs">
<code class="literal">if</code> <code class="literal">(</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">)</code>
<a href="jls-14.html#jls-Statement" title="Statement">Statement</a>
</div>
</div>
</div>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">IfThenElseStatement:</div>
<div class="rhs">
<code class="literal">if</code> <code class="literal">(</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">)</code>
<a href="jls-14.html#jls-StatementNoShortIf" title="StatementNoShortIf">StatementNoShortIf</a>
<code class="literal">else</code> <a href="jls-14.html#jls-Statement" title="Statement">Statement</a>
</div>
</div>
</div>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">IfThenElseStatementNoShortIf:</div>
<div class="rhs">
<code class="literal">if</code> <code class="literal">(</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">)</code>
<a href="jls-14.html#jls-StatementNoShortIf" title="StatementNoShortIf">StatementNoShortIf</a>
<code class="literal">else</code> <a href="jls-14.html#jls-StatementNoShortIf" title="StatementNoShortIf">StatementNoShortIf</a>
</div>
</div>
</div>
</div>
<p class="norm"><a name="jls-14.5-150"></a>Statements are thus
grammatically divided into two categories: those that might end in an
<code class="literal">if</code> statement that has no <code class="literal">else</code> clause (a "short <code class="literal">if</code> statement")
and those that definitely do not.
</p>
<p class="norm"><a name="jls-14.5-160"></a>Only statements that
definitely do not end in a short <code class="literal">if</code> statement may appear as an
immediate substatement before the keyword <code class="literal">else</code> in an <code class="literal">if</code> statement
that does have an <code class="literal">else</code> clause.
</p>
<p class="norm"><a name="jls-14.5-170"></a>This simple rule prevents the
"dangling <code class="literal">else</code>" problem. The execution behavior of a statement with
the "no short <code class="literal">if</code>" restriction is identical to the execution behavior
of the same kind of statement without the "no short <code class="literal">if</code>" restriction;
the distinction is drawn purely to resolve the syntactic
difficulty.
</p>
</div>
<div class="section" title="14.6.&nbsp;The Empty Statement">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.6"></a>14.6.&nbsp;The Empty Statement
</h2>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.6-100"></a>An empty
statement does nothing.
</p>
<div id="jls-14.6-110" class="productionset"><a name="jls-14.6-110"></a>
<div class="production"><a name="jls-EmptyStatement"></a>
<div class="lhs">EmptyStatement:</div>
<div class="rhs">
<code class="literal">;</code>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.6-200"></a>Execution
of an empty statement always completes normally.
</p>
</div>
<div class="section" title="14.7.&nbsp;Labeled Statements">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.7"></a>14.7.&nbsp;Labeled Statements
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-14.7-100"></a>Statements
may have <span class="emphasis"><em>label</em></span> prefixes.
</p>
<div id="jls-14.7-110" class="productionset"><a name="jls-14.7-110"></a>
<div class="production"><a name="jls-LabeledStatement"></a>
<div class="lhs">LabeledStatement:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a> <code class="literal">:</code>
<a href="jls-14.html#jls-Statement" title="Statement">Statement</a>
</div>
</div>
<div class="production"><a name="jls-LabeledStatementNoShortIf"></a>
<div class="lhs">LabeledStatementNoShortIf:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a> <code class="literal">:</code>
<a href="jls-14.html#jls-StatementNoShortIf" title="StatementNoShortIf">StatementNoShortIf</a>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-14.7-120"></a>The
<span class="emphasis"><em>Identifier</em></span> is declared to be the label of the immediately contained
<span class="emphasis"><em>Statement</em></span>.
</p>
<p class="norm"><a name="jls-14.7-130"></a>Unlike C and C++, the
Java programming language has no <code class="literal">goto</code> statement; identifier
statement labels are used with <code class="literal">break</code> or <code class="literal">continue</code> statements
(<a class="xref" href="jls-14.html#jls-14.15" title="14.15.&nbsp;The break Statement">&sect;14.15</a>, <a class="xref" href="jls-14.html#jls-14.16" title="14.16.&nbsp;The continue Statement">&sect;14.16</a>) appearing
anywhere within the labeled statement.
</p>
<p class="norm-static"><a name="jls-14.7-200"></a>The scope
of a label of a labeled statement is the immediately contained
<span class="emphasis"><em>Statement</em></span>.
</p>
<p class="norm-error"><a name="jls-14.7-210"></a>It is a compile-time error if the
name of a label of a labeled statement is used within the scope of the
label as a label of another labeled statement.
</p>
<p class="norm-static"><a name="jls-14.7-220"></a>There is no
restriction against using the same identifier as a label and as the
name of a package, class, interface, method, field, parameter, or
local variable. Use of an identifier to label a statement does not
obscure (<a class="xref" href="jls-6.html#jls-6.4.2" title="6.4.2.&nbsp;Obscuring">&sect;6.4.2</a>) a package, class, interface,
method, field, parameter, or local variable with the same name. Use of
an identifier as a class, interface, method, field, local variable or
as the parameter of an exception handler (<a class="xref" href="jls-14.html#jls-14.20" title="14.20.&nbsp;The try statement">&sect;14.20</a>)
does not obscure a statement label with the same name.
</p>
<p class="norm-dynamic"><a name="jls-14.7-300"></a>A labeled
statement is executed by executing the immediately contained
<span class="emphasis"><em>Statement</em></span>.
</p>
<p class="norm-dynamic"><a name="jls-14.7-310"></a>If the
statement is labeled by an <span class="emphasis"><em>Identifier</em></span> and the contained <span class="emphasis"><em>Statement</em></span>
completes abruptly because of a <code class="literal">break</code> with the same <span class="emphasis"><em>Identifier</em></span>,
then the labeled statement completes normally. In all other cases of
abrupt completion of the <span class="emphasis"><em>Statement</em></span>, the labeled statement completes
abruptly for the same reason.
</p>
<div class="example"><a name="d5e20117"></a><p class="title"><b>Example&nbsp;14.7-1.&nbsp;Labels and Identifiers</b></p>
<div class="example-contents">
<p class="note">The following code was taken from a version of the
class <code class="literal">String</code> and its method <code class="literal">indexOf</code>, where the
label was originally called <code class="literal">test</code>. Changing the
label to have the same name as the local variable <code class="literal">i</code>
does not obscure the label in the scope of the declaration
of <code class="literal">i</code>. Thus, the code is valid.
</p><pre class="programlisting">
class Test {
char[] value;
int offset, count;
int indexOf(TestString str, int fromIndex) {
char[] v1 = value, v2 = str.value;
int max = offset + (count - str.count);
int start = offset + ((fromIndex &lt; 0) ? 0 : fromIndex);
i:
for (int i = start; i &lt;= max; i++) {
int n = str.count, j = i, k = str.offset;
while (n-- != 0) {
if (v1[j++] != v2[k++])
continue i;
}
return i - offset;
}
return -1;
}
}
</pre><p class="note">The identifier <code class="literal">max</code> could also
have been used as the statement label; the label would not obscure the
local variable <code class="literal">max</code> within the labeled
statement.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="14.8.&nbsp;Expression Statements">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.8"></a>14.8.&nbsp;Expression Statements
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-14.8-100"></a>Certain
kinds of expressions may be used as statements by following them with
semicolons.
</p>
<div id="jls-14.8-110" class="productionset"><a name="jls-14.8-110"></a>
<div class="production"><a name="jls-ExpressionStatement"></a>
<div class="lhs">ExpressionStatement:</div>
<div class="rhs">
<a href="jls-14.html#jls-StatementExpression" title="StatementExpression">StatementExpression</a> <code class="literal">;</code>
</div>
</div>
<div class="production"><a name="jls-StatementExpression"></a>
<div class="lhs">StatementExpression:</div>
<div class="rhs">
<a href="jls-15.html#jls-Assignment" title="Assignment">Assignment</a> <br>
<a href="jls-15.html#jls-PreIncrementExpression" title="PreIncrementExpression">PreIncrementExpression</a> <br>
<a href="jls-15.html#jls-PreDecrementExpression" title="PreDecrementExpression">PreDecrementExpression</a> <br>
<a href="jls-15.html#jls-PostIncrementExpression" title="PostIncrementExpression">PostIncrementExpression</a> <br>
<a href="jls-15.html#jls-PostDecrementExpression" title="PostDecrementExpression">PostDecrementExpression</a> <br>
<a href="jls-15.html#jls-MethodInvocation" title="MethodInvocation">MethodInvocation</a> <br>
<a href="jls-15.html#jls-ClassInstanceCreationExpression" title="ClassInstanceCreationExpression">ClassInstanceCreationExpression</a>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.8-200"></a>An <span class="emphasis"><em>expression statement</em></span> is
executed by evaluating the expression; if the expression has a value,
the value is discarded.
</p>
<p class="norm-dynamic"><a name="jls-14.8-210"></a>Execution
of the expression statement completes normally if and only if
evaluation of the expression completes normally.
</p>
<p class="note">Unlike C and C++, the Java programming language allows only certain
forms of expressions to be used as expression statements. Note that
the Java programming language does not allow a "cast to <code class="literal">void</code>" - <code class="literal">void</code> is not a
type - so the traditional C trick of writing an expression statement
such as:
</p><pre class="screen">
(void)... ; // incorrect!
</pre><p class="note">does not work. On the other hand, the Java programming language
allows all the most useful kinds of expressions in expression
statements, and it does not require a method invocation used as an
expression statement to invoke a <code class="literal">void</code> method, so such a trick is
almost never needed. If a trick is needed, either an assignment
statement (<a class="xref" href="jls-15.html#jls-15.26" title="15.26.&nbsp;Assignment Operators">&sect;15.26</a>) or a local variable
declaration statement (<a class="xref" href="jls-14.html#jls-14.4" title="14.4.&nbsp;Local Variable Declaration Statements">&sect;14.4</a>) can be used
instead.
</p>
</div>
<div class="section" title="14.9.&nbsp;The if Statement">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.9"></a>14.9.&nbsp;The <code class="literal">if</code> Statement
</h2>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.9-100"></a>The <code class="literal">if</code>
statement allows conditional execution of a statement or a conditional
choice of two statements, executing one or the other but not
both.
</p>
<div id="jls-14.9-110" class="productionset"><a name="jls-14.9-110"></a>
<div class="production"><a name="jls-IfThenStatement"></a>
<div class="lhs">IfThenStatement:</div>
<div class="rhs">
<code class="literal">if</code> <code class="literal">(</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">)</code>
<a href="jls-14.html#jls-Statement" title="Statement">Statement</a>
</div>
</div>
<div class="production"><a name="jls-IfThenElseStatement"></a>
<div class="lhs">IfThenElseStatement:</div>
<div class="rhs">
<code class="literal">if</code> <code class="literal">(</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">)</code>
<a href="jls-14.html#jls-StatementNoShortIf" title="StatementNoShortIf">StatementNoShortIf</a>
<code class="literal">else</code> <a href="jls-14.html#jls-Statement" title="Statement">Statement</a>
</div>
</div>
<div class="production"><a name="jls-IfThenElseStatementNoShortIf"></a>
<div class="lhs">IfThenElseStatementNoShortIf:</div>
<div class="rhs">
<code class="literal">if</code> <code class="literal">(</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">)</code>
<a href="jls-14.html#jls-StatementNoShortIf" title="StatementNoShortIf">StatementNoShortIf</a>
<code class="literal">else</code> <a href="jls-14.html#jls-StatementNoShortIf" title="StatementNoShortIf">StatementNoShortIf</a>
</div>
</div>
</div>
<p class="norm-error"><a name="jls-14.9-200"></a>The
<span class="emphasis"><em>Expression</em></span> must have type <code class="literal">boolean</code> or <code class="literal">Boolean</code>, or a compile-time
error occurs.
</p>
<div class="section" title="14.9.1.&nbsp;The if-then Statement">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-14.9.1"></a>14.9.1.&nbsp;The <code class="literal">if</code>-<code class="literal">then</code> Statement
</h3>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.9.1-100"></a>An
<code class="literal">if</code>-<code class="literal">then</code> statement is executed by first evaluating the
<span class="emphasis"><em>Expression</em></span>. If the result is of type <code class="literal">Boolean</code>, it is subject to
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>).
</p>
<p class="norm-dynamic"><a name="jls-14.9.1-110"></a>If
evaluation of the <span class="emphasis"><em>Expression</em></span> or the subsequent unboxing conversion
(if any) completes abruptly for some reason, the <code class="literal">if</code>-<code class="literal">then</code> statement
completes abruptly for the same reason.
</p>
<p class="norm-dynamic"><a name="jls-14.9.1-120"></a>Otherwise, execution continues by making a
choice based on the resulting value:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.9.1-120-A"></a>If
the value is <code class="literal">true</code>, then the contained <span class="emphasis"><em>Statement</em></span> is executed;
the <code class="literal">if</code>-<code class="literal">then</code> statement completes normally if and only if
execution of the <span class="emphasis"><em>Statement</em></span> completes normally.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.9.1-120-B"></a>If
the value is <code class="literal">false</code>, no further action is taken and the
<code class="literal">if</code>-<code class="literal">then</code> statement completes normally.
</p>
</li>
</ul>
</div>
</div>
<div class="section" title="14.9.2.&nbsp;The if-then-else Statement">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-14.9.2"></a>14.9.2.&nbsp;The <code class="literal">if</code>-<code class="literal">then</code>-<code class="literal">else</code> Statement
</h3>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.9.2-100"></a>An
<code class="literal">if</code>-<code class="literal">then</code>-<code class="literal">else</code> statement is executed by first evaluating the
<span class="emphasis"><em>Expression</em></span>. If the result is of type <code class="literal">Boolean</code>, it is subject to
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>).
</p>
<p class="norm-dynamic"><a name="jls-14.9.2-110"></a>If
evaluation of the <span class="emphasis"><em>Expression</em></span> or the subsequent unboxing conversion
(if any) completes abruptly for some reason, then the
<code class="literal">if</code>-<code class="literal">then</code>-<code class="literal">else</code> statement completes abruptly for the same
reason.
</p>
<p class="norm-dynamic"><a name="jls-14.9.2-120"></a>Otherwise, execution continues by making a
choice based on the resulting value:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.9.2-120-A"></a>If
the value is <code class="literal">true</code>, then the first contained <span class="emphasis"><em>Statement</em></span> (the one
before the <code class="literal">else</code> keyword) is executed; the <code class="literal">if</code>-<code class="literal">then</code>-<code class="literal">else</code>
statement completes normally if and only if execution of that
statement completes normally.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.9.2-120-B"></a>If
the value is <code class="literal">false</code>, then the second contained <span class="emphasis"><em>Statement</em></span> (the
one after the <code class="literal">else</code> keyword) is executed; the <code class="literal">if</code>-<code class="literal">then</code>-<code class="literal">else</code>
statement completes normally if and only if execution of that
statement completes normally.
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="section" title="14.10.&nbsp;The assert Statement">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.10"></a>14.10.&nbsp;The <code class="literal">assert</code> Statement
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-14.10-100"></a>An
<span class="emphasis"><em>assertion</em></span> is an <code class="literal">assert</code> statement containing a
boolean expression. An assertion is either
<span class="emphasis"><em>enabled</em></span> or <span class="emphasis"><em>disabled</em></span>. If an
assertion is enabled, execution of the assertion causes evaluation of
the boolean expression and an error is reported if the expression
evaluates to <code class="literal">false</code>. If the assertion is disabled, execution of the
assertion has no effect whatsoever.
</p>
<div id="jls-14.10-110" class="productionset"><a name="jls-14.10-110"></a>
<div class="production"><a name="jls-AssertStatement"></a>
<div class="lhs">AssertStatement:</div>
<div class="rhs">
<code class="literal">assert</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">;</code> <br>
<code class="literal">assert</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">:</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">;</code>
</div>
</div>
</div>
<p class="norm"><a name="jls-14.10-120"></a>To ease the presentation, the
first <span class="emphasis"><em>Expression</em></span> in both forms of the <code class="literal">assert</code> statement is referred
to as <span class="emphasis"><em>Expression1</em></span>. In the second form of the
<code class="literal">assert</code> statement, the second <span class="emphasis"><em>Expression</em></span> is referred to
as <span class="emphasis"><em>Expression2</em></span>.
</p>
<p class="norm-error"><a name="jls-14.10-200"></a>It is a
compile-time error if <span class="emphasis"><em>Expression1</em></span> does not have
type <code class="literal">boolean</code> or <code class="literal">Boolean</code>.
</p>
<p class="norm-error"><a name="jls-14.10-210"></a>In the
second form of the <code class="literal">assert</code> statement, it is a compile-time error if
<span class="emphasis"><em>Expression2</em></span> is void (<a class="xref" href="jls-15.html#jls-15.1" title="15.1.&nbsp;Evaluation, Denotation, and Result">&sect;15.1</a>).
</p>
<p class="norm-dynamic"><a name="jls-14.10-300"></a>An <code class="literal">assert</code>
statement that is executed after its class has completed
initialization is enabled if and only if the host system has
determined that the top level class that lexically contains the
<code class="literal">assert</code> statement enables assertions.
</p>
<p class="norm-dynamic"><a name="jls-14.10-310"></a>Whether or
not a top level class enables assertions is determined no later than
the earliest of the initialization of the top level class and the
initialization of any class nested in the top level class, and cannot
be changed after it has been determined.
</p>
<p class="norm-dynamic"><a name="jls-14.10-320"></a>An <code class="literal">assert</code>
statement that is executed before its class has completed
initialization is enabled.
</p>
<div class="informalexample">
<p class="note">This rule is
motivated by a case that demands special treatment. Recall that the
assertion status of a class is set no later
than the time it is initialized. It is possible, though
generally not desirable, to execute methods or constructors prior to
initialization. This can happen when a class hierarchy contains a
circularity in its static initialization, as in the following
example:
</p><pre class="programlisting">
public class Foo {
public static void main(String[] args) {
Baz.testAsserts();
// Will execute after Baz is initialized.
}
}
class Bar {
static {
Baz.testAsserts();
// Will execute before Baz is initialized!
}
}
class Baz extends Bar {
static void testAsserts() {
boolean enabled = false;
assert enabled = true;
System.out.println("Asserts " +
(enabled ? "enabled" : "disabled"));
}
}
</pre><p class="note">Invoking <code class="literal">Baz.testAsserts()</code>
causes <code class="literal">Baz</code> to be initialized. Before this can
happen, <code class="literal">Bar</code> must be
initialized. <code class="literal">Bar</code>'s static initializer again
invokes <code class="literal">Baz.testAsserts()</code>. Because initialization
of <code class="literal">Baz</code> is already in progress by the current
thread, the second invocation executes immediately,
though <code class="literal">Baz</code> is not initialized
(<a class="xref" href="jls-12.html#jls-12.4.2" title="12.4.2.&nbsp;Detailed Initialization Procedure">&sect;12.4.2</a>).
</p>
<p class="note">Because of the rule above, if the program above is
executed without enabling assertions, it must print:
</p><pre class="screen">
Asserts enabled
Asserts disabled
</pre></div>
<p class="norm-dynamic"><a name="jls-14.10-400"></a>A
disabled <code class="literal">assert</code> statement does nothing. In particular, neither
<span class="emphasis"><em>Expression1</em></span> nor <span class="emphasis"><em>Expression2</em></span>
(if it is present) are evaluated. Execution of a disabled <code class="literal">assert</code>
statement always completes normally.
</p>
<p class="norm-dynamic"><a name="jls-14.10-410"></a>An
enabled <code class="literal">assert</code> statement is executed by first
evaluating <span class="emphasis"><em>Expression1</em></span>. If the result is of type
<code class="literal">Boolean</code>, it is subject to 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>).
</p>
<p class="norm-dynamic"><a name="jls-14.10-420"></a>If
evaluation of <span class="emphasis"><em>Expression1</em></span> or the subsequent
unboxing conversion (if any) completes abruptly for some reason, the
<code class="literal">assert</code> statement completes abruptly for the same reason.
</p>
<p class="norm-dynamic"><a name="jls-14.10-430"></a>
Otherwise, execution continues by making a choice based on the value
of <span class="emphasis"><em>Expression1</em></span>:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.10-430-A"></a>If
the value is <code class="literal">true</code>, no further action is taken and the <code class="literal">assert</code>
statement completes normally.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.10-430-B"></a>If
the value is <code class="literal">false</code>, the execution behavior depends on
whether <span class="emphasis"><em>Expression2</em></span> is present:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.10-430-B-A"></a>If <span class="emphasis"><em>Expression2</em></span>
is present, it is evaluated. Then:
</p>
<div class="norm">
<ul class="norm" type="square">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.10-430-B-A-A"></a>If the evaluation completes
abruptly for some reason, the <code class="literal">assert</code> statement
completes abruptly for the same reason.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.10-430-B-A-B"></a>If the evaluation
completes normally, an <code class="literal">AssertionError</code> instance whose
"detail message" is the resulting value
of <span class="emphasis"><em>Expression2</em></span> is
created. Then:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.10-430-B-A-B-A"></a>If the instance
creation completes abruptly for some reason, the
<code class="literal">assert</code> statement completes abruptly for the same
reason.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.10-430-B-A-B-B"></a>If the instance
creation completes normally, the <code class="literal">assert</code> statement
completes abruptly by throwing the newly created
<code class="literal">AssertionError</code> object.
</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.10-430-B-B"></a>If <span class="emphasis"><em>Expression2</em></span>
is not present, an <code class="literal">AssertionError</code> instance with no "detail
message" is created. Then:
</p>
<div class="norm">
<ul class="norm" type="square">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.10-430-B-B-A"></a>If the instance creation
completes abruptly for some reason, the <code class="literal">assert</code>
statement completes abruptly for the same reason.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.10-430-B-B-B"></a>If the instance creation
completes normally, the <code class="literal">assert</code> statement completes
abruptly by throwing the newly created <code class="literal">AssertionError</code>
object.
</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
<p class="note">Typically, assertion-checking is enabled during
program development and testing, and disabled for deployment, to
improve performance.
</p>
<p class="note">Because assertions may be disabled, programs must
not assume that the expressions contained in assertions will be
evaluated. Thus, these boolean expressions should generally be free of
side effects. Evaluating such a boolean expression should not affect
any state that is visible after the evaluation is complete. It is not
illegal for a boolean expression contained in an assertion to have a
side effect, but it is generally inappropriate, as it could cause
program behavior to vary depending on whether assertions were enabled
or disabled.
</p>
<p class="note">In light of this, assertions should not be used for
argument checking in <code class="literal">public</code> methods. Argument checking is typically
part of the contract of a method, and this contract must be upheld
whether assertions are enabled or disabled.
</p>
<p class="note">A secondary problem with using assertions for
argument checking is that erroneous arguments should result in an
appropriate run-time exception (such as <code class="literal">IllegalArgumentException</code>,
<code class="literal">ArrayIndexOutOfBoundsException</code>, or <code class="literal">NullPointerException</code>). An assertion failure will not throw an
appropriate exception. Again, it is not illegal to use assertions for
argument checking on <code class="literal">public</code> methods, but it is generally
inappropriate. It is intended that <code class="literal">AssertionError</code> never be caught,
but it is possible to do so, thus the rules for <code class="literal">try</code> statements
should treat assertions appearing in a <code class="literal">try</code> block similarly to the
current treatment of <code class="literal">throw</code> statements.
</p>
</div>
<div class="section" title="14.11.&nbsp;The switch Statement">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.11"></a>14.11.&nbsp;The <code class="literal">switch</code> Statement
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-14.11-100"></a>The <code class="literal">switch</code> statement
transfers control to one of several statements depending on the value
of an expression.
</p>
<div id="jls-14.11-110" class="productionset"><a name="jls-14.11-110"></a>
<div class="production"><a name="jls-SwitchStatement"></a>
<div class="lhs">SwitchStatement:</div>
<div class="rhs">
<code class="literal">switch</code> <code class="literal">(</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">)</code>
<a href="jls-14.html#jls-SwitchBlock" title="SwitchBlock">SwitchBlock</a>
</div>
</div>
<div class="production"><a name="jls-SwitchBlock"></a>
<div class="lhs">SwitchBlock:</div>
<div class="rhs">
<code class="literal">{</code>
{<a href="jls-14.html#jls-SwitchBlockStatementGroup" title="SwitchBlockStatementGroup">SwitchBlockStatementGroup</a>}
{<a href="jls-14.html#jls-SwitchLabel" title="SwitchLabel">SwitchLabel</a>}
<code class="literal">}</code>
</div>
</div>
<div class="production"><a name="jls-SwitchBlockStatementGroup"></a>
<div class="lhs">SwitchBlockStatementGroup:</div>
<div class="rhs">
<a href="jls-14.html#jls-SwitchLabels" title="SwitchLabels">SwitchLabels</a> <a href="jls-14.html#jls-BlockStatements" title="BlockStatements">BlockStatements</a>
</div>
</div>
<div class="production"><a name="jls-SwitchLabels"></a>
<div class="lhs">SwitchLabels:</div>
<div class="rhs">
<a href="jls-14.html#jls-SwitchLabel" title="SwitchLabel">SwitchLabel</a> {<a href="jls-14.html#jls-SwitchLabel" title="SwitchLabel">SwitchLabel</a>}
</div>
</div>
<div class="production"><a name="jls-SwitchLabel"></a>
<div class="lhs">SwitchLabel:</div>
<div class="rhs">
<code class="literal">case</code> <a href="jls-15.html#jls-ConstantExpression" title="ConstantExpression">ConstantExpression</a> <code class="literal">:</code> <br>
<code class="literal">case</code> <a href="jls-14.html#jls-EnumConstantName" title="EnumConstantName">EnumConstantName</a> <code class="literal">:</code> <br>
<code class="literal">default</code> <code class="literal">:</code>
</div>
</div>
<div class="production"><a name="jls-EnumConstantName"></a>
<div class="lhs">EnumConstantName:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
</div>
</div>
</div>
<p class="norm-error"><a name="jls-14.11-200"></a>The type of the
<span class="emphasis"><em>Expression</em></span> must be <code class="literal">char</code>, <code class="literal">byte</code>, <code class="literal">short</code>, <code class="literal">int</code>, <code class="literal">Character</code>,
<code class="literal">Byte</code>, <code class="literal">Short</code>, <code class="literal">Integer</code>, <code class="literal">String</code>, or an enum
type (<a class="xref" href="jls-8.html#jls-8.9" title="8.9.&nbsp;Enum Types">&sect;8.9</a>), or a compile-time error
occurs.
</p>
<p class="norm-static"><a name="jls-14.11-300"></a>The body of
a <code class="literal">switch</code> statement is known as a <span class="emphasis"><em>switch
block</em></span>. Any statement immediately contained by the switch
block may be labeled with one or more <span class="emphasis"><em>switch
labels</em></span>, which are <code class="literal">case</code> or <code class="literal">default</code> labels. Every <code class="literal">case</code>
label has a <code class="literal">case</code> constant, which is either a constant expression or
the name of an enum constant. Switch labels and their <code class="literal">case</code> constants
are said to be <span class="emphasis"><em>associated</em></span> with the <code class="literal">switch</code>
statement.
</p>
<p class="norm-error"><a name="jls-14.11-310"></a>Given a
<code class="literal">switch</code> statement, all of the following must be true or a
compile-time error occurs:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-14.11-310-A"></a>
Every <code class="literal">case</code> constant associated with the <code class="literal">switch</code> statement
must be assignment compatible with the type of the <code class="literal">switch</code>
statement's <span class="emphasis"><em>Expression</em></span> (<a class="xref" href="jls-5.html#jls-5.2" title="5.2.&nbsp;Assignment Contexts">&sect;5.2</a>).
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-14.11-310-B"></a>
If the type of the <code class="literal">switch</code> statement's <span class="emphasis"><em>Expression</em></span> is an enum
type, then every <code class="literal">case</code> constant associated with the <code class="literal">switch</code>
statement must be an enum constant of that type.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-14.11-310-C"></a>
No two of the <code class="literal">case</code> constants associated with the <code class="literal">switch</code>
statement have the same value.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-14.11-310-D"></a>
No <code class="literal">case</code> constant associated with the <code class="literal">switch</code> statement is
<code class="literal">null</code>.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-14.11-310-E"></a>
At most one <code class="literal">default</code> label is associated with the <code class="literal">switch</code>
statement.
</p>
</li>
</ul>
</div>
<p class="note">The prohibition against using <code class="literal">null</code> as a <code class="literal">case</code>
constant prevents code being written that can never be executed. If
the <code class="literal">switch</code> statement's <span class="emphasis"><em>Expression</em></span> is of a
reference type, that is, <code class="literal">String</code> or a boxed primitive type or an enum
type, then an exception will be thrown will occur if
the <span class="emphasis"><em>Expression</em></span> evaluates to <code class="literal">null</code> at run
time. In the judgment of the designers of the Java programming language, this is a
better outcome than silently skipping the entire <code class="literal">switch</code> statement or
choosing to execute the statements (if any) after the <code class="literal">default</code> label
(if any).
</p>
<p class="note">A Java compiler is encouraged (but not required) to
provide a warning if a <code class="literal">switch</code> on an enum-valued expression lacks a
<code class="literal">default</code> label and lacks <code class="literal">case</code> labels for one or more of the enum's
constants. Such a <code class="literal">switch</code> will silently do nothing if the expression
evaluates to one of the missing constants.
</p>
<div class="informalexample">
<p class="note">In C and C++ the body of a <code class="literal">switch</code> statement can be
a statement and statements with <code class="literal">case</code> labels do not have to be
immediately contained by that statement. Consider the simple
loop:
</p><pre class="screen">
for (i = 0; i &lt; n; ++i) foo();
</pre><p class="note">where <code class="literal">n</code> is known to be
positive. A trick known as <span class="emphasis"><em>Duff's device</em></span> can be
used in C or C++ to unroll the loop, but this is not valid code in the
Java programming language:
</p><pre class="screen">
int q = (n+7)/8;
switch (n%8) {
case 0: do { foo(); // Great C hack, Tom,
case 7: foo(); // but it's not valid here.
case 6: foo();
case 5: foo();
case 4: foo();
case 3: foo();
case 2: foo();
case 1: foo();
} while (--q &gt; 0);
}
</pre><p class="note">Fortunately, this trick does not seem to be widely
known or used. Moreover, it is less needed nowadays; this sort of code
transformation is properly in the province of state-of-the-art
optimizing compilers.
</p>
</div>
<p class="norm-dynamic"><a name="jls-14.11-400"></a>
When the <code class="literal">switch</code> statement is executed, first the <span class="emphasis"><em>Expression</em></span> is
evaluated. If the <span class="emphasis"><em>Expression</em></span> evaluates to <code class="literal">null</code>, a <code class="literal">NullPointerException</code> is thrown
and the entire <code class="literal">switch</code> statement completes abruptly for that
reason. Otherwise, if the result is of a reference type, it is subject
to 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>).
</p>
<p class="norm-dynamic"><a name="jls-14.11-410"></a>
If evaluation of the <span class="emphasis"><em>Expression</em></span> or the subsequent unboxing
conversion (if any) completes abruptly for some reason, the <code class="literal">switch</code>
statement completes abruptly for the same reason.
</p>
<p class="norm-dynamic"><a name="jls-14.11-420"></a>
Otherwise, execution continues by comparing the value of the
<span class="emphasis"><em>Expression</em></span> with each <code class="literal">case</code> constant, and there is a choice:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.11-420-A"></a>
If one of the <code class="literal">case</code> constants is equal to the value of the
expression, then we say that the <code class="literal">case</code>
label <span class="emphasis"><em>matches</em></span>. All statements after the
matching <code class="literal">case</code> label in the <code class="literal">switch</code> block, if any, are
executed in sequence.
</p>
<p class="norm-dynamic"><a name="jls-14.11-420-A.1"></a>
If all these statements complete normally, or if there are no
statements after the matching <code class="literal">case</code> label, then the entire
<code class="literal">switch</code> statement completes normally.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.11-420-B"></a>
If no <code class="literal">case</code> label matches but there is a <code class="literal">default</code> label, then
all statements after the <code class="literal">default</code> label in the
<code class="literal">switch</code> block, if any, are executed in sequence.
</p>
<p class="norm-dynamic"><a name="jls-14.11-420-B.1"></a>
If all these statements complete normally, or if there are no
statements after the <code class="literal">default</code> label, then the entire <code class="literal">switch</code>
statement completes normally.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.11-420-C"></a>
If no <code class="literal">case</code> label matches and there is no <code class="literal">default</code> label, then
no further action is taken and the <code class="literal">switch</code> statement completes
normally.
</p>
</li>
</ul>
</div>
<p class="norm-dynamic"><a name="jls-14.11-430"></a>
If any statement immediately contained by the <span class="emphasis"><em>Block</em></span> body of the
<code class="literal">switch</code> statement completes abruptly, it is handled as
follows:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.11-430-A"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly because of a
<code class="literal">break</code> with no label, no further action is taken and the
<code class="literal">switch</code> statement completes normally.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.11-430-B"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly for any other
reason, the <code class="literal">switch</code> statement completes abruptly for the same
reason.
</p>
<p class="note">The case of abrupt completion because of a
<code class="literal">break</code> with a label is handled by the general rule for labeled
statements (<a class="xref" href="jls-14.html#jls-14.7" title="14.7.&nbsp;Labeled Statements">&sect;14.7</a>).
</p>
</li>
</ul>
</div>
<div class="example"><a name="d5e20563"></a><p class="title"><b>Example&nbsp;14.11-1.&nbsp;Fall-Through in the <code class="literal">switch</code> Statement</b></p>
<div class="example-contents">
<p class="note">As in C and C++, execution of statements in a
<code class="literal">switch</code> block "falls through labels."
</p>
<p class="note">For example, the program:</p><pre class="programlisting">
class TooMany {
static void howMany(int k) {
switch (k) {
case 1: System.out.print("one ");
case 2: System.out.print("too ");
case 3: System.out.println("many");
}
}
public static void main(String[] args) {
howMany(3);
howMany(2);
howMany(1);
}
}
</pre><p class="note">contains a <code class="literal">switch</code> block in which the code for each
<code class="literal">case</code> falls through into the code for the next <code class="literal">case</code>. As a result,
the program prints:
</p><pre class="screen">
many
too many
one too many
</pre><p class="note">If code is not to fall through <code class="literal">case</code> to <code class="literal">case</code> in
this manner, then <code class="literal">break</code> statements should be used, as in this
example:
</p><pre class="programlisting">
class TwoMany {
static void howMany(int k) {
switch (k) {
case 1: System.out.println("one");
break; // exit the switch
case 2: System.out.println("two");
break; // exit the switch
case 3: System.out.println("many");
break; // not needed, but good style
}
}
public static void main(String[] args) {
howMany(1);
howMany(2);
howMany(3);
}
}
</pre><p class="note">This program prints:</p><pre class="screen">
one
two
many
</pre></div>
</div><br class="example-break"></div>
<div class="section" title="14.12.&nbsp;The while Statement">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.12"></a>14.12.&nbsp;The <code class="literal">while</code> Statement
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-14.12-100"></a>The <code class="literal">while</code> statement
executes an <span class="emphasis"><em>Expression</em></span> and a <span class="emphasis"><em>Statement</em></span> repeatedly until the value
of the <span class="emphasis"><em>Expression</em></span> is <code class="literal">false</code>.
</p>
<div id="jls-14.12-110" class="productionset"><a name="jls-14.12-110"></a>
<div class="production"><a name="jls-WhileStatement"></a>
<div class="lhs">WhileStatement:</div>
<div class="rhs">
<code class="literal">while</code> <code class="literal">(</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">)</code>
<a href="jls-14.html#jls-Statement" title="Statement">Statement</a>
</div>
</div>
<div class="production"><a name="jls-WhileStatementNoShortIf"></a>
<div class="lhs">WhileStatementNoShortIf:</div>
<div class="rhs">
<code class="literal">while</code> <code class="literal">(</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">)</code>
<a href="jls-14.html#jls-StatementNoShortIf" title="StatementNoShortIf">StatementNoShortIf</a>
</div>
</div>
</div>
<p class="norm-error"><a name="jls-14.12-200"></a>The
<span class="emphasis"><em>Expression</em></span> must have type <code class="literal">boolean</code> or <code class="literal">Boolean</code>, or a compile-time
error occurs.
</p>
<p class="norm-dynamic"><a name="jls-14.12-300"></a>
A <code class="literal">while</code> statement is executed by first evaluating the
<span class="emphasis"><em>Expression</em></span>. If the result is of type <code class="literal">Boolean</code>, it is subject to
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>).
</p>
<p class="norm-dynamic"><a name="jls-14.12-310"></a>
If evaluation of the <span class="emphasis"><em>Expression</em></span> or the subsequent unboxing
conversion (if any) completes abruptly for some reason, the <code class="literal">while</code>
statement completes abruptly for the same reason.
</p>
<p class="norm-dynamic"><a name="jls-14.12-320"></a>
Otherwise, execution continues by making a choice based on the
resulting value:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.12-320-A"></a>
If the value is <code class="literal">true</code>, then the contained <span class="emphasis"><em>Statement</em></span> is
executed. Then there is a choice:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.12-320-A-A"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes normally, then the
entire <code class="literal">while</code> statement is executed again, beginning by
re-evaluating the <span class="emphasis"><em>Expression</em></span>.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.12-320-A-B"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly, see
<a class="xref" href="jls-14.html#jls-14.12.1" title="14.12.1.&nbsp;Abrupt Completion of while Statement">&sect;14.12.1</a>.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.12-320-B"></a>
If the (possibly unboxed) value of the <span class="emphasis"><em>Expression</em></span> is <code class="literal">false</code>,
no further action is taken and the <code class="literal">while</code> statement completes
normally.
</p>
<p class="note">If the (possibly unboxed) value of the
<span class="emphasis"><em>Expression</em></span> is <code class="literal">false</code> the first time it is evaluated, then the
<span class="emphasis"><em>Statement</em></span> is not executed.
</p>
</li>
</ul>
</div>
<div class="section" title="14.12.1.&nbsp;Abrupt Completion of while Statement">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-14.12.1"></a>14.12.1.&nbsp;Abrupt Completion of <code class="literal">while</code> Statement
</h3>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.12.1-100"></a>
Abrupt completion of the contained <span class="emphasis"><em>Statement</em></span> is handled in the
following manner:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.12.1-100-A"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly because of a
<code class="literal">break</code> with no label, no further action is taken and the
<code class="literal">while</code> statement completes normally.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.12.1-100-B"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly because of a
<code class="literal">continue</code> with no label, then the entire <code class="literal">while</code> statement is
executed again.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.12.1-100-C"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly because of a
<code class="literal">continue</code> with label <code class="varname">L</code>, then there is a choice:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.12.1-100-C-A"></a>
If the <code class="literal">while</code> statement has label <code class="varname">L</code>, then the entire
<code class="literal">while</code> statement is executed again.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.12.1-100-C-B"></a>
If the <code class="literal">while</code> statement does not have label <code class="varname">L</code>, the
<code class="literal">while</code> statement completes abruptly because of a <code class="literal">continue</code>
with label <code class="varname">L</code>.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.12.1-100-D"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly for any other
reason, the <code class="literal">while</code> statement completes abruptly for the same
reason.
</p>
<p class="note">The case of abrupt completion because of a
<code class="literal">break</code> with a label is handled by the general rule for labeled
statements (<a class="xref" href="jls-14.html#jls-14.7" title="14.7.&nbsp;Labeled Statements">&sect;14.7</a>).
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="section" title="14.13.&nbsp;The do Statement">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.13"></a>14.13.&nbsp;The <code class="literal">do</code> Statement
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-14.13-100"></a>The <code class="literal">do</code> statement executes a
<span class="emphasis"><em>Statement</em></span> and an <span class="emphasis"><em>Expression</em></span> repeatedly until the value of the
<span class="emphasis"><em>Expression</em></span> is <code class="literal">false</code>.
</p>
<div id="jls-14.13-110" class="productionset"><a name="jls-14.13-110"></a>
<div class="production"><a name="jls-DoStatement"></a>
<div class="lhs">DoStatement:</div>
<div class="rhs">
<code class="literal">do</code> <a href="jls-14.html#jls-Statement" title="Statement">Statement</a> <code class="literal">while</code>
<code class="literal">(</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">)</code> <code class="literal">;</code>
</div>
</div>
</div>
<p class="norm-error"><a name="jls-14.13-200"></a>The
<span class="emphasis"><em>Expression</em></span> must have type <code class="literal">boolean</code> or <code class="literal">Boolean</code>, or a compile-time
error occurs.
</p>
<p class="norm-dynamic"><a name="jls-14.13-300"></a>
A <code class="literal">do</code> statement is executed by first executing the <span class="emphasis"><em>Statement</em></span>. Then
there is a choice:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13-300-A"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes normally, then the
<span class="emphasis"><em>Expression</em></span> is evaluated. If the result is of type <code class="literal">Boolean</code>,
it is subject to 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>).
</p>
<p class="norm-dynamic"><a name="jls-14.13-300-A.1"></a>
If evaluation of the <span class="emphasis"><em>Expression</em></span> or the subsequent unboxing
conversion (if any) completes abruptly for some reason, the <code class="literal">do</code>
statement completes abruptly for the same reason.
</p>
<p class="norm-dynamic"><a name="jls-14.13-300-A.2"></a>
Otherwise, there is a choice based on the resulting value:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13-300-A.2-A"></a>
If the value is <code class="literal">true</code>, then the entire <code class="literal">do</code> statement is
executed again.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13-300-A.2-B"></a>
If the value is <code class="literal">false</code>, no further action is taken and the
<code class="literal">do</code> statement completes normally.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13-300-B"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly, see
<a class="xref" href="jls-14.html#jls-14.13.1" title="14.13.1.&nbsp;Abrupt Completion of do Statement">&sect;14.13.1</a>.
</p>
</li>
</ul>
</div>
<p class="note">Executing a <code class="literal">do</code> statement always executes the
contained <span class="emphasis"><em>Statement</em></span> at least once.
</p>
<div class="section" title="14.13.1.&nbsp;Abrupt Completion of do Statement">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-14.13.1"></a>14.13.1.&nbsp;Abrupt Completion of <code class="literal">do</code> Statement
</h3>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.13.1-100"></a>
Abrupt completion of the contained <span class="emphasis"><em>Statement</em></span> is handled in the
following manner:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13.1-100-A"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly because of a
<code class="literal">break</code> with no label, then no further action is taken and the
<code class="literal">do</code> statement completes normally.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13.1-100-B"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly because of a
<code class="literal">continue</code> with no label, then the <span class="emphasis"><em>Expression</em></span> is
evaluated. Then there is a choice based on the resulting
value:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13.1-100-B-A"></a>
If the value is <code class="literal">true</code>, then the entire <code class="literal">do</code> statement is
executed again.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13.1-100-B-B"></a>
If the value is <code class="literal">false</code>, no further action is taken and the
<code class="literal">do</code> statement completes normally.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13.1-100-C"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly because of a
<code class="literal">continue</code> with label <code class="varname">L</code>, then there is a choice:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13.1-100-C-A"></a>
If the <code class="literal">do</code> statement has label <code class="varname">L</code>, then the <span class="emphasis"><em>Expression</em></span>
is evaluated. Then there is a choice:
</p>
<div class="norm">
<ul class="norm" type="square">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13.1-100-C-A-A"></a>
If the value of the <span class="emphasis"><em>Expression</em></span> is <code class="literal">true</code>, then the
entire <code class="literal">do</code> statement is executed again.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13.1-100-C-A-B"></a>
If the value of the <span class="emphasis"><em>Expression</em></span> is <code class="literal">false</code>, no further
action is taken and the <code class="literal">do</code> statement completes
normally.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13.1-100-C-B"></a>
If the <code class="literal">do</code> statement does not have label <code class="varname">L</code>, the <code class="literal">do</code>
statement completes abruptly because of a <code class="literal">continue</code> with
label <code class="varname">L</code>.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.13.1-100-D"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly for any other
reason, the <code class="literal">do</code> statement completes abruptly for the same
reason.
</p>
<p class="note">The case of abrupt completion because of a
<code class="literal">break</code> with a label is handled by the general rule for labeled
statements (<a class="xref" href="jls-14.html#jls-14.7" title="14.7.&nbsp;Labeled Statements">&sect;14.7</a>).
</p>
</li>
</ul>
</div>
</div>
<div class="example"><a name="d5e20801"></a><p class="title"><b>Example&nbsp;14.13-1.&nbsp;The <code class="literal">do</code> Statement</b></p>
<div class="example-contents">
<p class="note">The following code is one possible implementation of
the <code class="literal">toHexString</code> method of class <code class="literal">Integer</code>:
</p><pre class="programlisting">
public static String toHexString(int i) {
StringBuffer buf = new StringBuffer(8);
do {
buf.append(Character.forDigit(i &amp; 0xF, 16));
i &gt;&gt;&gt;= 4;
} while (i != 0);
return buf.reverse().toString();
}
</pre><p class="note">Because at least one digit must be generated, the
<code class="literal">do</code> statement is an appropriate control structure.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="14.14.&nbsp;The for Statement">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-14.14"></a>14.14.&nbsp;The <code class="literal">for</code> Statement
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-14.14-100"></a>The <code class="literal">for</code> statement has two
forms:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-14.14-100-A"></a>
The basic <code class="literal">for</code> statement.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-14.14-100-B"></a>
The enhanced <code class="literal">for</code> statement
</p>
</li>
</ul>
</div>
<div id="jls-14.14-110" class="productionset"><a name="jls-14.14-110"></a>
<div class="production"><a name="jls-ForStatement"></a>
<div class="lhs">ForStatement:</div>
<div class="rhs">
<a href="jls-14.html#jls-BasicForStatement" title="BasicForStatement">BasicForStatement</a> <br>
<a href="jls-14.html#jls-EnhancedForStatement" title="EnhancedForStatement">EnhancedForStatement</a>
</div>
</div>
<div class="production"><a name="jls-ForStatementNoShortIf"></a>
<div class="lhs">ForStatementNoShortIf:</div>
<div class="rhs">
<a href="jls-14.html#jls-BasicForStatementNoShortIf" title="BasicForStatementNoShortIf">BasicForStatementNoShortIf</a> <br>
<a href="jls-14.html#jls-EnhancedForStatementNoShortIf" title="EnhancedForStatementNoShortIf">EnhancedForStatementNoShortIf</a>
</div>
</div>
</div>
<div class="section" title="14.14.1.&nbsp;The basic for Statement">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-14.14.1"></a>14.14.1.&nbsp;The basic <code class="literal">for</code> Statement
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-14.14.1-100"></a>The basic <code class="literal">for</code> statement
executes some initialization code, then executes an <span class="emphasis"><em>Expression</em></span>, a
<span class="emphasis"><em>Statement</em></span>, and some update code repeatedly until the value of the
<span class="emphasis"><em>Expression</em></span> is <code class="literal">false</code>.
</p>
<div id="jls-14.14.1-110" class="productionset"><a name="jls-14.14.1-110"></a>
<div class="production"><a name="jls-BasicForStatement"></a>
<div class="lhs">BasicForStatement:</div>
<div class="rhs">
<code class="literal">for</code> <code class="literal">(</code>
[<a href="jls-14.html#jls-ForInit" title="ForInit">ForInit</a>] <code class="literal">;</code>
[<a href="jls-15.html#jls-Expression" title="Expression">Expression</a>] <code class="literal">;</code>
[<a href="jls-14.html#jls-ForUpdate" title="ForUpdate">ForUpdate</a>]
<code class="literal">)</code> <a href="jls-14.html#jls-Statement" title="Statement">Statement</a>
</div>
</div>
<div class="production"><a name="jls-BasicForStatementNoShortIf"></a>
<div class="lhs">BasicForStatementNoShortIf:</div>
<div class="rhs">
<code class="literal">for</code> <code class="literal">(</code>
[<a href="jls-14.html#jls-ForInit" title="ForInit">ForInit</a>] <code class="literal">;</code>
[<a href="jls-15.html#jls-Expression" title="Expression">Expression</a>] <code class="literal">;</code>
[<a href="jls-14.html#jls-ForUpdate" title="ForUpdate">ForUpdate</a>]
<code class="literal">)</code> <a href="jls-14.html#jls-StatementNoShortIf" title="StatementNoShortIf">StatementNoShortIf</a>
</div>
</div>
<div class="production"><a name="jls-ForInit"></a>
<div class="lhs">ForInit:</div>
<div class="rhs">
<a href="jls-14.html#jls-StatementExpressionList" title="StatementExpressionList">StatementExpressionList</a> <br>
<a href="jls-14.html#jls-LocalVariableDeclaration" title="LocalVariableDeclaration">LocalVariableDeclaration</a>
</div>
</div>
<div class="production"><a name="jls-ForUpdate"></a>
<div class="lhs">ForUpdate:</div>
<div class="rhs"><a href="jls-14.html#jls-StatementExpressionList" title="StatementExpressionList">StatementExpressionList</a></div>
</div>
<div class="production"><a name="jls-StatementExpressionList"></a>
<div class="lhs">StatementExpressionList:</div>
<div class="rhs">
<a href="jls-14.html#jls-StatementExpression" title="StatementExpression">StatementExpression</a> {<code class="literal">,</code> <a href="jls-14.html#jls-StatementExpression" title="StatementExpression">StatementExpression</a>}
</div>
</div>
</div>
<p class="norm-error"><a name="jls-14.14.1-200"></a>The
<span class="emphasis"><em>Expression</em></span> must have type <code class="literal">boolean</code> or <code class="literal">Boolean</code>, or a compile-time
error occurs.
</p>
<p class="norm-static"><a name="jls-14.14.1-300"></a>The scope and shadowing of a local
variable declared in the <span class="emphasis"><em>ForInit</em></span> part of a basic
<code class="literal">for</code> statement 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> and
<a class="xref" href="jls-6.html#jls-6.4" title="6.4.&nbsp;Shadowing and Obscuring">&sect;6.4</a>.
</p>
<div class="section" title="14.14.1.1.&nbsp;Initialization of for Statement">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="jls-14.14.1.1"></a>14.14.1.1.&nbsp;Initialization of <code class="literal">for</code> Statement
</h4>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.14.1.1-100"></a>
A <code class="literal">for</code> statement is executed by first executing
the <span class="emphasis"><em>ForInit</em></span> code:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.1-100-A"></a>
If the <span class="emphasis"><em>ForInit</em></span> code is a list of statement
expressions (<a class="xref" href="jls-14.html#jls-14.8" title="14.8.&nbsp;Expression Statements">&sect;14.8</a>), the expressions are
evaluated in sequence from left to right; their values, if any,
are discarded.
</p>
<p class="norm-dynamic"><a name="jls-14.14.1.1-100-A.1"></a>
If evaluation of any expression completes abruptly for some
reason, the <code class="literal">for</code> statement completes abruptly for the same
reason; any <span class="emphasis"><em>ForInit</em></span> statement expressions
to the right of the one that completed abruptly are not
evaluated.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.1-100-B"></a>
If the <span class="emphasis"><em>ForInit</em></span> code is a local variable
declaration (<a class="xref" href="jls-14.html#jls-14.4" title="14.4.&nbsp;Local Variable Declaration Statements">&sect;14.4</a>), it is executed as if
it were a local variable declaration statement appearing in a
block.
</p>
<p class="norm-dynamic"><a name="jls-14.14.1.1-100-B.1"></a>
If execution of the local variable declaration completes
abruptly for any reason, the <code class="literal">for</code> statement completes abruptly
for the same reason.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.1-100-C"></a>
If the <span class="emphasis"><em>ForInit</em></span> part is not present, no
action is taken.
</p>
</li>
</ul>
</div>
</div>
<div class="section" title="14.14.1.2.&nbsp;Iteration of for Statement">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="jls-14.14.1.2"></a>14.14.1.2.&nbsp;Iteration of <code class="literal">for</code> Statement
</h4>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.14.1.2-100"></a>
Next, a <code class="literal">for</code> iteration step is performed, as follows:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.2-100-A"></a>
If the <span class="emphasis"><em>Expression</em></span> is present, it is evaluated. If the result
is of type <code class="literal">Boolean</code>, it is subject to 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>).
</p>
<p class="norm-dynamic"><a name="jls-14.14.1.2-100-A.1"></a>
If evaluation of the <span class="emphasis"><em>Expression</em></span> or the subsequent unboxing
conversion (if any) completes abruptly, the <code class="literal">for</code> statement
completes abruptly for the same reason.
</p>
<p class="note">Otherwise, there is then a choice based on the
presence or absence of the <span class="emphasis"><em>Expression</em></span> and the resulting value
if the <span class="emphasis"><em>Expression</em></span> is present; see next bullet.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.2-100-B"></a>
If the <span class="emphasis"><em>Expression</em></span> is not present, or it is present and the
value resulting from its evaluation (including any possible
unboxing) is <code class="literal">true</code>, then the contained <span class="emphasis"><em>Statement</em></span> is
executed. Then there is a choice:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.2-100-B-A"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes normally, then the
following two steps are performed in sequence:
</p>
<div class="orderedlist">
<ol class="orderedlist" type="1">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.2-100-B-A-A"></a>
First, if the <span class="emphasis"><em>ForUpdate</em></span> part is
present, the expressions are evaluated in sequence from
left to right; their values, if any, are discarded. If
evaluation of any expression completes abruptly for some
reason, the <code class="literal">for</code> statement completes abruptly for the
same reason; any <span class="emphasis"><em>ForUpdate</em></span>
statement expressions to the right of the one that
completed abruptly are not evaluated.
</p>
<p class="norm-dynamic"><a name="jls-14.14.1.2-100-B-A-A.1"></a>
If the <span class="emphasis"><em>ForUpdate</em></span> part is not
present, no action is taken.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.2-100-B-A-B"></a>
Second, another <code class="literal">for</code> iteration step is performed.
</p>
</li>
</ol>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.2-100-B-B"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly, see
<a class="xref" href="jls-14.html#jls-14.14.1.3" title="14.14.1.3.&nbsp;Abrupt Completion of for Statement">&sect;14.14.1.3</a>.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.2-100-C"></a>
If the <span class="emphasis"><em>Expression</em></span> is present and the value resulting from its
evaluation (including any possible unboxing) is <code class="literal">false</code>, no
further action is taken and the <code class="literal">for</code> statement completes
normally.
</p>
<p class="note">If the (possibly unboxed) value of the
<span class="emphasis"><em>Expression</em></span> is <code class="literal">false</code> the first time it is evaluated, then the
<span class="emphasis"><em>Statement</em></span> is not executed.
</p>
</li>
</ul>
</div>
<p class="norm-dynamic"><a name="jls-14.14.1.2-200"></a>
If the <span class="emphasis"><em>Expression</em></span> is not present, then the only way a <code class="literal">for</code>
statement can complete normally is by use of a <code class="literal">break</code>
statement.
</p>
</div>
<div class="section" title="14.14.1.3.&nbsp;Abrupt Completion of for Statement">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="jls-14.14.1.3"></a>14.14.1.3.&nbsp;Abrupt Completion of <code class="literal">for</code> Statement
</h4>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-14.14.1.3-100"></a>
Abrupt completion of the contained <span class="emphasis"><em>Statement</em></span> is handled in the
following manner:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-A"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly because of a
<code class="literal">break</code> with no label, no further action is taken and the <code class="literal">for</code>
statement completes normally.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-B"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly because of a
<code class="literal">continue</code> with no label, then the following two steps are
performed in sequence:
</p>
<div class="orderedlist">
<ol class="orderedlist" type="1">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-B-A"></a>
First, if the <span class="emphasis"><em>ForUpdate</em></span> part is
present, the expressions are evaluated in sequence from left
to right; their values, if any, are discarded.
</p>
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-B-A.1"></a>
If the <span class="emphasis"><em>ForUpdate</em></span> part is not present,
no action is taken.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-B-B"></a>
Second, another <code class="literal">for</code> iteration step is performed.
</p>
</li>
</ol>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-C"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly because of a
<code class="literal">continue</code> with label <code class="varname">L</code>, then there is a choice:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-C-A"></a>
If the <code class="literal">for</code> statement has label <code class="varname">L</code>, then the following two
steps are performed in sequence:
</p>
<div class="orderedlist">
<ol class="orderedlist" type="1">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-C-A-A"></a>
First, if the <span class="emphasis"><em>ForUpdate</em></span> part is
present, the expressions are evaluated in sequence from
left to right; their values, if any, are
discarded.
</p>
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-C-A-A.1"></a>
If the <span class="emphasis"><em>ForUpdate</em></span> is not present, no
action is taken.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-C-A-B"></a>
Second, another <code class="literal">for</code> iteration step is performed.
</p>
</li>
</ol>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-C-B"></a>
If the <code class="literal">for</code> statement does not have label <code class="varname">L</code>, the <code class="literal">for</code>
statement completes abruptly because of a <code class="literal">continue</code> with
label <code class="varname">L</code>.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-14.14.1.3-100-D"></a>
If execution of the <span class="emphasis"><em>Statement</em></span> completes abruptly for any other
reason, the <code class="literal">for</code> statement completes abruptly for the same
reason.
</p>
<p class="note">Note that the case of abrupt completion because
of a <code class="literal">break</code> with a label is handled by the general rule for
labeled statements (<a class="xref" href="jls-14.html#jls-14.7" title="14.7.&nbsp;Labeled Statements">&sect;14.7</a>).
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="section" title="14.14.2.&nbsp;The enhanced for statement">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-14.14.2"></a>14.14.2.&nbsp;The enhanced <code class="literal">for</code> statement
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-14.14.2-100"></a>The
enhanced <code class="literal">for</code> statement has the form:
</p>
<div id="jls-14.14.2-110" class="productionset"><a name="jls-14.14.2-110"></a>
<div class="production"><a name="jls-EnhancedForStatement"></a>
<div class="lhs">EnhancedForStatement:</div>
<div class="rhs">
<code class="literal">for</code> <code class="literal">(</code>
{<a href="jls-8.html#jls-VariableModifier" title="VariableModifier">VariableModifier</a>}
<a href="jls-8.html#jls-UnannType" title="UnannType">UnannType</a>
<a href="jls-8.html#jls-VariableDeclaratorId" title="VariableDeclaratorId">VariableDeclaratorId</a>
<code class="literal">:</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a>
<code class="literal">)</code>
<a href="jls-14.html#jls-Statement" title="Statement">Statement</a>