📄 concepts.doc.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><html><head><title>VM Spec Java Concepts</title></head><body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000><table width=100%><tr><td><a href="VMSpecTOC.doc.html">Contents</a> | <a href="Introduction.doc.html">Prev</a> | <a href="Overview.doc.html">Next</a> | <a href="Lindholm.INDEX.html">Index</a></td><td align=right><i><i>The Java<sup><font size=-2>TM</font></sup> Virtual Machine Specification</i></i></td></tr></table><hr><br> <a name="27073"></a><p><strong>CHAPTER 2 </strong></p><a name="27074"></a><h1>Java Concepts</h1><hr><p><a name="27075"></a>The Java Virtual Machine was designed to support the Java programming language. Some concepts and vocabulary from the Java language are thus necessary to understand the virtual machine. This chapter gives enough of an overview of Java to support the discussion of the Java Virtual Machine to follow. Its material has been condensed from <i>The Java Language Specification</i>, by James Gosling, Bill Joy, and Guy Steele. For a complete discussion of the Java language, or for details and examples of the material in this chapter, refer to that book. Readers familiar with that book<i> </i>may wish to skip this chapter. Readers familiar with Java, but not with <i>The</i> <i>Java Language Specification</i>, should at least skim this chapter for the terminology it introduces.<p><a name="22928"></a>This chapter does not attempt to provide an introduction to or a full treatment of the Java language. For an introduction to Java, see <i>The Java Programming Language</i>, by Ken Arnold and James Gosling.<p><a name="25310"></a><hr><h2>2.1 Unicode</h2><a name="25315"></a>Java programs are written using the<i> Unicode</i> character encoding, version 1.1.5, as specified in <em>The Unicode Standard: Worldwide Character Encoding</em>, Version 1.0, Volume 1, ISBN 0-201-56788-1, and Volume 2, ISBN 0-201-60845-6, and the update information about Unicode 1.1.5 available at <code>ftp://unicode.org</code>. There are a few minor errors in this update information; refer to <i>The Java Language Specification</i> for corrections. Updates to the Unicode information published there will be posted under the URL <code>http://java.sun.com/docs/books/index.html</code>.<p><a name="23148"></a>Except for comments and identifiers <a href="Concepts.doc.html#25339">(§2.2)</a> and the contents of character and string literals <a href="Concepts.doc.html#23152">(§2.3)</a>, all input elements in a Java program are formed from only <i>ASCII</i> characters. ASCII (ANSI X3.4) is the American Standard Code for Information Interchange. The first 128 characters of the Unicode character encoding are the ASCII characters.<p><a name="25339"></a><hr><h2>2.2 Identifiers</h2><a name="23857"></a>An <i>identifier</i> is an unlimited-length sequence of Unicode <i>letters</i> and <i>digits</i>, the first of which must be a letter. Letters and digits may be drawn from the entire Unicode character set, which supports most writing scripts in use in the world today. This allows Java programmers to use identifiers in their programs that are written in their native languages.<p><a name="23858"></a>The Java method <code>Character.isJavaLetter</code> returns <code>true</code> when passed a Unicode character that is considered to be a letter in Java identifiers. The Java method <code>Character.isJavaLetterOrDigit</code> returns <code>true</code> when passed a Unicode character that is considered to be a letter or digit in Java identifiers.<p><a name="23151"></a>Two identifiers are the same only if they have the same Unicode character for each letter or digit; identifiers that have the same external appearance may still be different. An identifier must not be the same as a Java keyword or a boolean literal (<code>true or</code> <code>false</code>).<p><a name="23152"></a><hr><h2>2.3 Literals</h2><a name="23156"></a>A <i>literal</i> is the source code representation of a value of a primitive type <a href="Concepts.doc.html#19511">(§2.4.1)</a>, the <code>String</code> type <a href="Concepts.doc.html#25486">(§2.4.7)</a>, or the null type <a href="Concepts.doc.html#22930">(§2.4)</a>. String literals and, more generally, strings that are the values of constant expressions, are "interned" so as to share unique instances, using the method <code>String.intern</code>. <p><a name="27355"></a>The null type has one value, the null reference, denoted by the literal <code>null</code>.<p><a name="27356"></a>The <code>boolean</code> type has two values, denoted by the literals <code>true</code> and <code>false</code>.<p><a name="22930"></a><hr><h2>2.4 Types and Values</h2><a name="17177"></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="Concepts.doc.html#17203">(§2.5)</a> can hold or that an expression can produce, limit the operations supported on those values, and determine the meaning of those operations. Strong typing helps detect errors at compile time. <p><a name="23869"></a>The types of the Java language are divided into two categories: <i>primitive types</i> <a href="Concepts.doc.html#19511">(§2.4.1)</a> and <i>reference types</i> <a href="Concepts.doc.html#25376">(§2.4.5)</a>. There is also a special <i>null type</i>, the type of the expression <code>null</code>, which has no name. The null reference is the only possible value of an expression of null type, and can always be converted to any reference type. In practice, the Java programmer can ignore the null type and just pretend that <code>null</code> is a special literal that can be of any reference type.<p><a name="23205"></a>Corresponding to the primitive types and reference types, there are two categories of data values that can be stored in variables, passed as arguments, returned by methods, and operated upon: <i>primitive values</i> <a href="Concepts.doc.html#19511">(§2.4.1)</a> and <i>reference values</i> <a href="Concepts.doc.html#25376">(§2.4.5)</a>.<p><a name="19511"></a><h3>2.4.1 Primitive Types and Values</h3><a name="27843"></a>A <i>primitive type</i> is a type that is predefined by the Java language and named by a reserved keyword. <i>Primitive values</i> do not share state with other primitive values. A variable whose type is a primitive type always holds a primitive value of that type.<a href="#27846"><sup>1</sup></a><p><a name="23025"></a>The primitive types are the <code>boolean</code> <i>type</i> and the <i>numeric types</i>. The numeric types are the <i>integral types</i> and the <i>floating-point types. </i><p><a name="23026"></a>The integral types 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 <a href="Concepts.doc.html#25310">(§2.1)</a>. <p><a name="19367"></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 as specified in <i>IEEE Standard for Binary Floating-Point Arithmetic</i>, ANSI/IEEE Standard 754-1985 (IEEE, New York). The IEEE 754 standard includes not only positive and negative sign-magnitude numbers, but also positive and negative zeroes, positive and negative <i>infinities</i>, and a special Not-a-Number (hereafter abbreviated NaN) value. The NaN value is used to represent the result of certain operations such as dividing zero by zero.<p><a name="23033"></a>The <code>boolean</code> type has the truth values <code>true</code> and <code>false</code>.<p><a name="17876"></a><h3>2.4.2 Operators on Integral Values</h3><a name="17881"></a>Java provides a number of operators that act on integral values, including numerical comparison (which results in a value of type <code>boolean</code>), arithmetic operators, increment and decrement, bitwise logical and shift operators, and numeric cast <a href="Concepts.doc.html#18168">(§2.6.8)</a>.<p><a name="20881"></a>Operands of certain unary and binary operators are subject to numeric promotion <a href="Concepts.doc.html#16021">(§2.6.9)</a>.<p><a name="17886"></a>The built-in integer operators do not indicate overflow or underflow in any way; they wrap around on overflow or underflow. The only integer operators that can throw an exception are the integer divide and integer remainder operators, which can throw an <code>ArithmeticException</code> if the right-hand operand is zero.<p><a name="23049"></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><a name="17889"></a><h3>2.4.3 Operators on Floating-Point Values</h3><a name="17902"></a>Java provides a number of operators that act on floating-point values, including numerical comparison (which results in a value of type <code>boolean</code>), arithmetic operators, increment and decrement, and numeric cast <a href="Concepts.doc.html#18168">(§2.6.8)</a>.<p><a name="19472"></a>If at least one of the operands to a binary operator is of floating-point type, then the operation is a floating-point operation, even if the other operand is integral. Operands of certain unary and binary operators are subject to numeric promotion <a href="Concepts.doc.html#16021">(§2.6.9)</a>.<p><a name="23051"></a>Operators on floating-point numbers behave exactly as specified by IEEE 754. In particular, Java requires support of IEEE 754 <i>denormalized</i> floating-point numbers and <i>gradual underflow</i>, which make it easier to prove desirable properties of particular numerical algorithms.<p><a name="17932"></a>Java requires that floating-point arithmetic behave as if every floating-point operator rounded its floating-point result to the result precision. <i>Inexact</i> results must be rounded to the representable value nearest to the infinitely precise result; if the two nearest representable values are equally near, the one with its least significant bit zero is chosen. This is the IEEE 754 standard's default rounding mode known as <i>round-to-nearest</i>.<p><a name="17933"></a>Java uses <i>round-towards-zero</i> mode when converting a floating-point value to an integer <a href="Concepts.doc.html#26142">(§2.6.3)</a>. Round-towards-zero mode acts as though the number were truncated, discarding the mantissa bits. Round-towards-zero chooses as its result the format's value closest to and no greater in magnitude than the infinitely precise result.<p><a name="17934"></a>Java floating-point operators produce no exceptions <a href="Concepts.doc.html#22727">(§2.15)</a>. An operation that overflows produces a signed infinity; an operation that underflows produces a signed zero; and an operation that has no mathematically definite result produces <code></code>NaN. All numeric operations (except for numeric comparison) with <code></code>NaN as an operand produce <code></code>NaN as a result.<p><a name="23230"></a>Any value of any floating-point type may be cast <a href="Concepts.doc.html#18168">(§2.6.8)</a> to or from any numeric type. There are no casts between floating-point types and the type <code>boolean</code>.<p><a name="17937"></a><h3>2.4.4 Operators on <code>boolean</code> Values </h3><a name="18642"></a>The boolean operators include relational operators and logical operators. Only <code>boolean</code> expressions can be used in Java's control flow statements and as the first operand of the conditional operator <code>?:</code>. An integral value <code>x</code> can be converted to a value of type <code>boolean</code>, following the C language convention that any nonzero value is <code>true</code>, by the expression <code>x!=0</code>. An object reference <code>obj</code> can be converted to a value of type <code>boolean</code>, following the C language convention that any reference other than <code>null</code> is <code>true</code>, by the expression <code>obj!=null</code>.<p><a name="23229"></a>There are no casts between the type <code>boolean</code> and any other type.<p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -