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

📄 concepts.doc.html

📁 A Java virtual machine instruction consists of an opcode specifying the operation to be performed, f
💻 HTML
📖 第 1 页 / 共 5 页
字号:
to instances of class <code>String</code>.<p><a name="17255"></a><h3>2.4.9    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-timetype</i>, that is either a primitive type <a href="Concepts.doc.html#19511">(&#167;2.4.1)</a> or a reference type <a href="Concepts.doc.html#29375">(&#167;2.4.6)</a>. A variable always contains a value that is assignment compatible <a href="Concepts.doc.html#19674">(&#167;2.6.7)</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">(&#167;2.6.7)</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 language because default values <a href="Concepts.doc.html#15858">(&#167;2.5.1)</a> are compatible and all assignments to a variable are checked, at compile time, for assignment compatibility. 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#29882">(&#167;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">(&#167;2.17.2)</a> and are initialized on creation to default values <a href="Concepts.doc.html#15858">(&#167;2.5.1)</a>. The class variable effectively ceases to exist when its class or interface is unloaded <a href="Concepts.doc.html#32202">(&#167;2.17.8)</a>.<p><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#29882">(&#167;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">(&#167;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">(&#167;2.17.7)</a> has been completed.<p><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">(&#167;2.5.1)</a> whenever a new object that is an array is created <a href="Concepts.doc.html#19124">(&#167;2.17.6)</a>. The array components effectively cease to exist when the array is no longer referenced.<p><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.<p><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.<p><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">(&#167;2.16.2)</a>. The new variable is initialized with the actual object associated with the exception <a href="Concepts.doc.html#24863">(&#167;2.16.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">(&#167;2.16.2)</a> is complete.<p><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 program must have a value before it is used:<p><ul><li>Each class variable, instance variable, and array component is initialized with a <i>default value</i> when it is created:<p><li>For type <code>byte</code>, the default value is zero, that is, the value of <code>(byte)0</code>.<p><li>For type <code>short</code>, the default value is zero, that is, the value of <code>(short)0</code>.<p><li>For type <code>int</code>, the default value is zero, that is, <code>0</code>.<p><li>For type <code>long</code>, the default value is zero, that is, <code>0L</code>.<p><li>For type <code>float</code>, the default value is positive zero, that is, <code>0.0f</code>.<p><li>For type <code>double</code>, the default value is positive zero, that is, <code>0.0</code>.<p><li>For type <code>char</code>, the default value is the null character, that is, <code>'\u0000'</code>.<p><li>For type <code>boolean</code>, the default value is <code>false</code>.<p><li>For all reference types <a href="Concepts.doc.html#29375">(&#167;2.4.6)</a>, the default value is <code>null</code> <a href="Concepts.doc.html#20359">(&#167;2.3)</a>.<p><li>Each method parameter <a href="Concepts.doc.html#17203">(&#167;2.5)</a> is initialized to the corresponding argument value provided by the invoker of the method.<p><li>Each constructor parameter <a href="Concepts.doc.html#17203">(&#167;2.5)</a> is initialized to the corresponding argument value provided by an object creation expression or explicit constructor invocation.<p><li>An exception-handler parameter <a href="Concepts.doc.html#22746">(&#167;2.16.2)</a> is initialized to the thrown object representing the exception <a href="Concepts.doc.html#24863">(&#167;2.16.3)</a>. <p><li>A local variable must be explicitly given a value, by either initialization or assignment, before it is used.</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">(&#167;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 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>A <i>conversion</i> from type S to type T allows an expression of type S to be treated at compile time as if it were of type T instead. In some cases this will require a correspondingaction 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 T. <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 the Java programming language, there are six broad kinds of conversions:<p><ul><li>Identity conversions<p><li>Widening primitive conversions<p><li>Narrowing primitive conversions<p><li>Widening reference conversions<p><li>Narrowing reference conversions<p><li>String conversions</ul><a name="23424"></a>There are five <i>conversion contexts</i> in which conversion expressions can occur. Each context allows conversions in some of the above-named categories but not others. The conversion contexts are:<p><ul><li>Assignment conversion <a href="Concepts.doc.html#19674">(&#167;2.6.7)</a>, which converts the type of an expression  to the type of a specified variable. The conversions permitted for assignment are limited in such a way that assignment conversion never causes an exception.<p><li>Method invocation conversion <a href="Concepts.doc.html#19685">(&#167;2.6.8)</a>, which is applied to each argument  in a method or constructor invocation, and, except in one case, performs the same conversions that assignment conversion does. Method invocation conversion never causes an exception.<p><li>Casting conversion <a href="Concepts.doc.html#18168">(&#167;2.6.9)</a>, which converts the type of an expression to a type explicitly specified by a cast operator. It is more inclusive than assignment or method invocation conversion, allowing any specific conversion other than a string conversion, but certain casts to a reference type may cause an exception at run time.<p><li>String conversion, which allows any type to be converted to type <code>String</code> <a href="Concepts.doc.html#25486">(&#167;2.4.8)</a>.<p><li>Numeric promotion, which brings the operands of a numeric operator to a common type so that an operation can be performed.</ul><a name="15976"></a>String conversion only applies to operands of the binary <code>+</code> and <code>+=</code> operators when one of the arguments is a <code>String</code>; it will not be covered further. <p><a name="19691"></a><h3>2.6.1    Identity Conversions</h3><a name="23436"></a>A conversion from a type to that same type is permitted for any type.<p><a name="23435"></a><h3>2.6.2    Widening Primitive Conversions</h3><a name="19693"></a>The following conversions on primitive types are called the <i>widening primitive conversions</i>:<p><ul><li><code>byte</code> to <code>short</code>, <code>int</code>, <code>long</code>, <code>float</code>, or <code>double</code><p><li><code>short</code> to <code>int</code>, <code>long</code>, <code>float</code>, or <code>double</code><p><li><code>char</code> to <code>int</code>, <code>long</code>, <code>float</code>, or <code>double</code><p><li><code>int</code> to <code>long</code>, <code>float</code>, or <code>double</code><p><li><code>long</code> to <code>float</code> or <code>double</code><p><li><code>float</code> to <code>double</code></ul><a name="19697"></a>Widening conversions do not lose information about the sign or order of magnitude of a numeric value. Conversions widening from an integral type to another integral type do not lose any information at all; the numeric value is preserved exactly. Conversions widening from <code>float</code> to <code>double</code> in <code>strictfp</code> expressions <a href="Concepts.doc.html#24465">(&#167;2.18)</a> also preserve the numeric value exactly; however, such conversions that are not <code>strictfp</code> may lose information about the overall magnitude of the converted value.<p><a name="38745"></a>Conversion of an <code>int</code> or a <code>long</code> value to <code>float</code>, or of a <code>long</code> value to <code>double</code>, may lose precision, that is, the result may lose some of the least significant bits of the value; the resulting floating-point value is a correctly rounded version of the integer value, using IEEE 754 round to nearest mode <a href="Concepts.doc.html#33499">(&#167;2.4.4)</a>.<p><a name="19699"></a>According to this rule, a widening conversion of a signed integer value to an integral type simply sign-extends the two's-complement representation of the integer value to fill the wider format. A widening conversion of a value of type <code>char</code> to an integral type zero-extends the representation of the character value to fill the wider format.<p><a name="19700"></a>Despite the fact that loss of precision may occur, widening conversions among primitive types never result in a runtime exception <a href="Concepts.doc.html#22727">(&#167;2.16)</a>.<p><a name="26142"></a><h3>2.6.3    Narrowing Primitive Conversions</h3><a name="19766"></a>The following conversions on primitive types are called <i>narrowing primitive conversions</i>:<p><ul><li><code>byte</code> to <code>char</code><p><li><code>short</code> to <code>byte</code> or <code>char</code><p><li><code>char</code> to <code>byte</code> or <code>short</code><p>

⌨️ 快捷键说明

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