📄 concepts.doc.html
字号:
<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><a name="23418"></a><li>Assignment conversion <a href="Concepts.doc.html#19674">(§2.6.6)</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.<a name="15957"></a><li>Method invocation conversion <a href="Concepts.doc.html#19685">(§2.6.7)</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.<a name="15964"></a><li>Casting conversion <a href="Concepts.doc.html#18168">(§2.6.8)</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.<a name="15971"></a><li>String conversion, which allows any type to be converted to type <code>String</code> <a href="Concepts.doc.html#25486">(§2.4.7)</a>.<a name="15975"></a><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><i>String conversion</i> only applies to operands of the binary <code>+</code> operator 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><a name="19694"></a><li><code>byte</code> to <code>short</code>, <code>int</code>, <code>long</code>, <code>float</code>, or <code>double</code><a name="19722"></a><li><code>short</code> to <code>int</code>, <code>long</code>, <code>float</code>, or <code>double</code><a name="19723"></a><li><code>char</code> to <code>int</code>, <code>long</code>, <code>float</code>, or <code>double</code><a name="19721"></a><li><code>int</code> to <code>long</code>, <code>float</code>, or <code>double</code><a name="19695"></a><li><code>long</code> to <code>float</code> or <code>double</code><a name="19696"></a><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 and from <code>float</code> to <code>double</code> do not lose any information at all; the numeric value is preserved exactly. 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#17889">(§2.4.3)</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">(§2.15)</a>.<p><a name="19747"></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><a name="19767"></a><li><code>byte</code> to <code>char</code><a name="19768"></a><li><code>short</code> to <code>byte</code> or <code>char</code><a name="19753"></a><li><code>char</code> to <code>byte</code> or <code>short</code><a name="19751"></a><li><code>int</code> to <code>byte</code>, <code>short</code>, or <code>char</code><a name="19704"></a><li><code>long</code> to <code>byte</code>, <code>short</code>, <code>char</code>, or <code>int</code><a name="19705"></a><li><code>float</code> to <code>byte</code>, <code>short</code>, <code>char</code>, <code>int</code>, or <code>long</code><a name="19706"></a><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 than 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 <code></code>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#17889">(§2.4.3)</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#17889">(§2.4.3)</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 therefore never throw an exception at run time. Because they do not affect the Java Virtual Machine, they will not be considered further.<p><a name="25683"></a><h3>2.6.5 Narrowing Reference Conversions</h3><a name="25686"></a>The following permitted conversions are called the <i>narrowing reference conversions</i>:<p><ul><a name="25687"></a><li>From any class type S<i></i> to any class type T<i></i>, provided that S<i></i> is a superclass of T<i></i>. (An important special case is that there is a narrowing conversion from the class type <code>Object</code> to any other class type.)<a name="25688"></a><li>From any class type S<i></i> to any interface type K<i></i>, provided that S<i></i> is not <code>final</code> and does not implement K<i></i>. (An important special case is that there is a narrowing conversion from the class type <code>Object</code> to any interface type.)<a name="25689"></a><li>From type <code>Object</code> to any array type.<a name="27292"></a><li>From type <code>Object</code> to any interface type.<a name="25690"></a><li>From any interface type J<i></i> to any class type T<i></i> that is not <code>final</code>.<a name="25691"></a><li>From any interface type J<i></i> to any class type T<i></i> that is <code>final</code>, provided that T<i></i> implements J<i></i>.<a name="25692"></a><li>From any interface type J<i></i> to any interface type K<i></i>, provided that J<i></i> is not a subinterface of K<i></i> and there is no method name m<i></i> such that J<i></i> and K<i></i> both declare a method named m<i></i> with the same signature but different return types.<a name="25693"></a><li>From any array type SC<code>[]</code> to any array type TC<code>[]</code>, provided that SC<i></i> and TC<i></i> are reference types and there is a permitted narrowing conversion from SC<i></i> to TC<i></i>.</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="19674"></a><h3>2.6.6 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">(§2.6.1)</a>, a widening primitive conversion <a href="Concepts.doc.html#23435">(§2.6.2)</a>, or a widening reference conversion <a href="Concepts.doc.html#25679">(§2.6.4)</a>. In addition, a narrowing primitive conversion <a href="Concepts.doc.html#26142">(§2.6.3)</a> may be used if all of the following conditions are satisfied:<p><ul><a name="25751"></a><li>The expression is a constant expression of type <code>int</code>.<a name="25752"></a><li>The type of the variable is <code>byte</code>, <code>short</code>, or <code>char</code>. <a name="25776"></a><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="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 to type <code>boolean</code>. A value of the null type may be assigned to 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><a name="19906"></a><li>If S is a class type:<a name="19910"></a><li>If T is a class type, then S must be the same class as T, or S must be a subclass of T.<a name="19917"></a><li>If T is an interface type, then S must implement interface T.<a name="19922"></a><li>If S is an interface type:<a name="19929"></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -