📄 4.doc.html
字号:
<li>An <i>exception-handler parameter</i> is created each time an exception is caught by a <code>catch</code> clause of a <code>try</code> statement <a href="14.doc.html#79311">(§14.18)</a>. The new variable is initialized with the actual object associated with the exception (<a href="11.doc.html#44153">§11.3</a>, <a href="14.doc.html#237350">§14.16</a>). The exception-handler parameter effectively ceases to exist when execution of the block associated with the <code>catch</code> clause is complete.
<a name="24801"></a>
<li><i>Local variables</i> are declared by local variable declaration statements <a href="14.doc.html#5920">(§14.3)</a>. Whenever the flow of control enters a block <a href="14.doc.html#24644">(§14.2)</a> or <code>for</code> statement <a href="14.doc.html#24588">(§14.12)</a>, a new variable is created for each local variable declared in a local variable declaration statement immediately contained within that block or <code>for</code> statement. A local variable declaration statement may contain an expression which initializes the variable. The local variable with an initializing expression is not initialized, however, until the local variable declaration statement that declares it is executed. (The rules of definite assignment <a href="16.doc.html#25979">(§16)</a> prevent the value of a local variable from being used before it has been initialized or otherwise assigned a value.) The local variable effectively ceases to exist when the execution of the block or <code>for</code> statement is complete.
</ol>
<ul><a name="24816"></a>
<br><br>Were it not for one exceptional situation, a local variable could always be regarded as being created when its local variable declaration statement is executed. The exceptional situation involves the <code>switch</code> statement <a href="14.doc.html#35518">(§14.9)</a>, where it is possible for control to enter a block but bypass execution of a local variable declaration statement. Because of the restrictions imposed by the rules of definite assignment <a href="16.doc.html#">(§16)</a>, however, the local variable declared by such a bypassed local variable declaration statement cannot be used before it has been definitely assigned a value by an assignment expression <a href="15.doc.html#5281">(§15.25)</a>.
</ul><a name="24836"></a>
The following example contains several different kinds of variables:<p>
<pre><a name="10918"></a>
class Point {
<a name="45636"></a> static int numPoints; // numPoints is a class variable
<a name="50087"></a> int x, y; // x and y are instance variables
<a name="50089"></a> int[] w = new int[10]; // w[0] is an array component
<a name="52523"></a> int setX(int x) { // x is a method parameter
<a name="52524"></a> int oldx = this.x; // oldx is a local variable
<a name="10925"></a> this.x = x;
<a name="10926"></a> return oldx;
<a name="10927"></a> }
<a name="10928"></a>}
</pre><a name="10931"></a>
<h3>4.5.4 Initial Values of Variables</h3>
<a name="10935"></a>
Every variable in a Java program must have a value before its value is used:
<p><ul><a name="10946"></a>
<li>Each class variable, instance variable, or array component is initialized with a <i>default value</i> when it is created (<a href="15.doc.html#41147">§15.8</a>, <a href="15.doc.html#46168">§15.9</a>, <a href="javalang.doc2.html#15088">§20.3.6</a>):
<ul>
<a name="10947"></a>
<li>For type <code>byte</code>, the default value is zero, that is, the value of <code>(byte)0</code>.
<a name="10948"></a>
<li>For type <code>short</code>, the default value is zero, that is, the value of <code>(short)0</code>.
<a name="10949"></a>
<li>For type <code>int</code>, the default value is zero, that is, <code>0</code>.
<a name="10950"></a>
<li>For type <code>long</code>, the default value is zero, that is, <code>0L</code>.
<a name="10951"></a>
<li>For type <code>float</code>, the default value is positive zero, that is, <code>0.0f</code>.
<a name="46977"></a>
<li>For type <code>double</code>, the default value is positive zero, that is, <code>0.0d</code>.
<a name="46978"></a>
<li>For type <code>char</code>, the default value is the null character, that is, '<code>\u0000</code>'.
<a name="46979"></a>
<li>For type <code>boolean</code>, the default value is <code>false</code>.
<a name="10955"></a>
<li>For all reference types <a href="4.doc.html#9317">(§4.3)</a>, the default value is <code>null</code>.
</ul>
<a name="11353"></a>
<li>Each method parameter <a href="8.doc.html#38698">(§8.4.1)</a> is initialized to the corresponding argument value provided by the invoker of the method <a href="15.doc.html#20448">(§15.11)</a>.
<a name="38160"></a>
<li>Each constructor parameter <a href="8.doc.html#29488">(§8.6.1)</a> is initialized to the corresponding argument value provided by a class instance creation expression <a href="15.doc.html#41147">(§15.8)</a> or explicit constructor invocation <a href="8.doc.html#78435">(§8.6.5)</a>.
<a name="11354"></a>
<li>An exception-handler parameter <a href="14.doc.html#79311">(§14.18)</a> is initialized to the thrown object representing the exception (<a href="11.doc.html#44153">§11.3</a>, <a href="14.doc.html#237350">§14.16</a>).
<a name="11569"></a>
<li>A local variable (<a href="14.doc.html#5920">§14.3</a>, <a href="14.doc.html#24588">§14.12</a>) must be explicitly given a value before it is used, by either initialization <a href="14.doc.html#5920">(§14.3)</a> or assignment <a href="15.doc.html#5281">(§15.25)</a>, in a way that can be verified by the compiler using the rules for definite assignment <a href="16.doc.html#25979">(§16)</a>.
</ul><a name="30962"></a>
The example program:
<p><pre><a name="30963"></a>
class Point {
<a name="12302"></a> static int npoints;
<a name="12303"></a> int x, y;
<a name="12309"></a> Point root;
<a name="12307"></a>}
<br><a name="12308"></a>
class Test {
<a name="12310"></a> public static void main(String[] args) {
<a name="50091"></a> System.out.println("npoints=" + Point.npoints);
<a name="12326"></a> Point p = new Point();
<a name="12327"></a> System.out.println("p.x=" + p.x + ", p.y=" + p.y);
<a name="12328"></a> System.out.println("p.root=" + p.root);
<a name="12332"></a> }
<a name="12329"></a>}
</pre><a name="12324"></a>
prints:
<p><pre><a name="12336"></a>
npoints=0
<a name="12337"></a>p.x=0, p.y=0
<a name="12325"></a>p.root=null
</pre><a name="24882"></a>
illustrating the default initialization of <code>npoints</code>, which occurs when the class
<code>Point</code> is prepared <a href="12.doc.html#47979">(§12.3.2)</a>, and the default initialization of <code>x</code>, <code>y</code>, and <code>root</code>, which
occurs when a new <code>Point</code> is instantiated. See <a href="12.doc.html#44410">§12</a> for a full description of all
aspects of loading, linking, and initialization of classes and interfaces, plus a
description of the instantiation of classes to make new class instances.
<p><a name="24887"></a>
<h3>4.5.5 Variables Have Types, Objects Have Classes</h3>
<a name="24888"></a>
Every object belongs to some particular class: the class that was mentioned in the
creation expression that produced the object, the class whose class object was
used to invoke the <code>newInstance</code> method <a href="javalang.doc2.html#15088">(§20.3.6)</a> to produce the object, or the
<code>String</code> class for objects implicitly created by the string concatenation operator <code>+
</code><a href="15.doc.html#39990">(§15.17.1)</a>. This class is called the <i>class of the object</i>. (Arrays also have a class, as
described at the end of this section.) An object is said to be an instance of its class
and of all superclasses of its class.
<p><a name="24900"></a>
(Sometimes a variable or expression is said to have a "run-time type" but that is an abuse of terminology; it refers to the class of the object referred to by the value of the variable or expression at run time, assuming that the value is not <code>null</code>. Properly speaking, type is a compile-time notion. A variable or expression has a type; an object or array has no type, but belongs to a class.)<p>
<a name="24901"></a>
The type of a variable is always declared, and the type of an expression can be deduced at compile time. The type limits the possible values that the variable can hold or the expression can produce at run time. If a run-time value is a reference that is not <code>null</code>, it refers to an object or array that has a class (not a type), and that class will necessarily be compatible with the compile-time type.<p>
<a name="24895"></a>
Even though a variable or expression may have a compile-time type that is an interface type, there are no instances of interfaces. A variable or expression whose type is an interface type can reference any object whose class implements <a href="8.doc.html#34031">(§8.1.4)</a> that interface.<p>
<a name="24918"></a>
Here is an example of creating new objects and of the distinction between the type of a variable and the class of an object:<p>
<pre><a name="52597"></a>
public interface Colorable {
<a name="24921"></a> void setColor(byte r, byte g, byte b);
<a name="24922"></a>}
<br><a name="52598"></a>class Point { int x, y; }
<a name="52599"></a>
class ColoredPoint extends Point implements Colorable {
<br><a name="52600"></a> byte r, g, b;
<br><a name="52601"></a>
public void setColor(byte rv, byte gv, byte bv) {
<a name="23360"></a> r = rv; g = gv; b = bv;
<a name="23361"></a> }
<br><a name="52604"></a>}
<br><a name="52605"></a>
class Test {
<a name="52606"></a> public static void main(String[] args) {
<a name="52607"></a> Point p = new Point();
<a name="52608"></a> ColoredPoint cp = new ColoredPoint();
<a name="52609"></a> p = cp;
<a name="52610"></a> Colorable c = cp;
<a name="23366"></a> }
<a name="52611"></a>}
</pre><a name="52612"></a>
In this example:
<p><ul><a name="52613"></a>
<li>The local variable <code>p</code> of the method <code>main</code> of class <code>Test</code> has type <code>Point</code> and is initially assigned a reference to a new instance of class <code>Point</code>.
<a name="52614"></a>
<li>The local variable <code>cp</code> similarly has as its type <code>ColoredPoint</code>, and is initially assigned a reference to a new instance of class <code>ColoredPoint</code>.
<a name="52615"></a>
<li>The assignment of the value of <code>cp</code> to the variable <code>p</code> causes <code>p</code> to hold a reference to a <code>ColoredPoint</code> object. This is permitted because <code>ColoredPoint</code> is a subclass of <code>Point</code>, so the class <code>ColoredPoint</code> is assignment compatible <a href="5.doc.html#170768">(§5.2)</a> with the type <code>Point</code>. A <code>ColoredPoint</code> object includes support for all the methods of a <code>Point</code>. In addition to its particular fields <code>r</code>, <code>g</code>, and <code>b</code>, it has the fields of class <code>Point</code>, namely <code>x</code> and <code>y</code>.
<a name="52586"></a>
<li>The local variable <code>c</code> has as its type the interface type <code>Colorable</code>, so it can hold a reference to any object whose class implements <code>Colorable</code>; specifically, it can hold a reference to a <code>ColoredPoint</code>.
<a name="24972"></a>
<li>Note that an expression such as "<code>new Colorable()</code>" is not valid because it is not possible to create an instance of an interface, only of a class.
</ul><a name="24976"></a>
Every array also has a class; the method <code>getClass</code> <a href="javalang.doc1.html#13783">(§20.1.1)</a>, when invoked for an array object, will return a class object (of class <code>Class</code>) that represents the class of the array. The classes for arrays have strange names that are not valid Java identifiers; for example, the class for an array of <code>int</code> components has the name "<code>[I</code>" and so the value of the expression:<p>
<pre><a name="24985"></a>new int[10].getClass().getName()
</pre><a name="85679"></a>
is the string <code>"[I"</code>; see <a href="javalang.doc1.html#13783">§20.1.1</a> for details.
<p>
<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="3.doc.html">Prev</a> | <a href="5.doc.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright © 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -