📄 concepts.doc.html
字号:
<a name="16022"></a><i>Numeric promotion </i>is applied to the operands of an arithmetic operator. Numeric promotion contexts allow the use of an identity conversion <a href="Concepts.doc.html#19691">(§2.6.1)</a> or a widening primitive conversion <a href="Concepts.doc.html#23435">(§2.6.2)</a><i>.</i><p><a name="28838"></a>Numeric promotions are used to convert the operands of a numeric operator to a common type where an operation can be performed. The two kinds of numeric promotion are <i>unary numeric promotion</i> and <i>binary numeric promotion</i>. The analogous conversions in C are called "the usual unary conversions" and "the usual binary conversions." Numeric promotion is not a general feature of the Java programming language, but rather a property of specific built-in operators. <p><a name="20080"></a>An operator that applies unary numeric promotion to a single operand of numeric type converts an operand of type <code>byte</code>, <code>short</code>, or <code>char</code> to <code>int</code> by a widening primitive conversion, and otherwise leaves the operand alone. Value set conversion <a href="Concepts.doc.html#32922">(§2.6.6)</a> is then applied. The operands of the shift operators are promoted independently using unary numeric promotions.<p><a name="20091"></a>When an operator applies binary numeric promotion to a pair of numeric operands, the following rules apply, in order, using widening primitive conversion to convert operands as necessary:<p><ul><li>If either operand is of type <code>double</code>, the other is converted to <code>double</code>.<p><li>Otherwise, if either operand is of type <code>float</code>, the other is converted to <code>float</code>.<p><li>Otherwise, if either operand is of type <code>long</code>, the other is converted to <code>long</code>.<p><li>Otherwise, both operands are converted to type <code>int</code>.</ul><a name="32967"></a>After type conversion, if any, value set conversion is applied to each operand.<p><a name="21272"></a><hr><h2>2.7 Names and Packages</h2><a name="37331"></a><i>Names</i> are used to refer to entities declared in a program. A declared entity is a package, type, member (field or method) of a type, parameter, or local variable. Programsare organized sets of <i>packages</i>. <p><a name="21410"></a><h3>2.7.1 Simple Names and Qualified Names</h3><a name="29689"></a>A <i>simple name</i> is a single identifier <a href="Concepts.doc.html#25339">(§2.2)</a>. <i>Qualified names</i> <a href="Concepts.doc.html#18914">(§2.7.4)</a> provide access to members of packages and reference types. A <i></i>qualified name consists of a name, a "." token, and an identifier. <p><a name="18902"></a>Not all identifiers are part of a name. Identifiers are also used in declarations, where the identifier determines the name by which an entity will be known, in field access expressions and method invocation expressions, and in statement labels and <code>break</code> and <code>continue</code> statements that refer to statement labels.<p><a name="20319"></a><h3>2.7.2 Packages</h3><a name="18906"></a>A package consists of a number of compilation units and has a hierarchical name. Packages are independently developed, and each package has its own set of names, which helps to prevent name conflicts. Each Java virtual machine implementation determines how packages, compilation units, and subpackages are created and stored; which top-level package names are in scope in a particular compilation; and which packages are accessible. Packages may be stored in a local file system, in a distributed file system, or in some form of database.<p><a name="20320"></a>A package name component or class name might contain a character that cannot legally appear in a host file system's ordinary directory or file name: for instance, a Unicode character on a system that allows only ASCII characters in file names. <p><a name="26168"></a>A Java virtual machine implementation must support at least one unnamed package; it may support more than one but is not required to do so. Which compilation units are in each unnamed package is determined by the host system. Unnamed packages are provided principally for convenience when developing small or temporary applications or when just beginning development.<p><a name="20328"></a>An <code>import</code> declaration allows a type declared in another package to be known by a simple name rather than by the fully qualified name <a href="Concepts.doc.html#20207">(§2.7.5)</a> of the type. An import declaration affects only the type declarations of a single compilation unit. A compilation unit automatically imports each of the <code>public</code> type names declared in the predefined package <code>java.lang</code>.<p><a name="26049"></a><h3>2.7.3 Members</h3><a name="26053"></a>Packages and reference types have <i>members</i>. The members of a package <a href="Concepts.doc.html#20319">(§2.7.2)</a> are subpackages and all the class <a href="Concepts.doc.html#29321">(§2.8)</a> and interface <a href="Concepts.doc.html#16432">(§2.13)</a> types declared in all the compilation units of the package. The members of a reference type are fields <a href="Concepts.doc.html#16338">(§2.9)</a>, methods <a href="Concepts.doc.html#16317">(§2.10)</a>, and nested classes and interfaces.<p><a name="27480"></a><h4>2.7.3.1 The Members of a Package</h4><a name="27481"></a>In general, the subpackages of a package are determined by the host system. However,the standard package <code>java</code> always has the subpackages <code>lang</code>, <code>util</code>, <code>io</code>, and <code>net</code>. No two distinct members of the same package may have the same simple name <a href="Concepts.doc.html#21410">(§2.7.1)</a>, but members of different packages may have the same simple name.<p><a name="26071"></a><h4>2.7.3.2 The Members of a Class Type</h4><a name="26072"></a>The members of a class type <a href="Concepts.doc.html#29321">(§2.8)</a> are fields <a href="Concepts.doc.html#16338">(§2.9)</a>, methods <a href="Concepts.doc.html#16317">(§2.10)</a>, and nested classes and interfaces. These include members inherited from its direct superclass <a href="Concepts.doc.html#32983">(§2.8.3)</a>, if it has one, members inherited from any direct superinterfaces <a href="Concepts.doc.html#20603">(§2.13.2)</a>, and any members declared in the body of the class. There is no restriction against a field and a method of a class type having the same simple name. <p><a name="26073"></a>A class type may have two or more methods with the same simple name if they have different numbers of parameters or different parameter types in at least one parameter position. Such a method member name is said to be <i>overloaded</i>. A class type may contain a declaration for a method with the same name and the same signature as a method that would otherwise be inherited from a superclass or superinterface. In this case, the method of the superclass or superinterface is not inherited. If the method not inherited is <code>abstract</code>, the new declaration is said to <i>implement</i> the method; if it is not <code>abstract</code>, the new declaration is said to <i>override</i> it.<p><a name="26076"></a><h4>2.7.3.3 The Members of an Interface Type</h4><a name="26077"></a>The members of an interface type <a href="Concepts.doc.html#16432">(§2.13)</a> are fields, methods, and nested classes and interfaces. The members of an interface are the members inherited from any direct superinterfaces <a href="Concepts.doc.html#20603">(§2.13.2)</a> and members declared in the body of the interface.<p><a name="26078"></a><h4>2.7.3.4 The Members of an Array Type</h4><a name="26079"></a>The members of an array type <a href="Concepts.doc.html#16446">(§2.15)</a> are the members inherited from its superclass, the class <code>Object</code> <a href="Concepts.doc.html#27433">(§2.4.7)</a>, and the field <code>length</code>, which is a constant (<code>final</code>) field of every array.<p><a name="18914"></a><h3>2.7.4 Qualified Names and Access Control</h3><a name="28883"></a>Qualified names <a href="Concepts.doc.html#21410">(§2.7.1)</a> are a means of access to members of packages and referencetypes; related means of access include field access expressions and method invocation expressions. All three are syntactically similar in that a "." token appears, preceded by some indication of a package, type, or expression having a type and followedby an identifier that names a member of the package or type. These are collectivelyknown as constructs for <i>qualified access</i>. <p><a name="28884"></a>The Java programming language provides mechanisms for limiting qualified access, to prevent users of a package or class from depending on unnecessary details of the implementation of that package or class. Access control also applies to constructors.<p><a name="26122"></a>Whether a package is accessible is determined by the host system.<p><a name="26129"></a>A class or interface may be declared <code>public</code>, in which case it may be accessed, using a qualified name, by any class or interface that can access the package in which it is declared. A class or interface that is not declared <code>public</code> may be accessed from, and only from, anywhere in the package in which it is declared.<p><a name="26130"></a>Every field or method of an interface must be <code>public</code>. Every member of a <code>public</code> interface is implicitly <code>public</code>, whether or not the keyword <code>public</code> appears in its declaration. It follows that a member of an interface is accessible if and only if the interface itself is accessible.<p><a name="26131"></a>A field, method, or constructor of a class may be declared using at most one of the <code>public</code>, <code>private</code>, or <code>protected</code> keywords. A <code>public</code> member may be accessed by any class or interface. A <code>private</code> member may be accessed only from within the class that contains its declaration. A member that is not declared <code>public</code>, <code>protected</code>, or <code>private</code> is said to have <i>default access</i> and may be accessed from, and only from, anywhere in the package in which it is declared.<p><a name="26127"></a>A <code>protected</code> member of an object may be accessed only by code responsible for the implementation of that object. To be precise, a <code>protected</code> member may be accessed from anywhere in the package in which it is declared and, in addition, it may be accessed from within any declaration of a subclass of the class type that contains its declaration, provided that certain restrictions are obeyed. <p><a name="20207"></a><h3>2.7.5 Fully Qualified Names</h3><a name="26136"></a>Every package, class, interface, array type, and primitive type has a fully qualified name. It follows that every type except the null type has a fully qualified name.<p><ul><li>The fully qualified name of a primitive type is the keyword for that primitive type, namely, <code>boolean</code>, <code>char</code>, <code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>, <code>float</code>, or <code>double</code>.<p><li>The fully qualified name of a named package that is not a subpackage of a named package is its simple name.<p><li>The fully qualified name of a named package that is a subpackage of another named package consists of the fully qualified name of the containing package followed by "<code>.</code>" followed by the simple (member) name of the subpackage.<p><li>The fully qualified name of a class or interface that is declared in an unnamed package is the simple name of the class or interface.<p><li>The fully qualified name of a class or interface that is declared in a named package consists of the fully qualified name of the package followed by "<code>.</code>" followed by the simple name of the class or interface.<p><li>The fully qualified name of an array type consists of the fully qualified name of the component type of the array type followed by "<code>[]</code>".</ul><a name="29321"></a><hr><h2>2.8 Classes</h2><a name="29324"></a>A <i>class declaration </i>specifies a new reference type and provides its implementation. Each class is implemented as an extension or subclass of a single existing class. A class may also implement one or more interfaces.<p><a name="18225"></a>The body of a class declares members (fields and met
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -