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

📄 5.doc.html

📁 java语言规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="25266"></a>
A narrowing conversion of a character to an integral type <i>T</i> likewise simply discards all but the <i>n </i>lowest order bits, where <i>n </i>is the number of bits used to represent type <i>T</i>. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the resulting value to be a negative number, even though characters represent 16-bit unsigned integer values.<p>
<a name="25267"></a>
A narrowing conversion of a floating-point number to an integral type <i>T</i><em></em> takes two steps:<p>
<ol>
<a name="25268"></a>
<li>In the first step, the floating-point number is converted either to a <code>long</code>, if <i>T</i> is <code>long</code>, or to an <code>int</code>, if <i>T</i><em></em> is <code>byte</code>, <code>short</code>, <code>char</code>, or <code>int</code>, as follows:
<ul>
<a name="25272"></a>
<li>If the floating-point number is NaN <a href="4.doc.html#9208">(&#167;4.2.3)</a>, the result of the first step of the conversion is an <code>int</code> or <code>long</code> <code>0</code>.
<a name="174285"></a>
<li>Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value <i>V</i>, rounding toward zero using IEEE 754 round-toward-zero mode <a href="4.doc.html#9208">(&#167;4.2.3)</a>. Then there are two cases:
<ul>
<a name="25277"></a>
<li>If <i>T</i><em></em> is <code>long</code>, and this integer value can be represented as a <code>long</code>, then the result of the first step is the <code>long</code> value <i>V</i>.
<a name="25278"></a>
<li>Otherwise, if this integer value can be represented as an <code>int</code>, then the result of the first step is the <code>int</code> value <i>V</i>.
</ul>
<a name="25279"></a>
<li>Otherwise, one of the following two cases must be true:
<ul>
<a name="25280"></a>
<li>The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type <code>int</code> or <code>long</code>.
<a name="25281"></a>
<li>The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type <code>int</code> or <code>long</code>.
</ul>
</ul>
<a name="25282"></a>
<li>In the second step:
<ul>
<a name="25283"></a>
<li>If <i>T</i> is <code>int</code> or <code>long</code>, the result of the conversion is the result of the first step.
<a name="25284"></a>
<li>If <i>T</i> is <code>byte</code>, <code>char</code>, or <code>short</code>, the result of the conversion is the result of a narrowing conversion to type <i>T</i> <a href="5.doc.html#175672">(&#167;5.1.3)</a> of the result of the first step.
</ul>
</ol>
<a name="176319"></a>
The example:
<p><pre><a name="176320"></a>
class Test {
<a name="25290"></a>	public static void main(String[] args) {
<a name="25291"></a>		float fmin = Float.NEGATIVE_INFINITY;
<a name="25292"></a>		float fmax = Float.POSITIVE_INFINITY;
<a name="25293"></a>		System.out.println("long: " + (long)fmin +
<a name="25294"></a>								".." + (long)fmax);
<a name="25295"></a>		System.out.println("int: " + (int)fmin +
<a name="25296"></a>								".." + (int)fmax);
<a name="25297"></a>		System.out.println("short: " + (short)fmin +
<a name="25298"></a>								".." + (short)fmax);
<a name="25299"></a>		System.out.println("char: " + (int)(char)fmin +
<a name="25300"></a>								".." + (int)(char)fmax);
<a name="25301"></a>		System.out.println("byte: " + (byte)fmin +
<a name="25302"></a>								".." + (byte)fmax);
<a name="25303"></a>	}
<a name="25304"></a>}
</pre><a name="25305"></a>
produces the output:
<p><pre><a name="25306"></a>
long: -9223372036854775808..9223372036854775807
<a name="25307"></a>int: -2147483648..2147483647
<a name="25308"></a>short: 0..-1
<a name="25309"></a>char: 0..65535
<a name="25310"></a>byte: 0..-1
</pre><a name="25311"></a>
The results for <code>char</code>, <code>int</code>, and <code>long</code> are unsurprising, producing the minimum and maximum representable values of the type.<p>
<a name="25312"></a>
The results for <code>byte</code> and <code>short</code> lose information about the sign and magnitude of the numeric values and also lose precision. The results can be understood by examining the low order bits of the minimum and maximum <code>int.</code> The minimum <code>int</code> is, in hexadecimal, <code>0x80000000</code>, and the maximum <code>int</code> is <code>0x7fffffff</code>. This explains the <code>short</code> results, which are the low 16 bits of these values, namely, <code>0x0000</code> and <code>0xffff</code>; it explains the <code>char</code> results, which also are the low 16 bits of these values, namely, <code>'\u0000'</code> and <code>'\uffff'</code>; and it explains the <code>byte</code> results, which are the low 8 bits of these values, namely, <code>0x00</code> and <code>0xff</code>.<p>
<a name="25315"></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 value too small to be represented as a <code>float</code> is converted to 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> NaN is always converted to a <code>float</code> NaN.<p>
<a name="25316"></a>
Despite the fact that overflow, underflow, or other loss of information may occur, narrowing conversions among primitive types never result in a run-time exception <a href="11.doc.html#44043">(&#167;11)</a>.<p>
<a name="25321"></a>
Here is a small test program that demonstrates a number of narrowing conversions that lose information:<p>
<pre><a name="25322"></a>
class Test {
<a name="25323"></a>
	public static void main(String[] args) {
<a name="25324"></a>
		// A narrowing of int to short loses high bits:
<a name="25325"></a>		System.out.println("(short)0x12345678==0x" +
<a name="25326"></a>					Integer.toHexString((short)0x12345678));
<br><a name="25328"></a>
		// A int value not fitting in byte changes sign and magnitude:
<a name="25329"></a>		System.out.println("(byte)255==" + (byte)255);
<br><a name="25331"></a>
		// A float value too big to fit gives largest int value:
<a name="25332"></a>		System.out.println("(int)1e20f==" + (int)1e20f);
<br><a name="25334"></a>
		// A NaN converted to int yields zero:
<a name="25335"></a>		System.out.println("(int)NaN==" + (int)Float.NaN);
<br><a name="25337"></a>
		// A double value too large for float yields infinity:
<a name="25338"></a>		System.out.println("(float)-1e100==" + (float)-1e100);
<br><a name="25340"></a>
		// A double value too small for float underflows to zero:
<a name="25341"></a>		System.out.println("(float)1e-50==" + (float)1e-50);
<br><a name="25342"></a>	}
<br><a name="25346"></a>}
</pre><a name="25347"></a>
This test program produces the following output:
<p><pre><a name="25348"></a>
(short)0x12345678==0x5678
<a name="25349"></a>(byte)255==-1
<a name="25350"></a>(int)1e20f==2147483647
<a name="25351"></a>(int)NaN==0
<a name="25352"></a>(float)-1e100==-Infinity
<a name="25353"></a>(float)1e-50==0.0
</pre><a name="25215"></a>
<h3>5.1.4    Widening Reference Conversions</h3>
<a name="25460"></a>
The following conversions are called the <i>widening reference conversions</i>:
<p><ul><a name="25482"></a>
<li>From any class type <i>S</i> to any class type <i>T</i>, provided that <i>S</i> is a subclass of <i>T</i>. (An important special case is that there is a widening conversion to the class type <code>Object</code> from any other class type.)
<a name="25484"></a>
<li>From any class type <i>S</i> to any interface type <i>K</i>, provided that <i>S</i> implements <i>K</i>.
<a name="25483"></a>
<li>From the null type to any class type, interface type, or array type.
<a name="25502"></a>
<li>From any interface type <i>J</i> to any interface type <i>K</i>, provided that <i>J</i> is a subinterface of <i>K</i>.
<a name="25500"></a>
<li>From any interface type to type <code>Object</code>.
<a name="174846"></a>
<li>From any array type to type <code>Object</code>.
<a name="174848"></a>
<li>From any array type to type <code>Cloneable</code>.
<a name="25525"></a>
<li>From any array type <i>SC</i><code>[]</code> to any array type <i>TC</i><code>[]</code>, provided that <i>SC</i> and <i>TC</i> are reference types and there is a widening conversion from <i>SC</i> to <i>TC</i>.
</ul><a name="25616"></a>
Such conversions never require a special action at run time and therefore never 
throw an exception at run time. They consist simply in regarding a reference as 
having some other type in a manner that can be proved correct at compile time.
<p><a name="25457"></a>
See <a href="8.doc.html#3857">&#167;8</a> for the detailed specifications for classes, <a href="9.doc.html#238678">&#167;9</a> for interfaces, and <a href="10.doc.html#27803">&#167;10</a> for arrays.<p>
<a name="25379"></a>
<h3>5.1.5    Narrowing Reference Conversions</h3>
<a name="175454"></a>
The following conversions are called the <i>narrowing reference conversions</i>:
<p><ul><a name="175455"></a>
<li>From any class type <i>S</i> to any class type <i>T</i>, provided that <i>S</i> is a superclass of <i>T</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="25705"></a>
<li>From any class type <i>S</i> to any interface type <i>K</i>, provided that <i>S</i> is not final and does not implement <i>K.</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="25766"></a>
<li>From type <code>Object</code> to any array type.
<a name="175692"></a>
<li>From type <code>Object</code> to any interface type.
<a name="25783"></a>
<li>From any interface type <i>J</i> to any class type <i>T</i> that is not <code>final</code>.
<a name="25787"></a>
<li>From any interface type <i>J</i> to any class type <i>T</i> that is <code>final</code>, provided that <i>T</i> implements <i>J</i>.
<a name="25774"></a>
<li>From any interface type <i>J</i> to any interface type <i>K</i>, provided that <i>J</i> is not a subinterface of <i>K</i> and there is no method name <i>m</i> such that <i>J</i> and <i>K</i> both declare a method named <i>m</i> with the same signature but different return types.
<a name="25710"></a>
<li>From any array type <i>SC</i><code>[]</code> to any array type <i>TC</i><code>[]</code>, provided that <i>SC</i> and <i>TC</i> are reference types and there is a narrowing conversion from <i>SC</i> to <i>TC</i>.
</ul><a name="176885"></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 not, then a <code>ClassCastException</code> is 
thrown.
<p><a name="176886"></a>
<h3>5.1.6    String Conversions</h3>
<a name="175033"></a>
There is a string conversion to type <code>String</code> from every other type, including the 
null type.
<p><a name="175034"></a>
<h3>5.1.7    Forbidden Conversions</h3>
<ul><a name="175035"></a>
<li>There is no permitted conversion from any reference type to any primitive type.
<a name="25844"></a>
<li>Except for the string conversions, there is no permitted conversion from any primitive type to any reference type.
<a name="25860"></a>
<li>There is no permitted conversion from the null type to any primitive type.
<a name="25835"></a>
<li>There is no permitted conversion to the null type other than the identity conversion.
<a name="25993"></a>
<li>There is no permitted conversion to the type <code>boolean</code> other than the identity conversion.
<a name="25997"></a>
<li>There is no permitted conversion from the type <code>boolean</code> other than the identity conversion and string conversion.
<a name="25357"></a>
<li>There is no permitted conversion other than string conversion from class type <i>S</i> to a different class type <i>T</i> if <i>S</i> is not a subclass of <i>T</i> and <i>T</i> is not a subclass of <i>S</i>.
<a name="25885"></a>
<li>There is no permitted conversion from class type <i>S</i> to interface type <i>K</i> if <i>S</i> is <code>final</code> and does not implement <i>K</i>.
<a name="25899"></a>
<li>There is no permitted conversion from class type <i>S</i> to any array type if <i>S</i> is not <code>Object</code>.
<a name="25902"></a>
<li>There is no permitted conversion other than string conversion from interface type <i>J</i> to class type <i>T</i> if <i>T</i> is <code>final</code> and does not implement <i>J</i>.
<a name="25924"></a>
<li>There is no permitted conversion from interface type <i>J</i> to interface type <i>K</i> if <i>J</i> and <i>K</i> declare methods with the same signature but different return types.
<a name="25954"></a>
<li>There is no permitted conversion from any array type to any class type other than <code>Object</code> or <code>String</code>.
<a name="25958"></a>
<li>There is no permitted conversion from any array type to any interface type, except to the interface type <code>Cloneable</code>, which is implemented by all arrays.
<a name="25966"></a>
<li>There is no permitted conversion from array type <i>SC</i><code>[]</code> to array type <i>TC</i><code>[]</code> if there is no permitted conversion other than a string conversion from <i>SC</i> to <i>TC.</i>
</ul><a name="170768"></a>

⌨️ 快捷键说明

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