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

📄 4.doc.html

📁 java语言规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html>
<head>
<title>The Java Language Specification Types, Values, and Variables</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
 
<a href="index.html">Contents</a> | <a href="3.doc.html">Prev</a> | <a href="5.doc.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
 
<a name="95843"></a>
<p><strong>
CHAPTER 4 </strong></p>
<a name="95845"></a>
<h1>Types, Values, and Variables</h1>
<hr><p>
<a name="45200"></a>
Java is a <i>strongly typed</i> language, which means that every variable and every 
expression has a type that is known at compile time. Types limit the values that a 
variable <a href="4.doc.html#18470">(&#167;4.5)</a> can hold or that an expression can produce, limit the operations 
supported on those values, and determine the meaning of the operations. Strong 
typing helps detect errors at compile time.
<p><a name="50743"></a>
The types of the Java language are divided into two categories: primitive types and reference types. The primitive types <a href="4.doc.html#85587">(&#167;4.2)</a> are the <code>boolean</code> type and the numeric types. The numeric types are the integral types <code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>, and <code>char</code>, and the floating-point types <code>float</code> and <code>double</code>. The reference types <a href="4.doc.html#9317">(&#167;4.3)</a> are class types, interface types, and array types. There is also a special null type. An object <a href="4.doc.html#12028">(&#167;4.3.1)</a> in Java is a dynamically created instance of a class type or a dynamically created array. The values of a reference type are references to objects. All objects, including arrays, support the methods of class <code>Object</code> <a href="4.doc.html#11055">(&#167;4.3.2)</a>. String literals are represented by <code>String</code> objects <a href="4.doc.html#26992">(&#167;4.3.3)</a>.<p>
<a name="50838"></a>
Types are the same <a href="4.doc.html#52197">(&#167;4.3.4)</a> if they have the same fully qualified names and are loaded by the same class loader. Names of types are used <a href="4.doc.html#25948">(&#167;4.4)</a> in declarations, in casts, in class instance creation expressions, in array creation expressions, and in <code>instanceof</code> operator expressions.<p>
<a name="50800"></a>
A variable <a href="4.doc.html#18470">(&#167;4.5)</a> is a storage location. A variable of a primitive type always holds a value of that exact type. A variable of a class type <i>T</i><em></em> can hold a null reference or a reference to an instance of class <i>T</i><em></em> or of any class that is a subclass of <i>T</i>. A variable of an interface type can hold a null reference or a reference to any instance of any class that implements the interface. If <i>T</i> is a primitive type, then a variable of type "array of <i>T</i><em></em>" can hold a null reference or a reference to any array of type "array of <i>T</i><em></em>"; if <i>T</i> is a reference type, then a variable of type "array of <i>T</i><em></em>" can hold a null reference or a reference to any array of type "array of <i>S</i><em></em>" such that type <i>S</i><em></em> is assignable <a href="5.doc.html#170768">(&#167;5.2)</a> to type <i>T</i><em></em>. A variable of type <code>Object</code> can hold a null reference or a reference to any object, whether class instance or array.<p>
<a name="11128"></a>
<h2>4.1    The Kinds of Types and Values</h2>
<a name="10737"></a>
There are two kinds of <i>types</i> in Java: primitive types <a href="4.doc.html#85587">(&#167;4.2)</a> and reference types 
<a href="4.doc.html#9317">(&#167;4.3)</a>. There are, correspondingly, two kinds of data values that can be stored in 
variables, passed as arguments, returned by methods, and operated on: primitive 
values <a href="4.doc.html#85587">(&#167;4.2)</a> and reference values <a href="4.doc.html#9317">(&#167;4.3)</a>.
<p><ul><pre>
<i>Type:<br>
</i>	<i>PrimitiveType<br>
	ReferenceType
</i></pre></ul><a name="23953"></a>
There is also a special <i>null type</i>, the type of the expression <code>null</code>, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the Java programmer can ignore the null type and just pretend that <code>null</code> is merely a special literal that can be of any reference type.<p>
<a name="85587"></a>
<h2>4.2    Primitive Types and Values</h2>
<a name="9122"></a>
A <i>primitive type</i> is predefined by the Java language and named by its reserved 
keyword <a href="3.doc.html#229308">(&#167;3.9)</a>:
<p><ul><pre>
<i>PrimitiveType:<br>
	NumericType<br>
	</i><code>boolean
</code>
<i>NumericType:<br>
	IntegralType<br>
	FloatingPointType
</i>
<i>IntegralType:</i> <i>one</i> <i>of<br>
</i>	<code>byte short int long char
</code>
<i>FloatingPointType:</i> <i>one</i> <i>of<br>
</i><code>	float double
</code></pre></ul><a name="50919"></a>
Primitive values do not share state with other primitive values. A variable whose type is a primitive type always holds a primitive value of that same type. The value of a variable of primitive type can be changed only by assignment operations on that variable.<p>
<a name="9137"></a>
The <i>numeric types</i> are the integral types and the floating-point types. <p>
<a name="88061"></a>
The <i>integral types</i> are <code>byte</code>, <code>short</code>, <code>int</code>, and <code>long</code>, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and <code>char</code>, whose values are 16-bit unsigned integers representing Unicode characters.<p>
<a name="88062"></a>
The <i>floating-point types</i> are <code>float</code>, whose values are 32-bit IEEE 754 floating-point numbers, and <code>double</code>, whose values are 64-bit IEEE 754 floating-point numbers.<p>
<a name="50913"></a>
The <code>boolean</code> type has exactly two values: <code>true</code> and <code>false.</code><p>
<a name="9151"></a>
<h3>4.2.1    Integral Types and Values</h3>
<a name="50950"></a>
The values of the integral types are integers in the following ranges:
<p><ul><a name="9163"></a>
<li>For <code>byte</code>, from -128 to 127, inclusive
<a name="9164"></a>
<li>For <code>short</code>, from -32768 to 32767, inclusive
<a name="9165"></a>
<li>For <code>int</code>, from -2147483648 to 2147483647, inclusive
<a name="9166"></a>
<li>For <code>long</code>, from -9223372036854775808 to 9223372036854775807, inclusive
<a name="51034"></a>
<li>For <code>char</code>, from <code>'\u0000'</code> to <code>'\uffff'</code> inclusive, that is, from 0 to 65535
</ul><a name="51035"></a>
<h3>4.2.2    Integer Operations</h3>
<a name="29775"></a>
Java provides a number of operators that act on integral values:
<p><ul><a name="10358"></a>
<li>The comparison operators, which result in a value of type <code>boolean</code>:
<ul>
<a name="9181"></a>
<li>The numerical comparison operators <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, and <code>&gt;=</code> <a href="15.doc.html#153654">(&#167;15.19.1)</a>
<a name="17601"></a>
<li>The numerical equality operators <code>==</code> and <code>!=</code> <a href="15.doc.html#5198">(&#167;15.20.1)</a>
</ul>
<a name="10365"></a>
<li>The numerical operators, which result in a value of type <code>int</code> or <code>long</code>:
<ul>
<a name="18334"></a>
<li>The unary plus and minus operators <code>+</code> and <code>-</code> (<a href="15.doc.html#24924">&#167;15.14.3</a>, <a href="15.doc.html#236345">&#167;15.14.4</a>)
<a name="17539"></a>
<li>The multiplicative operators <code>*</code>, <code>/</code>, and <code>%</code> <a href="15.doc.html#239829">(&#167;15.16)</a>
<a name="19482"></a>
<li>The additive operators <code>+</code> and <code>-</code> <a href="15.doc.html#13510">(&#167;15.17.2)</a>
<a name="24026"></a>
<li>The increment operator <code>++</code>, both prefix <a href="15.doc.html#39547">(&#167;15.14.1)</a> and postfix <a href="15.doc.html#39438">(&#167;15.13.2)</a>
<a name="24036"></a>
<li>The decrement operator <code>--</code>, both prefix <a href="15.doc.html#239136">(&#167;15.14.2)</a> and postfix <a href="15.doc.html#4987">(&#167;15.13.3)</a>
<a name="19496"></a>
<li>The signed and unsigned shift operators <code>&lt;&lt;</code>, <code>&gt;&gt;</code>, and <code>&gt;&gt;&gt;</code> <a href="15.doc.html#5121">(&#167;15.18)</a>
<a name="51061"></a>
<li>The bitwise complement operator <code>~</code> <a href="15.doc.html#5017">(&#167;15.14.5)</a>
<a name="17770"></a>
<li>The integer bitwise operators <code>&amp;</code>, <code>|</code>, and <code>^</code> <a href="15.doc.html#5233">(&#167;15.21.1)</a>
</ul>
<a name="19469"></a>
<li>The conditional operator <code>? :</code> <a href="15.doc.html#5257">(&#167;15.24)</a>
<a name="18373"></a>
<li>The cast operator, which can convert from an integral value to a value of any specified numeric type (<a href="5.doc.html#176921">&#167;5.4</a>, <a href="15.doc.html#238146">&#167;15.15</a>)
<a name="17774"></a>
<li>The string concatenation operator <code>+</code> <a href="15.doc.html#39990">(&#167;15.17.1)</a>, which, when given a <code>String</code> operand and an integral operand, will convert the integral operand to a <code>String</code> representing its value in decimal form, and then produce a newly created <code>String</code> that is the concatenation of the two strings
</ul><a name="51025"></a>
Other useful constructors, methods, and constants are predefined in the classes 
<code>Integer</code> <a href="javalang.doc6.html#14348">(&#167;20.7)</a>, <code>Long</code> <a href="javalang.doc7.html#46750">(&#167;20.8)</a>, and <code>Character</code> <a href="javalang.doc4.html#14345">(&#167;20.5)</a>.
<p><a name="28057"></a>
If an integer operator other than a shift operator has at least one operand of type <code>long</code>, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type <code>long</code>. If the other operand is not <code>long</code>, it is first widened <a href="5.doc.html#25222">(&#167;5.1.2)</a> to type <code>long</code> by numeric promotion <a href="5.doc.html#26917">(&#167;5.6)</a>. Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type <code>int</code>. If either operand is not an <code>int</code>, it is first widened to type <code>int</code> by numeric promotion.<p>
<a name="9194"></a>
The built-in integer operators do not indicate overflow or underflow in any way. The only numeric operators that can throw an exception <a href="11.doc.html#44043">(&#167;11)</a> are the integer divide operator <code>/</code> <a href="15.doc.html#5047">(&#167;15.16.2)</a> and the integer remainder operator <code>%</code> <a href="15.doc.html#24956">(&#167;15.16.3)</a>, which throw an <code>ArithmeticException</code> if the right-hand operand is zero. <p>
<a name="86654"></a>
The example:<p>
<pre><a name="51117"></a>
class Test {
<a name="51118"></a>	public static void main(String[] args) {
<a name="51119"></a>		int i = 1000000;
<a name="51254"></a>		System.out.println(i * i);
<a name="51216"></a>		long l = i;
<a name="86535"></a>		System.out.println(l * l);
<a name="86536"></a>		System.out.println(20296 / (l - i));
<a name="86537"></a>	}
<a name="86538"></a>}
</pre><a name="51131"></a>
produces the output:
<p><pre><a name="51149"></a>
-727379968
<a name="51133"></a>1000000000000
</pre><a name="51199"></a>
and then encounters an <code>ArithmeticException</code> in the division by <code>l</code> <code>-</code> <code>i</code>, because 
<code>l</code> <code>-</code> <code>i</code> is zero. The first multiplication is performed in 32-bit precision, whereas the 
second multiplication is a <code>long</code> multiplication. The value <code>-727379968</code> is the decimal
value of the low 32 bits of the mathematical result, <code>1000000000000</code>, which is 
a value too large for type <code>int</code>.
<p><a name="51203"></a>
Any value of any integral type may be cast to or from any numeric type. There are no casts between integral types and the type <code>boolean</code>.<p>

⌨️ 快捷键说明

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