📄 15.doc.html
字号:
<ul><pre>
<i>Literal:<br>
IntegerLiteral<br>
FloatingPointLiteral<br>
BooleanLiteral<br>
CharacterLiteral<br>
StringLiteral<br>
NullLiteral
</i></pre></ul><a name="36400"></a>
The type of a literal is determined as follows:<p>
<ul><a name="43814"></a>
<li>The type of an integer literal that ends with <code>L</code> or <code>l</code> is <code>long</code>; the type of any other integer literal is <code>int</code>.
<a name="36413"></a>
<li>The type of a floating-point literal that ends with <code>F</code> or <code>f</code> is <code>float</code>; the type of any other floating-point literal is <code>double</code>.
<a name="36429"></a>
<li>The type of a boolean literal is <code>boolean</code>.
<a name="36436"></a>
<li>The type of a character literal is <code>char</code>.
<a name="36437"></a>
<li>The type of a string literal is <code>String</code>.
<a name="36411"></a>
<li>The type of the null literal <code>null</code> is the null type; its value is the null reference.
</ul><a name="36442"></a>
Evaluation of a literal always completes normally.<p>
<a name="31980"></a>
<h3>15.7.2 <code>this</code> </h3>
<a name="20069"></a>
The keyword <code>this</code> may be used only in the body of an instance method or constructor,
or in the initializer of an instance variable of a class. If it appears anywhere
else, a compile-time error occurs.
<p><a name="31983"></a>
When used as a primary expression, the keyword <code>this</code> denotes a value, that is a reference to the object for which the instance method was invoked <a href="15.doc.html#20448">(§15.11)</a>, or to the object being constructed. The type of <code>this</code> is the class <i>C</i> within which the keyword <code>this</code> occurs. At run time, the class of the actual object referred to may be the class <i>C</i> or any subclass of <i>C</i>.<p>
<a name="31984"></a>
In the example:<p>
<pre><a name="31985"></a>
class IntVector {
<a name="20035"></a> int[] v;
<a name="20033"></a> boolean equals(IntVector other) {
<a name="20034"></a> if (this == other)
<a name="43815"></a> return true;
<a name="20048"></a> if (v.length != other.v.length)
<a name="43816"></a> return false;
<a name="20049"></a> for (int i = 0; i < v.length; i++)
<a name="20050"></a> if (v[i] != other.v[i])
<a name="20051"></a> return false;
<a name="20052"></a> return true;
<a name="20053"></a> }
<br><a name="20028"></a>}
</pre><a name="36469"></a>
the class <code>IntVector</code> implements a method <code>equals</code>, which compares two vectors.
If the <code>other</code> vector is the same vector object as the one for which the <code>equals</code>
method was invoked, then the check can skip the length and value comparisons.
The <code>equals</code> method implements this check by comparing the reference to the
<code>other</code> object to <code>this</code>.
<p><a name="20077"></a>
The keyword <code>this</code> is also used in a special explicit constructor invocation statement, which can appear at the beginning of a constructor body <a href="8.doc.html#78435">(§8.6.5)</a>.<p>
<a name="236822"></a>
<h3>15.7.3 Parenthesized Expressions</h3>
<a name="236823"></a>
A parenthesized expression is a primary expression whose type is the type of the
contained expression and whose value at run time is the value of the contained
expression.
<p><a name="41147"></a>
<h2>15.8 Class Instance Creation Expressions</h2>
<a name="36595"></a>
A class instance creation expression is used to create new objects that are
instances of classes.
<p><ul><pre>
<i>ClassInstanceCreationExpression:<br>
</i> <code>new </code><i>ClassType</i><code> ( </code><i>ArgumentList</i><sub><i>opt</i></sub><code> )
</code>
<i>ArgumentList:<br>
</i> <i>Expression<br>
</i> <i>ArgumentList</i><code> , </code><i>Expression
</i></pre></ul><a name="224294"></a>
In a class instance creation expression, the <i>ClassType </i>must name a class that is not <code>abstract</code>. This class type is the type of the creation expression.<p>
<a name="224300"></a>
The arguments in the argument list, if any, are used to select a constructor declared in the body of the named class type, using the same matching rules as for method invocations <a href="15.doc.html#20448">(§15.11)</a>. As in method invocations, a compile-time method matching error results if there is no unique constructor that is both applicable to the provided arguments and the most specific of all the applicable constructors.<p>
<a name="23745"></a>
<h3>15.8.1 Run-time Evaluation of Class Instance Creation Expressions</h3>
<a name="36655"></a>
At run time, evaluation of a class instance creation expression is as follows.
<p><a name="23746"></a>
First, space is allocated for the new class instance. If there is insufficient space to allocate the object, evaluation of the class instance creation expression completes abruptly by throwing an <code>OutOfMemoryError</code> <a href="15.doc.html#36687">(§15.8.2)</a>.<p>
<a name="23753"></a>
The new object contains new instances of all the fields declared in the specified class type and all its superclasses. As each new field instance is created, it is initialized to its standard default value <a href="4.doc.html#10931">(§4.5.4)</a>.<p>
<a name="36584"></a>
Next, the argument list is evaluated, left-to-right. If any of the argument evaluations completes abruptly, any argument expressions to its right are not evaluated, and the class instance creation expression completes abruptly for the same reason.<p>
<a name="36513"></a>
Next, the selected constructor of the specified class type is invoked. This results in invoking at least one constructor for each superclass of the class type. This process can be directed by explicit constructor invocation statements <a href="8.doc.html#41652">(§8.6)</a> and is described in detail in <a href="12.doc.html#44670">§12.5</a>.<p>
<a name="23747"></a>
The value of a class instance creation expression is a reference to the newly created object of the specified class. Every time the expression is evaluated, a fresh object is created.<p>
<a name="36687"></a>
<h3>15.8.2 Example: Evaluation Order and Out-of-Memory Detection</h3>
<a name="36695"></a>
If evaluation of a class instance creation expression finds there is insufficient
memory to perform the creation operation, then an <code>OutOfMemoryError</code> is thrown.
This check occurs before any argument expressions are evaluated.
<p><a name="36696"></a>
So, for example, the test program:<p>
<pre><a name="36697"></a>
class List {
<a name="36776"></a> int value;
<a name="36699"></a> List next;
<a name="36700"></a> static List head = new List(0);
<a name="36773"></a> List(int n) { value = n; next = head; head = this; }
<a name="36701"></a>}
<a name="36702"></a>
class Test {
<a name="36703"></a> public static void main(String[] args) {
<a name="36704"></a> int id = 0, oldid = 0;
<a name="36706"></a> try {
<a name="36841"></a> for (;;) {
<a name="36707"></a> ++id;
<a name="36708"></a> new List(oldid = id);
<a name="36847"></a> }
<a name="36709"></a> } catch (Error e) {
<a name="36710"></a> System.out.println(e + ", " + (oldid==id));
<a name="36713"></a> }
<a name="36714"></a> }
<a name="36715"></a>}
</pre><a name="36716"></a>
prints:
<p><pre><a name="36787"></a>java.lang.OutOfMemoryError: List, false
</pre><a name="36788"></a>
because the out-or-memory condition is detected before the argument expression
<code>oldid</code> <code>=</code> <code>id</code> is evaluated.
<p><a name="46163"></a>
Compare this to the treatment of array creation expressions <a href="15.doc.html#46168">(§15.9)</a>, for which the out-of-memory condition is detected after evaluation of the dimension expressions <a href="15.doc.html#36736">(§15.9.3)</a>.<p>
<a name="46168"></a>
<h2>15.9 Array Creation Expressions</h2>
<a name="46169"></a>
An array instance creation expression is used to create new arrays <a href="10.doc.html#27803">(§10)</a>.
<p><ul><pre>
<i>ArrayCreationExpression:<br>
</i> <code>new </code><i>PrimitiveType</i><code> </code><i>DimExprs</i><code> </code><i>Dims</i><sub><i>opt<br>
</i></sub><code>new </code><i>TypeName</i><code> </code><i>DimExprs</i><code> </code><i>Dims</i><sub><i>opt
</i></sub>
<i>DimExprs:<br>
</i> <i>DimExpr<br>
</i> <i>DimExprs</i><code> </code><i>DimExpr
</i>
<i>DimExpr:<br>
</i> <code>[ </code><i>Expression</i><code> ]
</code>
<i>Dims:<br>
</i> <code>[ ]<br>
</code> <i>Dims</i><code> [ ]
</code></pre></ul><a name="224310"></a>
An array creation expression creates an object that is a new array whose elements are of the type specified by the <i>PrimitiveType</i> or <i>TypeName</i>. The <i>TypeName</i> may name any reference type, even an <code>abstract</code> class type <a href="8.doc.html#34944">(§8.1.2.1)</a> or an interface type <a href="9.doc.html#238678">(§9)</a>.<p>
<a name="11508"></a>
The type of the creation expression is an array type that can denoted by a copy of the creation expression from which the <code>new</code> keyword and every <i>DimExpr</i> expression have been deleted; for example, the type of the creation expression:<p>
<pre><a name="224311"></a>new double[3][3][]
</pre><a name="224312"></a>
is:
<p><pre><a name="224313"></a>double[][][]
</pre><a name="23548"></a>
The type of each dimension expression <i>DimExpr</i> must be an integral type, or a compile-time error occurs. Each expression undergoes unary numeric promotion <a href="5.doc.html#170952">(§5.6.1)</a>. The promoted type must be <code>int</code>, or a compile-time error occurs; this means, specifically, that the type of a dimension expression must not be <code>long</code>.<p>
<a name="23605"></a>
<h3>15.9.1 Run-time Evaluation of Array Creation Expressions</h3>
<a name="36668"></a>
At run time, evaluation of an array creation expression behaves as follows.
<p><a name="23552"></a>
First, the dimension expressions are evaluated, left-to-right. If any of the expression evaluations completes abruptly, the expressions to the right of it are not evaluated.<p>
<a name="23543"></a>
Next, the values of the dimension expressions are checked. If the value of any <i>DimExpr</i> expression is less than zero, then an <code>NegativeArraySizeException</code> is thrown.<p>
<a name="36922"></a>
Next, space is allocated for the new array. If there is insufficient space to allocate the array, evaluation of the array creation expression completes abruptly by throwing an <code>OutOfMemoryError</code>.<p>
<a name="23658"></a>
Then, if a single <i>DimExpr</i> appears, a single-dimensional array is created of the specified length, and each component of the array is initialized to its standard default value <a href="4.doc.html#10931">(§4.5.4)</a>.<p>
<a name="23640"></a>
If an array creation expression contains <i>N</i> <i>DimExpr</i> expressions, then it effectively executes a set of nested loops of depth <img src="15.doc.anc7.gif"> to create the implied arrays of arrays. For example, the declaration:<p>
<pre><a name="23641"></a><code>float[][] matrix = new float[3][3];
</code></pre><a name="23642"></a>
is equivalent in behavior to:
<p><pre><a name="23643"></a>
float[][] matrix = new float[3][];
<a name="23644"></a>for (int <i>d</i> = 0; <i>d</i> < matrix.length; <i>d</i>++)
<a name="50206"></a> matrix[<i>d</i>] = new float[3];
</pre><a name="23645"></a>
and:
<p><pre><a name="23646"></a>Age[][][][][] Aquarius = new Age[6][10][8][12][];
</pre><a name="23647"></a>
is equivalent to:
<p><pre><a name="23648"></a>
Age[][][][][] Aquarius = new Age[6][][][][];
<a name="23649"></a>for (int <i>d1</i> = 0; <i>d1</i> < Aquarius.length; <i>d1</i>++) {
<a name="237599"></a> Aquarius[<i>d1</i>] = new Age[8][][][];
<a name="237600"></a> for (int <i>d2</i> = 0; <i>d2</i> < Aquarius[<i>d1</i>].length; <i>d2</i>++) {
<a name="237601"></a> Aquarius[<i>d1</i>][<i>d2</i>] = new Age[10][][];
<a name="50214"></a> for (int <i>d3</i> = 0; <i>d3</i> < Aquarius[<i>d1</i>][<i>d2</i>].length; <i>d3</i>++) {
<a name="50216"></a> Aquarius[<i>d1</i>][<i>d2</i>][<i>d3</i>] = new Age[12][];
<a name="50218"></a> }
<a name="50220"></a> }
<a name="50222"></a>}
</pre><a name="36995"></a>
with <i>d,</i> <i>d1</i>, <i>d2</i> and <i>d3</i> replaced by names that are not already locally declared.
Thus, a single <code>new</code> expression actually creates one array of length 6, 6 arrays of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -