📄 14.doc.html
字号:
<html>
<head>
<title>The Java Language Specification Blocks and Statements</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="13.doc.html">Prev</a> | <a href="15.doc.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="44383"></a>
<p><strong>
CHAPTER 14 </strong></p>
<a name="101241"></a>
<h1>Blocks and Statements</h1>
<hr><p>
<a name="45234"></a>
The sequence of execution of a Java program is controlled by <i>statements</i>,
which are executed for their effect and do not have values.
<p><a name="17960"></a>
Some statements <i>contain</i> other statements as part of their structure; such other statements are substatements of the statement. We say that statement <i>S</i><i></i> <i>immediately  contains</i> statement <i>U</i><code></code> if there is no statement <i>T</i><em></em> different from <i>S</i><i></i> and <i>U</i><code></code> such that <i>S</i><i></i> contains <i>T</i><em></em> and <i>T</i><em></em> contains <i>U</i>. In the same manner, some statements contain expressions <a href="15.doc.html#4709">(§15)</a> as part of their structure.<p>
<a name="43082"></a>
The first section of this chapter discusses the distinction between normal and abrupt completion of statements <a href="14.doc.html#5894">(§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>
<a name="43210"></a>
Blocks are explained first <a href="14.doc.html#24644">(§14.2)</a>, because they can appear in certain places where other kinds of statements are not allowed, and because one other kind of statement, a local variable declaration statement <a href="14.doc.html#5920">(§14.3)</a>, must be immediately contained within a block.<p>
<a name="43203"></a>
Next a grammatical maneuver is explained that sidesteps the familiar "dangling <code>else</code>" problem <a href="14.doc.html#32584">(§14.4)</a>.<p>
<a name="43118"></a>
Statements that will be familiar to C and C++ programmers are the empty <a href="14.doc.html#5970">(§14.5)</a>, labeled <a href="14.doc.html#78993">(§14.6)</a>, expression <a href="14.doc.html#5984">(§14.7)</a>, <code>if</code> <a href="14.doc.html#5991">(§14.8)</a>, <code>switch</code> <a href="14.doc.html#35518">(§14.9)</a>, <code>while</code> <a href="14.doc.html#237277">(§14.10)</a>, <code>do</code> <a href="14.doc.html#6045">(§14.11)</a>, <code>for</code> <a href="14.doc.html#24588">(§14.12)</a>, <code>break</code> <a href="14.doc.html#6842">(§14.13)</a>, <code>continue</code> <a href="14.doc.html#6122">(§14.14)</a>, and <code>return</code> <a href="14.doc.html#6767">(§14.15)</a> statements.<p>
<a name="43199"></a>
Unlike C and C++, Java has no <code>goto</code> statement. However, the <code>break</code> and <code>continue</code> statements are extended in Java to allow them to mention statement labels.<p>
<a name="43200"></a>
The Java statements that are not in the C language are the <code>throw</code> <a href="14.doc.html#237350">(§14.16)</a>, <code>synchronized</code> <a href="14.doc.html#79287">(§14.17)</a>, and <code>try</code> <a href="14.doc.html#79311">(§14.18)</a> statements.<p>
<a name="43110"></a>
The last section <a href="14.doc.html#236365">(§14.19)</a> of this chapter addresses the requirement that every statement be <i>reachable</i> in a certain technical sense.<p>
<a name="5894"></a>
<h2>14.1 Normal and Abrupt Completion of Statements</h2>
<a name="78796"></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. If all the steps are carried out as described, with no
indication of abrupt completion, the statement is said to <i>complete normally</i>. However,
certain events may prevent a statement from completing normally:
<p><ul><a name="47062"></a>
<li>The <code>break</code> <a href="14.doc.html#6842">(§14.13)</a>, <code>continue</code> <a href="14.doc.html#6122">(§14.14)</a>, and <code>return</code> <a href="14.doc.html#6767">(§14.15)</a> statements cause a transfer of control that may prevent normal completion of statements that contain them.
<a name="78802"></a>
<li>Evaluation of certain Java expressions may throw exceptions from the Java Virtual Machine; these expressions are summarized in <a href="15.doc.html#79448">§15.5</a>. An explicit <code>throw</code> <a href="14.doc.html#237350">(§14.16)</a> statement also results in an exception. An exception causes a transfer of control that may prevent normal completion of statements.
</ul><a name="5898"></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 <i>complete abruptly</i>. An abrupt completion always has an associated <i>reason</i>, which is one of the following:<p>
<ul><a name="5899"></a>
<li>A <code>break</code> with no label
<a name="78812"></a>
<li>A <code>break</code> with a given label
<a name="5901"></a>
<li>A <code>continue</code> with no label
<a name="78822"></a>
<li>A <code>continue</code> with a given label
<a name="5903"></a>
<li>A <code>return</code> with no value
<a name="5904"></a>
<li>A <code>return</code> with a given value
<a name="78827"></a>
<li>A <code>throw</code> with a given value, including exceptions thrown by the Java Virtual Machine
</ul><a name="237105"></a>
The terms "complete normally" and "complete abruptly" also apply to the evaluation of expressions <a href="15.doc.html#79448">(§15.5)</a>. The only reason an expression can complete abruptly is that an exception is thrown, because of either a <code>throw</code> with a given value <a href="14.doc.html#237350">(§14.16)</a> or a run-time exception or error (<a href="11.doc.html#44043">§11</a>, <a href="15.doc.html#79448">§15.5</a>).<p>
<a name="237118"></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>
<a name="24642"></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>
<a name="24647"></a>
Unless otherwise specified, a statement completes normally if all expressions it evaluates and all substatements it executes complete normally.<p>
<a name="24644"></a>
<h2>14.2 Blocks</h2>
<a name="6391"></a>
A <i>block</i> is a sequence of statements and local variable declaration statements
within braces.
<p><ul><pre>
<i>Block:<br>
</i> <code>{ </code><i>BlockStatements</i><sub><i>opt</i></sub><code> }
</code>
<i>BlockStatements:<br>
</i><code> </code><i>BlockStatement<br>
</i><code> </code><i>BlockStatements</i><code> </code><i>BlockStatement
</i>
<i>BlockStatement:<br>
</i> <i>LocalVariableDeclarationStatement<br>
</i> <i>Statement
</i></pre></ul><a name="5918"></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>
<a name="5920"></a>
<h2>14.3 Local Variable Declaration Statements</h2>
<a name="25561"></a>
A <i>local variable declaration statement</i> declares one or more local variable names.
<p><ul><pre>
<i>LocalVariableDeclarationStatement:<br>
</i> <i>LocalVariableDeclaration</i><code> ;
</code>
<i>LocalVariableDeclaration:<br>
</i> <i>Type</i><code> </code><i>VariableDeclarators
</i></pre></ul><a name="238231"></a>
The following are repeated from <a href="8.doc.html#40898">§8.3</a> to make the presentation here clearer:
<p><ul><pre>
<i>VariableDeclarators:<br>
</i> <i>VariableDeclarator<br>
</i> <i>VariableDeclarators</i><code> , </code><i>VariableDeclarator
</i>
<i>VariableDeclarator:<br>
</i> <i>VariableDeclaratorId<br>
</i> <i>VariableDeclaratorId</i><code> = </code><i>VariableInitializer
</i>
<i>VariableDeclaratorId:<br>
</i> <i>Identifier<br>
</i> <i>VariableDeclaratorId</i><code> [ ]
</code>
<i>VariableInitializer:<br>
</i> <i>Expression<br>
</i> <i>ArrayInitializer
</i></pre></ul><a name="32641"></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>
<a name="35375"></a>
A local variable declaration can also appear in the header of a <code>for</code> statement <a href="14.doc.html#24588">(§14.12)</a>. In this case it is executed in the same manner as if it were part of a local variable declaration statement.<p>
<a name="35393"></a>
<h3>14.3.1 Local Variable Declarators and Types</h3>
<a name="35391"></a>
Each <i>declarator</i> in a local variable declaration declares one local variable, whose
name is the <i>Identifier</i> that appears in the declarator.
<p><a name="35396"></a>
The type of the variable is denoted by the <i>Type</i> that appears at the start of the local variable declaration, followed by any bracket pairs that follow the <i>Identifier</i> in the declarator. Thus, the local variable declaration:<p>
<pre><a name="35400"></a>int a, b[], c[][];
</pre><a name="35401"></a>
is equivalent to the series of declarations:
<p><pre><a name="35402"></a>
int a;
<a name="35403"></a>int[] b;
<a name="35404"></a>int[][] c;
</pre><a name="35405"></a>
Brackets are allowed in declarators as a nod to the tradition of C and C++. The
general rule, however, also means that the local variable declaration:
<p><pre><a name="35406"></a>float[][] f[][], g[][][], h[]; // Yechh!
</pre><a name="35409"></a>
is equivalent to the series of declarations:
<p><pre><a name="35410"></a>
float[][][][] f;
<a name="35411"></a>float[][][][][] g;
<a name="35412"></a>float[][][] h;
</pre><a name="35427"></a>
We do not recommend such "mixed notation" for array declarations.
<p><a name="32644"></a>
<h3>14.3.2 Scope of Local Variable Declarations</h3>
<a name="45940"></a>
The scope of a local variable declared in a block is the rest of the block, including
its own initializer. The name of the local variable parameter may not be redeclared
as a local variable or exception parameter within its scope, or a compile-time error
occurs; that is, hiding the name of a local variable is not permitted.
<p><a name="45944"></a>
A local variable cannot be referred to using a qualified name <a href="6.doc.html#33916">(§6.6)</a>, only a simple name.<p>
<a name="45938"></a>
The example:<p>
<pre><a name="32598"></a>
class Test {
<a name="32601"></a> static int x;
<a name="32599"></a> public static void main(String[] args) {
<a name="32600"></a> int x = x;
<a name="32602"></a> }
<a name="32603"></a>}
</pre><a name="32604"></a>
causes a compile-time error because the initialization of <code>x</code> is within the scope of
the declaration of <code>x</code> as a local variable, and the local <code>x</code> does not yet have a value
and cannot be used.
<p><a name="32610"></a>
The following program does compile:<p>
<pre><a name="32613"></a>
class Test {
<a name="32614"></a> static int x;
<a name="32615"></a> public static void main(String[] args) {
<a name="32616"></a> int x = (x=2)*2;
<a name="32619"></a> System.out.println(x);
<a name="32617"></a> }
<a name="32618"></a>}
</pre><a name="32611"></a>
because the local variable <code>x</code> is definitely assigned <a href="16.doc.html#25979">(§16)</a> before it is used. It prints:
<p><pre><a name="236246"></a>4
</pre><a name="5925"></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -