⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 14.doc.html

📁 java语言规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="45371"></a>
<h3>14.12.3    Abrupt Completion of <code>for</code> statement</h3>
<a name="43248"></a>
Abrupt completion of the contained <i>Statement</i> is handled in the following manner:
<p><ul><a name="6095"></a>
<li>If execution of the <i>Statement</i> completes abruptly because of a <code>break</code> with no label, no further action is taken and the <code>for</code> statement completes normally.
<a name="6096"></a>
<li>If execution of the <i>Statement</i> completes abruptly because of a <code>continue</code> with no label, then the following two steps are performed in sequence:
<ul>
<a name="6097"></a>
<li>First, if the <i>ForUpdate</i> part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If the <i>ForUpdate</i> &#32;part is not present, no action is taken.
<a name="6098"></a>
<li>Second, another <code>for</code> iteration step is performed.
</ul>
<a name="79161"></a>
<li>If execution of the <i>Statement</i> completes abruptly because of a <code>continue</code> with label <i>L</i>, then there is a choice:
<ul>
<a name="79162"></a>
<li>If the <code>for</code> statement has label <i>L</i>, then the following two steps are performed in sequence:
<ul>
<a name="79163"></a>
<li>First, if the <i>ForUpdate</i> part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If the<i> ForUpdate</i> is not present, no action is taken.
<a name="79164"></a>
<li>Second, another <code>for</code> iteration step is performed.
</ul>
<a name="79165"></a>
<li>If the <code>for</code> statement does not have label <i>L</i>, the <code>for</code> statement completes abruptly because of a <code>continue</code> with label <i>L</i>.
</ul>
<a name="6104"></a>
<li>If execution of the <i>Statement</i> completes abruptly for any other reason, the <code>for</code> statement completes abruptly for the same reason. Note that the case of abrupt completion because of a <code>break</code> with a label is handled by the general rule for labeled statements <a href="14.doc.html#78993">(&#167;14.6)</a>.
</ul><a name="6842"></a>
<h2>14.13    The <code>break</code> Statement</h2>
<a name="35540"></a>
A break statement transfers control out of an enclosing statement.
<p><ul><pre>
<i>BreakStatement:<br>
</i>	<code>break </code><i>Identifier</i><sub><i>opt</i></sub><code> ;
</code></pre></ul><a name="43392"></a>
A <code>break</code> statement with no label attempts to transfer control to the innermost enclosing <code>switch</code>, <code>while</code>, <code>do</code>, or <code>for</code> statement; this statement, which is called the <i>break target</i>, then immediately completes normally. To be precise, a <code>break</code> statement with no label always completes abruptly, the reason being a <code>break</code> with no label. If no <code>switch</code>, <code>while</code>, <code>do</code>, or <code>for</code> statement encloses the <code>break</code> statement, a compile-time error occurs.<p>
<a name="79188"></a>
A <code>break</code> statement with label <i>Identifier</i> attempts to transfer control to the enclosing labeled statement <a href="14.doc.html#78993">(&#167;14.6)</a> that has the same <i>Identifier</i> as its label; this statement, which is called the <i>break target</i>, then immediately completes normally. In this case, the <code>break</code> target need not be a <code>while</code>, <code>do</code>, <code>for</code>, or <code>switch</code> statement. To be precise, a <code>break</code> statement with label <i>Identifier</i> always completes abruptly, the reason being a <code>break</code> with label <i>Identifier</i>. If no labeled statement with <i>Identifier</i> &#32;as its label encloses the <code>break</code> statement, a compile-time error occurs.<p>
<a name="43504"></a>
It can be seen, then, that a <code>break</code> statement always completes abruptly.<p>
<a name="43404"></a>
The preceding descriptions say "attempts to transfer control" rather than just "transfers control" because if there are any <code>try</code> statements <a href="14.doc.html#79311">(&#167;14.18)</a> within the break target whose <code>try</code> blocks contain the <code>break</code> statement, then any <code>finally</code> clauses of those <code>try</code> statements are executed, in order, innermost to outermost, before control is transferred to the break target. Abrupt completion of a <code>finally</code> clause can disrupt the transfer of control initiated by a <code>break</code> statement.<p>
<a name="18004"></a>
In the following example, a mathematical graph is represented by an array of arrays. A graph consists of a set of nodes and a set of edges; each edge is an arrow that points from some node to some other node, or from a node to itself. In this example it is assumed that there are no redundant edges; that is, for any two nodes <i>P</i> and <i>Q</i>, where <i>Q</i> may be the same as <i>P</i>, there is at most one edge from <i>P</i> to <i>Q</i>. Nodes are represented by integers, and there is an edge from node <i>i</i> to node <code>edges[</code><i>i</i><code>][</code><i>j</i><code>]</code> for every <i>i</i> and <i>j</i> for which the array reference <code>edges[</code><i>i</i><code>][</code><i>j</i><code>]</code> does not throw an <code>IndexOutOfBoundsException</code>.<p>
<a name="42801"></a>
The task of the method <code>loseEdges</code>, given integers <i>i</i> and <i>j</i>, is to construct a new graph by copying a given graph but omitting the edge from node <i>i</i> to node <i>j</i>, if any, and the edge from node <i>j</i> to node <i>i</i>, if any:<p>
<pre><a name="42813"></a>
class Graph {
<a name="42814"></a>	int edges[][];
<a name="42816"></a>	public Graph(int[][] edges) { this.edges = edges; }
<a name="42818"></a>
	public Graph loseEdges(int i, int j) {
<a name="42819"></a>		int n = edges.length;
<a name="42820"></a>		int[][] newedges = new int[n][];
<a name="42821"></a>		for (int k = 0; k &lt; n; ++k) {
<a name="42822"></a>
			edgelist: {
<a name="42823"></a>				int z;
<a name="42824"></a>
				search: {
<a name="42825"></a>					if (k == i) {
<a name="42826"></a>						for (z = 0; z &lt; edges[k].length; ++z)
<a name="42827"></a>							if (edges[k][z] == j)
<a name="42828"></a>								break search;
<a name="42829"></a>					} else if (k == j) {
<a name="42831"></a>						for (z = 0; z &lt; edges[k].length; ++z)
<a name="42832"></a>							if (edges[k][z] == i)
<a name="42833"></a>								break search;
<a name="42834"></a>					}
<a name="42835"></a>					// No edge to be deleted; share this list.
<a name="42836"></a>					newedges[k] = edges[k];
<a name="42837"></a>					break edgelist;
<a name="42838"></a>				}//search
<a name="42839"></a>				// Copy the list, omitting the edge at position z.
<a name="42840"></a>				int m = edges[k].length - 1;
<a name="42841"></a>				int ne[] = new int[m];
<a name="42842"></a>				System.arraycopy(edges[k], 0, ne, 0, z);
<a name="42843"></a>				System.arraycopy(edges[k], z+1, ne, z, m-z);
<a name="42879"></a>				newedges[k] = ne;
<a name="42844"></a>			}//edgelist
<a name="42845"></a>		}
<a name="42846"></a>		return new Graph(newedges);
<a name="42847"></a>	}
<a name="42848"></a>}
</pre><a name="42887"></a>
Note the use of two statement labels, <code>edgelist</code> and <code>search</code>, and the use of <code>break</code> 
statements. This allows the code that copies a list, omitting one edge, to be shared 
between two separate tests, the test for an edge from node <i>i</i> to node <i>j</i>, and the test 
for an edge from node <i>j</i> to node <i>i</i>.
<p><a name="6122"></a>
<h2>14.14    The <code>continue</code> Statement</h2>
<a name="6123"></a>
A <code>continue</code> statement may occur only in a <code>while</code>, <code>do</code>, or <code>for</code> statement; statements
of these three kinds are called <i>iteration statements</i>. Control passes to the 
loop-continuation point of an iteration statement.
<p><ul><pre>
<i>ContinueStatement:<br>
</i>	<code>continue </code><i>Identifier</i><sub><i>opt</i></sub><code> ;
</code></pre></ul><a name="6125"></a>
A <code>continue</code> statement with no label attempts to transfer control to the innermost enclosing <code>while</code>, <code>do</code>, or <code>for</code> statement; this statement, which is called the <i>continue target</i>, then immediately ends the current iteration and begins a new one. To be precise, such a <code>continue</code> statement always completes abruptly, the reason being a <code>continue</code> with no label. If no <code>while</code>, <code>do</code>, or <code>for</code> statement encloses the <code>continue</code> statement, a compile-time error occurs.<p>
<a name="79215"></a>
A <code>continue</code> statement with label <i>Identifier</i> attempts to transfer control to the enclosing labeled statement <a href="14.doc.html#78993">(&#167;14.6)</a> that has the same <i>Identifier</i> as its label; that statement, which is called the <i>continue target</i>, then immediately ends the current iteration and begins a new one. The continue target must be a <code>while</code>, <code>do</code>, or <code>for</code> statement or a compile-time error occurs. More precisely, a <code>continue</code> statement with label <i>Identifier</i> always completes abruptly, the reason being a <code>continue</code> with label <i>Identifier</i>. If no labeled statement with <i>Identifier</i> as its label contains the <code>continue</code> statement, a compile-time error occurs.<p>
<a name="43498"></a>
It can be seen, then, that a <code>continue</code> statement always completes abruptly.<p>
<a name="6136"></a>
See the descriptions of the <code>while</code> statement <a href="14.doc.html#237277">(&#167;14.10)</a>, <code>do</code> statement <a href="14.doc.html#6045">(&#167;14.11)</a>, and <code>for</code> statement <a href="14.doc.html#24588">(&#167;14.12)</a> for a discussion of the handling of abrupt termination because of <code>continue</code>.<p>
<a name="43422"></a>
The preceding descriptions say "attempts to transfer control" rather than just "transfers control" because if there are any <code>try</code> statements <a href="14.doc.html#79311">(&#167;14.18)</a> within the continue target whose <code>try</code> blocks contain the <code>continue</code> statement, then any <code>finally</code> clauses of those <code>try</code> statements are executed, in order, innermost to outermost, before control is transferred to the continue target. Abrupt completion of a <code>finally</code> clause can disrupt the transfer of control initiated by a <code>continue</code> statement.<p>
<a name="42935"></a>
In the <code>Graph</code> example in the preceding section, one of the <code>break</code> statements is used to finish execution of the entire body of the outermost <code>for</code> loop. This <code>break</code> can be replaced by a <code>continue</code> if the <code>for</code> loop itself is labeled:<p>
<pre><a name="42900"></a>
class Graph {
<a name="42901"></a>	. . .
<a name="42903"></a>	public Graph loseEdges(int i, int j) {
<a name="42904"></a>		int n = edges.length;
<a name="42905"></a>		int[][] newedges = new int[n][];
<a name="42906"></a>
		edgelists: for (int k = 0; k &lt; n; ++k) {
<a name="42908"></a>			int z;
<a name="42909"></a>
			search: {
<a name="42989"></a>				if (k == i) {
<a name="42990"></a>					. . .
<a name="42991"></a>				} else if (k == j) {
<a name="42916"></a>					. . .
<a name="42919"></a>				}
<a name="42921"></a>				newedges[k] = edges[k];
<a name="42922"></a>				continue edgelists;
<a name="42923"></a>			}//search
<a name="42925"></a>			. . .
<a name="42983"></a>		}//edgelists
<a name="42932"></a>		return new Graph(newedges);
<a name="42933"></a>	}
<a name="42934"></a>}
</pre><a name="42984"></a>
Which to use, if either, is largely a matter of programming style.
<p><a name="6767"></a>
<h2>14.15    The <code>return</code> Statement</h2>
<a name="124158"></a>
A <code>return</code> statement returns control to the invoker of a method (<a href="8.doc.html#40420">&#167;8.4</a>, <a href="15.doc.html#20448">&#167;15.11</a>) or 
constructor (<a href="8.doc.html#41652">&#167;8.6</a>, <a href="15.doc.html#41147">&#167;15.8</a>).
<p><ul><pre>
<i>ReturnStatement:<br>
</i>	<code>return </code><i>Expression</i><sub><i>opt</i></sub><code> ;
</code></pre></ul><a name="236030"></a>
A <code>return</code> statement with no <i>Expression </i>must be contained in the body of a method that is declared, using the keyword <code>void</code>, not to return any value <a href="8.doc.html#40420">(&#167;8.4)</a>, or in the body of a constructor <a href="8.doc.html#41652">(&#167;8.6)</a>. A compile-time error occurs if a <code>return</code> statement appears within a static initializer <a href="8.doc.html#39245">(&#167;8.5)</a>. A <code>return</code> statement with no <i>Expression</i> attempts to transfer control to the invoker of the method or constructor that contains it. To be precise, a <code>return</code> statement with no <i>Expression</i> always completes abruptly, the reason being a <code>return</code> with no value.<p>
<a name="6148"></a>
A <code>return</code> statement with an <i>Expression</i> must be contained in a method declaration that is declared to return a value <a href="8.doc.html#40420">(&#167;8.4)</a> or a compile-time error occurs. The <i>Expression</i> must denote a variable or value of some type <i>T</i>, or a compile-time error occurs. The type <em>T</em> must be assignable <a href="5.doc.html#170768">(&#167;5.2)</a> to the declared result type of the method, or a compile-time error occurs.<p>
<a name="43489"></a>
A <code>return</code> statement with an <i>Expression</i> attempts to transfer control to the invoker of the method that contains it; the value of the <i>Expression</i> becomes the value of the method invocation. More precisely, execution of such a <code>return</code> statement first evaluates the <i>Expression</i>. If the evaluation of the <i>Expression</i> completes abruptly for some reason, then the <code>return</code> statement completes abruptly for that reason. If evaluation of the <i>Expression</i> completes normally, producing a value <i>V</i>, then the <code>return</code> statement completes abruptly, the reason being a <code>return</code> with value <i>V</i>.<p>
<a name="43495"></a>
It can be seen, then, that a <code>return</code> statement always completes abruptly.<p>
<a name="43508"></a>
The preceding descriptions say "attempts to transfer control" rather than just "transfers control" because if there are any <code>try</code> statements <a href="14.doc.html#79311">(&#167;14.18)</a> within the method or constructor whose <code>try</code> blocks contain the <code>return</code> statement, then any <code>finally</code> clauses of those <code>try</code> statements will be executed, in order, innermost to outermost, before control is transferred to the invoker of the method or constructor. Abrupt completion of a <code>finally</code> clause can disrupt the transfer of control initiated by a <code>return</code> statement.<p>
<a name="237350"></a>
<h2>14.16    The <code>throw</code> Statement</h2>
<a name="79254"></a>
A <code>throw</code> statement causes an exception <a href="11.doc.html#44043">(&#167;11)</a> to be thrown. The result is an immediate
transfer of control <a href="11.doc.html#44153">(&#167;11.3)</a> that may exit multiple statements and multiple 
constructor, static and field initializer evaluations, and method invocations until a 
<code>try</code> statement <a href="14.doc.html#79311">(&#167;14.18)</a> is found that catches the thrown value. If no such <code>try</code> 
statement is found, then execution of the thread (<a href="17.doc.html#26250">&#167;17</a>, <a href="javalang.doc18.html#2658">&#167;20.20</a>) that executed the 
<code>throw</code> is terminated <a href="11.doc.html#44153">(&#167;11.3)</a> after invocation of the <code>UncaughtException</code> method 
<a href="javalang.doc19.html#2901">(&#167;20.21.31)</a> for the thread group to which the thread belongs.
<p><ul><pre>
<i>ThrowStatement:<br>
</i>	<code>throw </code><i>Expression</i><code> ;
</code></pre></ul><a name="236072"></a>
The <i>Expression</i> in a throw statement must denote a variable or value of a reference type which is assignable <a href="5.doc.html#170768">(&#167;5.2)</a> to the type <code>Throwable</code>, or a compile-time error occurs. Moreover, at least one of the following three conditions must be true, or a compile-time error occurs:<p>
<ul><a name="236073"></a>
<li>The exception is not a checked exception <a href="11.doc.html#44121">(&#167;11.2)</a>-specifically, one of the following situations is true:
<ul>
<a name="39922"></a>
<li>The type of the <i>Expression</i> is the class <code>RuntimeException</code> or a subclass of <code>RuntimeException</code>.
<a name="236074"></a>
<li>The type of the <i>Expression</i> is the class <code>Error</code> or a subclass of <code>Error</code>.
</ul>
<a name="236078"></a>
<li>The <code>throw</code> statement is contained in the <code>try</code> block of a <code>try</code> statement <a href="14.doc.html#79311">(&#167;14.18)</a> and the type of the <i>Expression</i> is assignable <a href="5.doc.html#170768">(&#167;5.2)</a> to the type of the parameter of at least one <code>catch</code> clause of the <code>try</code> statement. (In this case we say the thrown value is <i>caught</i> by the <code>try</code> statement.)
<a name="236082"></a>
<li>Th

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -