📄 concepts.doc.html
字号:
<a name="25376"></a><h3>2.4.5 Reference Types, Objects, and Reference Values</h3><a name="25380"></a>There are three kinds of reference types: the <i>class types</i> <a href="Concepts.doc.html#16198">(§2.8)</a>, the <i>interface types</i> <a href="Concepts.doc.html#16432">(§2.13)</a>, and the <i>array types</i> <a href="Concepts.doc.html#16446">(§2.14)</a>. An <i>object</i> is a dynamically created class instance or an array. The reference values (often just <i>references</i>) are <i>pointers </i>to these objects and a special null reference, which refers to no object.<p><a name="25388"></a>A class instance is explicitly created by a <i>class instance creation expression</i>, or by invoking the <code>newInstance</code> method of class <code>Class</code>. An array is explicitly created by an <i>array creation expression</i>. An object is created in the Java heap, and is garbage collected after there are no more references to it. Objects are never reclaimed or freed by explicit Java language directives.<p><a name="25389"></a>There may be many references to the same object. Most objects have state, stored in the fields of objects that are instances of classes or in the variables that are the components of an array object. If two variables contain references to the same object, the state of the object can be modified using one variable's reference to the object, and then the altered state can be observed through the other variable's reference.<p><a name="25393"></a>Each object has an associated <i>lock</i> (<a href="Concepts.doc.html#24465">§2.17</a>, <a href="Threads.doc.html#22500">§8.13)</a> that is used by <code>synchronized</code> methods and by the <code>synchronized</code> statement to provide control over concurrent access to state by multiple threads (<a href="Concepts.doc.html#24465">§2.17</a>, <a href="Threads.doc.html#22488">§8.12)</a>.<p><a name="27419"></a>Reference types form a hierarchy. Each class type is a subclass of another class type, except for the class <code>Object</code> <a href="Concepts.doc.html#27433">(§2.4.6)</a>, which is the superclass <a href="Concepts.doc.html#20358">(§2.8.3)</a> of all other class types. All objects, including arrays, support the methods of class <code>Object</code>. String literals <a href="Concepts.doc.html#23152">(§2.3)</a> are references to instances of class <code>String</code> <a href="Concepts.doc.html#25486">(§2.4.7)</a>.<p><a name="27433"></a><h3>2.4.6 The Class <code>Object</code></h3><a name="25481"></a>The standard class <code>Object</code> is the superclass <a href="Concepts.doc.html#20358">(§2.8.3)</a> of all other classes. A variable of type <code>Object</code> can hold a reference to any object, whether it is an instance of a class or an array. All class and array types inherit the methods of class <code>Object</code>.<p><a name="25486"></a><h3>2.4.7 The Class <code>String</code> </h3><a name="25487"></a>Instances of class <code>String</code> represent sequences of Unicode characters <a href="Concepts.doc.html#25310">(§2.1)</a>. A <code>String</code> object has a constant, unchanging value. String literals <a href="Concepts.doc.html#23152">(§2.3)</a> are references to instances of class <code>String</code>.<p><a name="17255"></a><h3>2.4.8 Operators on Objects</h3><a name="23253"></a>The operators on objects include field access, method invocation, cast, string concatenation, comparison for equality, <code>instanceof</code>, and the conditional operator <code>?:</code>.<p><a name="17203"></a><hr><h2>2.5 Variables</h2><a name="17290"></a>A <i>variable</i> is a storage location. It has an associated type, sometimes called its <i>compile-time type</i>, that is either a primitive type <a href="Concepts.doc.html#19511">(§2.4.1)</a> or a reference type <a href="Concepts.doc.html#25376">(§2.4.5)</a>. A variable always contains a value that is assignment compatible <a href="Concepts.doc.html#19674">(§2.6.6)</a> with its type. A variable of a primitive type always holds a value of that exact primitive type. A variable of reference type can hold either a null reference or a reference to any object whose class is assignment compatible <a href="Concepts.doc.html#19674">(§2.6.6)</a> with the type of the variable. <p><a name="25533"></a>Compatibility of the value of a variable with its type is guaranteed by the design of the Java language because default values <a href="Concepts.doc.html#15858">(§2.5.1)</a> are compatible and all assignments to a variable are checked, at compile time, for assignment compatibility.<p><a name="19554"></a>There are seven kinds of variables:<p><ol><a name="17990"></a><li>A <i>class variable </i>is a field of a class type declared using the keyword <code>static</code> <a href="Concepts.doc.html#16297">(§2.9.1)</a> within a class declaration, or with or without the keyword <code>static</code> in an interface declaration. Class variables are created when the class or interface is loaded <a href="Concepts.doc.html#19175">(§2.16.2)</a> and are initialized on creation to default values <a href="Concepts.doc.html#15858">(§2.5.1)</a>. The class variable effectively ceases to exist when its class or interface is unloaded <a href="Concepts.doc.html#24377">(§2.16.8)</a> after any necessary finalization of the class <a href="Concepts.doc.html#24377">(§2.16.8)</a> has been completed.<a name="17982"></a><li>An <i>instance variable</i> is a field declared within a class declaration without using the keyword <code>static</code> <a href="Concepts.doc.html#16297">(§2.9.1)</a>. If a class T has a field a that is an instance variable, then a new instance variable a is created and initialized to a default value <a href="Concepts.doc.html#15858">(§2.5.1)</a> as part of each newly created object of class T or of any class that is a subclass of T. The instance variable effectively ceases to exist when the object of which it is a field is no longer referenced, after any necessary finalization of the object <a href="Concepts.doc.html#19147">(§2.16.7)</a> has been completed.<a name="17995"></a><li><i>Array components</i> are unnamed variables that are created and initialized to default values <a href="Concepts.doc.html#15858">(§2.5.1)</a> whenever a new object that is an array is created <a href="Concepts.doc.html#19124">(§2.16.6)</a>. The array components effectively cease to exist when the array is no longer referenced.<a name="17996"></a><li><i>Method parameters</i> name argument values passed to a method. For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked. The new variable is initialized with the corresponding argument value from the method invocation. The method parameter effectively ceases to exist when the execution of the body of the method is complete.<a name="23360"></a><li><i>Constructor parameters</i> name argument values passed to a constructor. For every parameter declared in a constructor declaration, a new parameter variable is created each time a class instance creation expression or explicit constructor invocation is evaluated. The new variable is initialized with the corresponding argument value from the creation expression or constructor invocation. The constructor parameter effectively ceases to exist when the execution of the body of the constructor is complete.<a name="17997"></a><li>An <i>exception-handler parameter</i> variable is created each time an exception is caught by a <code>catch</code> clause of a <code>try</code> statement <a href="Concepts.doc.html#22746">(§2.15.2)</a>. The new variable is initialized with the actual object associated with the exception <a href="Concepts.doc.html#24863">(§2.15.3)</a>. The exception-handler parameter effectively ceases to exist when execution of the block associated with the <code>catch</code> clause <a href="Concepts.doc.html#22746">(§2.15.2)</a> is complete.<a name="18000"></a><li><i>Local variables </i>are declared by local variable declaration statements. Whenever the flow of control enters a block or a <code>for</code> statement, 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. The local variable is not initialized, however, until the local variable declaration statement that declares it is executed. The local variable effectively ceases to exist when the execution of the block or <code>for</code> statement is complete.</ol><a name="15858"></a><h3>2.5.1 Initial Values of Variables</h3><a name="15862"></a>Every variable in a Java program must have a value before it is used:<p><ul><a name="15863"></a><li>Each class variable, instance variable, and array component is initialized with a <i>default value</i> when it is created:<a name="15874"></a><li>For type <code>byte</code>, the default value is zero, that is, the value of <code>(byte)0</code>.<a name="15875"></a><li>For type <code>short</code>, the default value is zero, that is, the value of <code>(short)0</code>.<a name="15876"></a><li>For type <code>int</code>, the default value is zero, that is, <code>0</code>.<a name="15877"></a><li>For type <code>long</code>, the default value is zero, that is, <code>0L</code>.<a name="15878"></a><li>For type <code>float</code>, the default value is positive zero, that is, <code>0.0f</code>.<a name="15879"></a><li>For type <code>double</code>, the default value is positive zero, that is, <code>0.0d</code>.<a name="15880"></a><li>For type <code>char</code>, the default value is the null character, that is, <code>'u0000'</code>.<a name="15881"></a><li>For type <code>boolean</code>, the default value is <code>false</code>.<a name="15888"></a><li>For all reference types <a href="Concepts.doc.html#25376">(§2.4.5)</a>, the default value is <code>null</code> <a href="Concepts.doc.html#23152">(§2.3)</a>.<a name="15892"></a><li>Each method parameter <a href="Concepts.doc.html#17203">(§2.5)</a> is initialized to the corresponding argument value provided by the invoker of the method.<a name="15899"></a><li>Each constructor parameter <a href="Concepts.doc.html#17203">(§2.5)</a> is initialized to the corresponding argument value provided by an object creation expression or explicit constructor invocation.<a name="15906"></a><li>An exception-handler parameter <a href="Concepts.doc.html#22746">(§2.15.2)</a> is initialized to the thrown object representing the exception <a href="Concepts.doc.html#24863">(§2.15.3)</a>. <a name="15919"></a><li>A local variable must be explicitly given a value before it is used, by either initialization or assignment.</ul><a name="23409"></a><h3>2.5.2 Variables Have Types, Objects Have Classes</h3><a name="25606"></a>Every object belongs to some particular class. This is the class that was mentioned in the class instance creation expression that produced the object, or the class whose class object was used to invoke the <code>newInstance</code> method to produce the object. This class is called <em>the</em> class of the object. An object is said to be an <i>instance</i> of its class and of all superclasses of its class. Sometimes the class of an object is called its "runtime type," but "class" is the more accurate term.<p><a name="15939"></a>(Sometimes a variable or expression is said to have a "runtime 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="25614"></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 runtime 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="25612"></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 href="Concepts.doc.html#16432">(§2.13)</a>. A variable or expression whose type is an interface type can reference any object whose class implements that interface.<p><a name="25629"></a>Every array also has a class. 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>.<p><a name="25611"></a><hr><h2>2.6 Conversions and Promotions</h2><a name="15943"></a><i>Conversions</i> implicitly change the type, and sometimes the value, of an expression to a type acceptable for its surrounding context. In some cases this will require a corresponding action at run time to check the validity of the conversion or to translate the runtime value of the expression into a form appropriate for the new type. <p><a name="25645"></a><i>Numeric promotions</i> are conversions that change an operand of a numeric operation to a wider type, or both operands of a numeric operation to a common type, so that an operation can be performed.<p><a name="18104"></a>In Java, there are six broad kinds of conversions:<p><ul><a name="15950"></a><li>Identity conversions<a name="23419"></a><li>Widening primitive conversions<a name="23420"></a><li>Narrowing primitive conversions<a name="23421"></a><li>Widening reference conversions<a name="23422"></a><li>Narrowing reference conversions<a name="23423"></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -