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

📄 concepts.doc.html

📁 A Java virtual machine instruction consists of an opcode specifying the operation to be performed, f
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<li><code>int</code> to <code>byte</code>, <code>short</code>, or <code>char</code><p><li><code>long</code> to <code>byte</code>, <code>short</code>, <code>char</code>, or <code>int</code><p><li><code>float</code> to <code>byte</code>, <code>short</code>, <code>char</code>, <code>int</code>, or <code>long</code><p><li><code>double</code> to <code>byte</code>, <code>short</code>, <code>char</code>, <code>int</code>, <code>long</code>, or <code>float</code></ul><a name="19707"></a>Narrowing conversions may lose information about the sign or order of magnitude, or both, of a numeric value (for example, narrowing an <code>int</code> value <code>32763</code> to type <code>byte</code> produces the value <code>-5</code>). Narrowing conversions may also lose precision.<p><a name="19708"></a>A narrowing conversion of a signed integer to an integral type simply discards all but the <i>n</i> lowest-order bits, where <i>n</i> is the number of bits used to represent the type. This may cause the resulting value to have a different sign from the input value.<p><a name="19798"></a>A narrowing conversion of a character to an integral type likewise simply discards all but the <i>n</i> lowest bits, where <i>n</i> is the number of bits used to represent the type. This may cause the resulting value to be a negative number, even though characters represent 16-bit unsigned integer values.<p><a name="19709"></a>In a narrowing conversion of a floating-point number to an integral type, if the floating-point number is NaN, the result of the conversion is <code>0</code> of the appropriate type. If the floating-point number is too large to be represented by the integral type or is positive infinity, the result is the largest representable value of the integral type. If the floating-point number is too small to be represented or is negative infinity, the result is the smallest representable value of the integral type. Otherwise, the result is the floating-point number rounded towards zero to an integer value using IEEE 754 round towards zero mode <a href="Concepts.doc.html#33499">(&#167;2.4.4)</a><p><a name="19713"></a>A narrowing conversion from <code>double</code> to <code>float</code> behaves in accordance with IEEE 754. The result is correctly rounded using IEEE 754 round to nearest mode <a href="Concepts.doc.html#33499">(&#167;2.4.4)</a>. A value too small to be represented as a <code>float</code> is converted to a positive or negative zero; a value too large to be represented as a <code>float</code> is converted to a positive or negative infinity. A <code>double</code> <code></code>NaN is always converted to a <code>float</code> <code></code>NaN.<p><a name="19715"></a>Despite the fact that overflow, underflow, or loss of precision may occur, narrowing conversions among primitive types never result in a runtime exception.<p><a name="25679"></a><h3>2.6.4    Widening Reference Conversions</h3><a name="25680"></a><em>Widening reference conversions</em> never require a special action at run time and thereforenever throw an exception at run time. Because they do not affect the Java virtual machine, they will not be considered further.<p><a name="32879"></a><h3>2.6.5    Narrowing Reference Conversions</h3><a name="32881"></a>The following permitted conversions are called the <i>narrowing reference conversions</i>:<p><ul><li>From any class type S to any class type T, provided that S is a superclass of T. (An important special case is that there is a narrowing conversion from the class type <code>Object</code> to any other class type.)<p><li>From any class type S to any interface type K, provided that S is not <code>final</code> and does not implement K. (An important special case is that there is a narrowing conversion from the class type <code>Object</code> to any interface type.)<p><li>From type <code>Object</code> to any array type.<p><li>From type <code>Object</code> to any interface type.<p><li>From any interface type J to any class type T that is not <code>final</code>.<p><li>From any interface type J to any class type T that is <code>final</code>, provided that T implements J.<p><li>From any interface type J to any interface type K, provided that J is not a subinterface of K and there is no method name m such that J and K both declare a method named m with the same signature but different return types.<p><li>From any array type SC<code>[]</code> to any array type TC<code>[]</code>, provided that SC and TC are reference types and there is a permitted narrowing conversion from SC to TC.</ul><a name="25694"></a>Such conversions require a test at run time to find out whether the actual reference value is a legitimate value of the new type. If it is not, the Java virtual machine throws a <code>ClassCastException</code>.<p><a name="32922"></a><h3>2.6.6    Value Set Conversion</h3><a name="34313"></a><i>Value set conversion</i> is the process of mapping a floating-point value from one value set <a href="Concepts.doc.html#33377">(&#167;2.4.3)</a> to another without changing its type.<p><a name="34314"></a>For each operation in an expression that is not FP-strict <a href="Concepts.doc.html#24465">(&#167;2.18)</a>, value set conversion allows an implementation of the Java programming language to choose between two options:<p><ul><li>If the value is an element of the float-extended-exponent value set, then the implementation may map the value to the nearest element of the float value set. This conversion may result in overflow (in which case the value is replaced by an infinity of the same sign) or underflow (in which case the value may lose precision because it is replaced by a denormalized number or zero of the same sign).<p><li>If the value is an element of the double-extended-exponent value set, then the implementation may map the value to the nearest element of the double value set. This conversion may result in overflow (in which case the value is replaced by an infinity of the same sign) or underflow (in which case the value may lose precision because it is replaced by a denormalized number or zero of the same sign).</ul><a name="34317"></a>Within an FP-strict expression, value set conversion does not provide any choices; every implementation must behave in the same way:<p><ul><li>If the value is of type float and is not an element of the float value set, then the implementation must map the value to the nearest element of the float value set. This conversion may result in overflow or underflow.<p><li>If the value is of type double and is not an element of the double value set, then the implementation must map the value to the nearest element of the double value set. This conversion may result in overflow or underflow.</ul><a name="34320"></a>Within an FP-strict expression, mapping values from the float-extended-exponent value set or double-extended-exponent value set is necessary only when a method is called whose declaration is not FP-strict and the implementation has chosen to represent the result of the method call as an element of an extended-exponent value set.<p><a name="34321"></a>Whether in FP-strict code or code that is not FP-strict, value set conversion always leaves unchanged any value whose type is neither float nor double.<p><a name="19674"></a><h3>2.6.7    Assignment Conversion</h3><a name="25744"></a><i>Assignment conversion</i> occurs when the value of an expression is assigned to a variable:the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of an identity conversion <a href="Concepts.doc.html#19691">(&#167;2.6.1)</a>, a widening primitive conversion <a href="Concepts.doc.html#23435">(&#167;2.6.2)</a>, or a widening reference conversion <a href="Concepts.doc.html#25679">(&#167;2.6.4)</a>. In addition,a narrowing primitive conversion <a href="Concepts.doc.html#26142">(&#167;2.6.3)</a> may be used if all of the following conditions are satisfied:<p><ul><li>The expression is a constant expression of type <code>int</code>.<p><li>The type of the variable is <code>byte</code>, <code>short</code>, or <code>char</code>. <p><li>The value of the expression is representable in the type of the variable.</ul><a name="19829"></a>If the type of the expression can be converted to the type of a variable by assignment conversion, we say the expression (or its value) is <i>assignable</i> to the variable or, equivalently, that the type of the expression is <i>assignment compatible</i> with the type of the variable.<p><a name="32947"></a>If the type of the variable is <code>float</code> or <code>double</code>, then value set conversion <a href="Concepts.doc.html#32922">(&#167;2.6.6)</a> is applied after the type conversion:<p><ul><li>If the value is of type <code>float</code> and is an element of the float-extended-exponent value set, then the implementation must map the value to the nearest element of the float value set. This conversion may result in overflow or underflow.<p><li>If the value is of type <code>double</code> and is an element of the double-extended-exponent value set, then the implementation must map the value to the nearest element of the double value set. This conversion may result in overflow or underflow.</ul><a name="19677"></a>An assignment conversion never causes an exception. A value of primitive type must not be assigned to a variable of reference type. A value of reference type must not be assigned to a variable of primitive type. A value of type <code>boolean</code> can be assigned only to a variable of type <code>boolean</code>. A value of the null type may be assigned to a variable of any reference type.<p><a name="21076"></a>Assignment of a value of compile-time reference type S (source) to a variable of compile-time reference type T (target) is permitted:<p><ul><li>If S is a class type:<p><ul><li>If T is a class type, then S must be the same class as T, or S must be  a subclass of T.<p><li>If T is an interface type, then S must implement interface T.<p></ul><li>If S is an interface type:<p><ul><li>If T is a class type, then T must be <code>Object</code>.<p><li>If T is an interface type, then T must be the same interface as S, or T  must be a superinterface of S.<p></ul><li>If S is an array type SC<code>[]</code>, that is, an array of components of type SC:<p><ul><li>If T is a class type, then T must be <code>Object</code>.<p><li>If T is an interface type, then T must be either <code>Cloneable</code> or <code>java.io.Serializable</code>.<p><li>If T is an array type TC<code>[]</code>, that is, an array of components of type TC,  then either<p><ul><li>TC and SC must be the same primitive type, or<p><li>TC and SC are both reference types and type SC is assignable to TC.</ul></ul></ul><a name="19685"></a><h3>2.6.8    Method Invocation Conversion</h3><a name="25833"></a><i>Method invocation conversion</i> is applied to each argument value in a method or constructorinvocation: the type of the argument expression must be converted to the type of the corresponding parameter. Method invocation contexts allow the use of an identity conversion <a href="Concepts.doc.html#19691">(&#167;2.6.1)</a>, a widening primitive conversion <a href="Concepts.doc.html#23435">(&#167;2.6.2)</a>, or a wideningreference conversion <a href="Concepts.doc.html#25679">(&#167;2.6.4)</a>. Method invocation conversions specifically do not include the implicit narrowing of integer constants that is part of assignment conversion <a href="Concepts.doc.html#19674">(&#167;2.6.7)</a>.<p><a name="34377"></a>If the type of an argument expression is either float or double, then value set conversion <a href="Concepts.doc.html#32922">(&#167;2.6.6)</a> is applied after the type conversion:<p><ul><li>If an argument value of type <code>float</code> is an element of the float-extended-exponent value set, then the implementation must map the value to the nearest element of the float value set. This conversion may result in overflow or underflow.<p><li>If an argument value of type <code>double</code> is an element of the double-extended-exponent value set, then the implementation must map the value to the nearest element of the double value set. This conversion may result in overflow or underflow.</ul><a name="18168"></a><h3>2.6.9    Casting Conversion</h3><a name="19492"></a><i>Casting conversions </i>are more powerful than assignment or method invocation conversionsapplied to the operand of a cast operator: the type of the operand expression must be converted to the type explicitly named by the cast operator. Casting contexts allow the use of an identity conversion <a href="Concepts.doc.html#19691">(&#167;2.6.1)</a>, a widening primitive conversion <a href="Concepts.doc.html#23435">(&#167;2.6.2)</a>, a narrowing primitive conversion <a href="Concepts.doc.html#26142">(&#167;2.6.3)</a>, a widening reference conversion<a href="Concepts.doc.html#25679">(&#167;2.6.4)</a>, or a narrowing reference conversion <a href="Concepts.doc.html#32879">(&#167;2.6.5)</a>. Thus, casting conversionsare more inclusive than assignment or method invocation conversions: a cast can do any permitted conversion other than a string conversion.<p><a name="34401"></a>Value set conversion <a href="Concepts.doc.html#32922">(&#167;2.6.6)</a> is applied after the type conversion.<p><a name="18837"></a>Casting can convert a value of any numeric type to any other numeric type. A value of type <code>boolean</code> cannot be cast to another type. A value of reference type cannot be cast to a value of primitive type. <p><a name="20937"></a>Some casts can be proven incorrect at compile time and result in a compile-time error. Otherwise, either the cast can be proven correct at compile time, or a runtime validity check is required. (See <i>The Java</i><sup><font size=-2>TM</font></sup><i> Language Specification</i> for details.) If the value at run time is a null reference, then the cast is allowed. If the check at run time fails, a <code>ClassCastException</code> is thrown.<p><a name="16021"></a><h3>2.6.10    Numeric Promotion</h3>

⌨️ 快捷键说明

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