Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
13189 lines (12752 sloc) 897 KB
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter&nbsp;15.&nbsp;Expressions</title>
<link rel="stylesheet" type="text/css" href="../../../javaspec.css">
<meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1">
<link rel="home" href="index.html" title="The Java&reg; Language Specification">
<link rel="up" href="index.html" title="The Java&reg; Language Specification">
<link rel="prev" href="jls-14.html" title="Chapter&nbsp;14.&nbsp;Blocks and Statements">
<link rel="next" href="jls-16.html" title="Chapter&nbsp;16.&nbsp;Definite Assignment">
<link rel="copyright" href="jls-0-front.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:rx="http://www.renderx.com/XSL/Extensions" id="logo"><img src="http://www.oracleimg.com/us/assets/oralogo-small.gif" alt="Oracle Logo"><br><p><a target="_blank" href="http://www.oracle.com/us/technologies/java/">Oracle
Technology Network</a> &gt; <a target="_blank" href="http://docs.oracle.com/javase/">Java SE</a>
&gt; <a href="index.html">Java Language Specification</a></p>
</div>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Chapter&nbsp;15.&nbsp;Expressions</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="jls-14.html">Prev</a>&nbsp;
</td>
<th width="60%" align="center">&nbsp;</th>
<td width="20%" align="right">&nbsp;<a accesskey="n" href="jls-16.html">Next</a></td>
</tr>
</table>
<hr>
</div>
<div lang="en" class="chapter" title="Chapter&nbsp;15.&nbsp;Expressions">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="jls-15"></a>Chapter&nbsp;15.&nbsp;Expressions
</h2>
</div>
</div>
</div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.1">15.1. Evaluation, Denotation, and Result</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.2">15.2. Forms of Expressions</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.3">15.3. Type of an Expression</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.4">15.4. FP-strict Expressions</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.5">15.5. Expressions and Run-Time Checks</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.6">15.6. Normal and Abrupt Completion of Evaluation</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.7">15.7. Evaluation Order</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.7.1">15.7.1. Evaluate Left-Hand Operand First</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.7.2">15.7.2. Evaluate Operands before Operation</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.7.3">15.7.3. Evaluation Respects Parentheses and Precedence</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.7.4">15.7.4. Argument Lists are Evaluated Left-to-Right</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.7.5">15.7.5. Evaluation Order for Other Expressions</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.8">15.8. Primary Expressions</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.8.1">15.8.1. Lexical Literals</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.8.2">15.8.2. Class Literals</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.8.3">15.8.3. <code class="literal">this</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.8.4">15.8.4. Qualified <code class="literal">this</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.8.5">15.8.5. Parenthesized Expressions</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.9">15.9. Class Instance Creation
Expressions</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.9.1">15.9.1. Determining the Class being Instantiated</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.9.2">15.9.2. Determining Enclosing Instances</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.9.3">15.9.3. Choosing the Constructor and its Arguments</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.9.4">15.9.4. Run-Time Evaluation of Class Instance Creation Expressions</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.9.5">15.9.5. Anonymous Class Declarations</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.9.5.1">15.9.5.1. Anonymous Constructors</a></span></dt>
</dl>
</dd>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.10">15.10. Array Creation and Access Expressions</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.10.1">15.10.1. Array Creation Expressions</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.10.2">15.10.2. Run-Time Evaluation of Array Creation Expressions</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.10.3">15.10.3. Array Access Expressions</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.10.4">15.10.4. Run-Time Evaluation of Array Access Expressions</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.11">15.11. Field Access Expressions</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.11.1">15.11.1. Field Access Using a Primary</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.11.2">15.11.2. Accessing Superclass Members using <code class="literal">super</code></a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.12">15.12. Method Invocation Expressions</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.12.1">15.12.1. Compile-Time Step 1: Determine Class or Interface to Search</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.12.2">15.12.2. Compile-Time Step 2: Determine
Method Signature</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.12.2.1">15.12.2.1. Identify Potentially Applicable Methods</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.12.2.2">15.12.2.2. Phase 1: Identify Matching Arity
Methods Applicable by Strict Invocation</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.12.2.3">15.12.2.3. Phase 2: Identify Matching Arity
Methods Applicable by Loose Invocation</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.12.2.4">15.12.2.4. Phase 3: Identify Methods
Applicable by Variable Arity Invocation</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.12.2.5">15.12.2.5. Choosing the Most Specific Method</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.12.2.6">15.12.2.6. Method Invocation Type</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.12.3">15.12.3. Compile-Time Step 3: Is the Chosen Method Appropriate?</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.12.4">15.12.4. Run-Time Evaluation of Method Invocation</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.12.4.1">15.12.4.1. Compute Target Reference (If Necessary)</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.12.4.2">15.12.4.2. Evaluate Arguments</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.12.4.3">15.12.4.3. Check Accessibility of Type and Method</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.12.4.4">15.12.4.4. Locate Method to Invoke</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.12.4.5">15.12.4.5. Create Frame, Synchronize, Transfer Control</a></span></dt>
</dl>
</dd>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.13">15.13. Method Reference Expressions</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.13.1">15.13.1. Compile-Time Declaration of a Method Reference</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.13.2">15.13.2. Type of a Method Reference</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.13.3">15.13.3. Run-time Evaluation of Method References</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.14">15.14. Postfix Expressions</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.14.1">15.14.1. Expression Names</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.14.2">15.14.2. Postfix Increment Operator <code class="literal">++</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.14.3">15.14.3. Postfix Decrement Operator <code class="literal">--</code></a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.15">15.15. Unary Operators</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.15.1">15.15.1. Prefix Increment Operator <code class="literal">++</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.15.2">15.15.2. Prefix Decrement Operator <code class="literal">--</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.15.3">15.15.3. Unary Plus Operator <code class="literal">+</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.15.4">15.15.4. Unary Minus Operator <code class="literal">-</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.15.5">15.15.5. Bitwise Complement Operator <code class="literal">~</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.15.6">15.15.6. Logical Complement Operator <code class="literal">!</code></a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.16">15.16. Cast Expressions</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.17">15.17. Multiplicative Operators</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.17.1">15.17.1. Multiplication Operator <code class="literal">*</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.17.2">15.17.2. Division Operator <code class="literal">/</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.17.3">15.17.3. Remainder Operator <code class="literal">%</code></a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.18">15.18. Additive Operators</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.18.1">15.18.1. String Concatenation Operator <code class="literal">+</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.18.2">15.18.2. Additive Operators (<code class="literal">+</code> and <code class="literal">-</code>) for Numeric Types</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.19">15.19. Shift Operators</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.20">15.20. Relational Operators</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.20.1">15.20.1. Numerical Comparison Operators <code class="literal">&lt;</code>,
<code class="literal">&lt;=</code>, <code class="literal">&gt;</code>, and <code class="literal">&gt;=</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.20.2">15.20.2. Type Comparison Operator <code class="literal">instanceof</code></a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.21">15.21. Equality Operators</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.21.1">15.21.1. Numerical Equality Operators <code class="literal">==</code> and <code class="literal">!=</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.21.2">15.21.2. Boolean Equality Operators <code class="literal">==</code> and <code class="literal">!=</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.21.3">15.21.3. Reference Equality Operators <code class="literal">==</code> and <code class="literal">!=</code></a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.22">15.22. Bitwise and Logical Operators</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.22.1">15.22.1. Integer Bitwise Operators <code class="literal">&amp;</code>, <code class="literal">^</code>, and <code class="literal">|</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.22.2">15.22.2. Boolean Logical Operators <code class="literal">&amp;</code>, <code class="literal">^</code>, and <code class="literal">|</code></a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.23">15.23. Conditional-And Operator <code class="literal">&amp;&amp;</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.24">15.24. Conditional-Or Operator <code class="literal">||</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.25">15.25. Conditional Operator <code class="literal">? :</code></a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.25.1">15.25.1. Boolean Conditional Expressions</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.25.2">15.25.2. Numeric Conditional Expressions</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.25.3">15.25.3. Reference Conditional Expressions</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.26">15.26. Assignment Operators</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.26.1">15.26.1. Simple Assignment Operator <code class="literal">=</code></a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.26.2">15.26.2. Compound Assignment Operators</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.27">15.27. Lambda Expressions</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-15.html#jls-15.27.1">15.27.1. Lambda Parameters</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.27.2">15.27.2. Lambda Body</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.27.3">15.27.3. Type of a Lambda Expression</a></span></dt>
<dt><span class="section"><a href="jls-15.html#jls-15.27.4">15.27.4. Run-time Evaluation of Lambda Expressions</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-15.html#jls-15.28">15.28. Constant Expressions</a></span></dt>
</dl>
</div>
<p class="norm"><a name="jls-15-100"></a>Much of the work in a program is
done by evaluating <span class="emphasis"><em>expressions</em></span>, either for their
side effects, such as assignments to variables, or for their values,
which can be used as arguments or operands in larger expressions, or
to affect the execution sequence in statements, or both.
</p>
<p class="norm"><a name="jls-15-110"></a>This chapter specifies the
meanings of expressions and the rules for their evaluation.
</p>
<div class="section" title="15.1.&nbsp;Evaluation, Denotation, and Result">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-15.1"></a>15.1.&nbsp;Evaluation, Denotation, and Result
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.1-100"></a>When an expression in a
program is <span class="emphasis"><em>evaluated</em></span>
(<span class="emphasis"><em>executed</em></span>), the result denotes one of three
things:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-15.1-100-A"></a>
A variable (<a class="xref" href="jls-4.html#jls-4.12" title="4.12.&nbsp;Variables">&sect;4.12</a>) (in C, this would be
called an <span class="emphasis"><em>lvalue</em></span>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.1-100-B"></a>
A value (<a class="xref" href="jls-4.html#jls-4.2" title="4.2.&nbsp;Primitive Types and Values">&sect;4.2</a>,
<a class="xref" href="jls-4.html#jls-4.3" title="4.3.&nbsp;Reference Types and Values">&sect;4.3</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.1-100-C"></a>
Nothing (the expression is said to be void)
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-15.1-110"></a>If an expression denotes a
variable, and a value is required for use in further evaluation, then
the value of that variable is used. In this context, if the expression
denotes a variable or a value, we may speak simply of
the <span class="emphasis"><em>value</em></span> of the expression.
</p>
<p class="norm"><a name="jls-15.1-120"></a>Value set conversion
(<a class="xref" href="jls-5.html#jls-5.1.13" title="5.1.13.&nbsp;Value Set Conversion">&sect;5.1.13</a>) is applied to the result of every
expression that produces a value, including when the value of a
variable of type <code class="literal">float</code> or <code class="literal">double</code> is used.
</p>
<p class="norm"><a name="jls-15.1-130"></a>An expression denotes nothing
if and only if it is a method invocation (<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>)
that invokes a method that does not return a value, that is, a method
declared <code class="literal">void</code> (<a class="xref" href="jls-8.html#jls-8.4" title="8.4.&nbsp;Method Declarations">&sect;8.4</a>). Such an expression can be
used only as an expression statement (<a class="xref" href="jls-14.html#jls-14.8" title="14.8.&nbsp;Expression Statements">&sect;14.8</a>),
because every other context in which an expression can appear requires
the expression to denote something. An expression statement that is a
method invocation may also invoke a method that produces a result; in
this case the value returned by the method is quietly discarded.
</p>
<p class="norm"><a name="jls-15.1-200"></a>Evaluation of an expression
can produce side effects, because expressions may contain embedded
assignments, increment operators, decrement operators, and method
invocations.
</p>
<p class="norm"><a name="jls-15.1-300"></a>An expression occurs in
either:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-15.1-300-A"></a>The declaration of some
(class or interface) type that is being declared: in a field
initializer, in a static initializer, in an instance
initializer, in a constructor declaration, in a method
declaration, or in an annotation.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.1-300-B"></a>An annotation on a
package declaration or on a top level type declaration.
</p>
</li>
</ul>
</div>
</div>
<div class="section" title="15.2.&nbsp;Forms of Expressions">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-15.2"></a>15.2.&nbsp;Forms of Expressions
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.2-100"></a>Expressions can be broadly
categorized into one of the following syntactic forms:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-15.2-100-A"></a>
Expression names (<a class="xref" href="jls-6.html#jls-6.5.6" title="6.5.6.&nbsp;Meaning of Expression Names">&sect;6.5.6</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.2-100-B"></a>
Primary expressions (<a class="xref" href="jls-15.html#jls-15.8" title="15.8.&nbsp;Primary Expressions">&sect;15.8</a> -
<a class="xref" href="jls-15.html#jls-15.13" title="15.13.&nbsp;Method Reference Expressions">&sect;15.13</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.2-100-C"></a>
Unary operator expressions (<a class="xref" href="jls-15.html#jls-15.14" title="15.14.&nbsp;Postfix Expressions">&sect;15.14</a> -
<a class="xref" href="jls-15.html#jls-15.16" title="15.16.&nbsp;Cast Expressions">&sect;15.16</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.2-100-D"></a>
Binary operator expressions (<a class="xref" href="jls-15.html#jls-15.17" title="15.17.&nbsp;Multiplicative Operators">&sect;15.17</a> -
<a class="xref" href="jls-15.html#jls-15.24" title="15.24.&nbsp;Conditional-Or Operator ||">&sect;15.24</a>, and
<a class="xref" href="jls-15.html#jls-15.26" title="15.26.&nbsp;Assignment Operators">&sect;15.26</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.2-100-E"></a>
Ternary operator expressions (<a class="xref" href="jls-15.html#jls-15.25" title="15.25.&nbsp;Conditional Operator ? :">&sect;15.25</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.2-100-F"></a>
Lambda expressions (<a class="xref" href="jls-15.html#jls-15.27" title="15.27.&nbsp;Lambda Expressions">&sect;15.27</a>)
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-15.2-200"></a>Precedence among operators is
managed by a hierarchy of grammar productions. The lowest precedence
operator is the arrow of a lambda expression (<code class="literal">-&gt;</code>), followed by
the assignment operators. Thus, all expressions are syntactically
included in the <span class="emphasis"><em>LambdaExpression</em></span>
and <span class="emphasis"><em>AssignmentExpression</em></span> nonterminals:
</p>
<div id="jls-15.2-210" class="productionset"><a name="jls-15.2-210"></a>
<div class="production"><a name="jls-Expression"></a>
<div class="lhs">Expression:</div>
<div class="rhs">
<a href="jls-15.html#jls-LambdaExpression" title="LambdaExpression">LambdaExpression</a> <br>
<a href="jls-15.html#jls-AssignmentExpression" title="AssignmentExpression">AssignmentExpression</a>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-15.2-300"></a>When some
expressions appear in certain contexts, they are considered
<span class="emphasis"><em>poly expressions</em></span>. The following forms of
expressions may be poly expressions:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-15.2-300-A"></a>
Parenthesized expressions (<a class="xref" href="jls-15.html#jls-15.8.5" title="15.8.5.&nbsp;Parenthesized Expressions">&sect;15.8.5</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.2-300-B"></a>
Class instance creation expressions
(<a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.2-300-C"></a>
Method invocation expressions
(<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.2-300-D"></a>
Method reference expressions
(<a class="xref" href="jls-15.html#jls-15.13" title="15.13.&nbsp;Method Reference Expressions">&sect;15.13</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.2-300-E"></a>
Conditional expressions (<a class="xref" href="jls-15.html#jls-15.25" title="15.25.&nbsp;Conditional Operator ? :">&sect;15.25</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.2-300-F"></a>
Lambda expressions (<a class="xref" href="jls-15.html#jls-15.27" title="15.27.&nbsp;Lambda Expressions">&sect;15.27</a>)
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-15.2-310"></a>The rules determining whether
an expression of one of these forms is a poly expression are given in
the individual sections that specify these forms of
expressions.
</p>
<p class="norm-static"><a name="jls-15.2-320"></a>Expressions
that are not poly expressions are <span class="emphasis"><em>standalone
expressions</em></span>. Standalone expressions are expressions of the
forms above when determined not to be poly expressions, as well as all
expressions of all other forms. Expressions of all other forms are
said to have a <span class="emphasis"><em>standalone form</em></span>.
</p>
<p class="norm-static"><a name="jls-15.2-400"></a>Some
expressions have a value that can be determined at compile time.
These are <span class="emphasis"><em>constant expressions</em></span>
(<a class="xref" href="jls-15.html#jls-15.28" title="15.28.&nbsp;Constant Expressions">&sect;15.28</a>).
</p>
</div>
<div class="section" title="15.3.&nbsp;Type of an Expression">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-15.3"></a>15.3.&nbsp;Type of an Expression
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.3-100"></a>If an expression denotes a variable or
a value, then the expression has a type known at compile time. The
type of a standalone expression can be determined entirely from the
contents of the expression; in contrast, the type of a poly expression
may be influenced by the expression's target type
(<a class="xref" href="jls-5.html" title="Chapter&nbsp;5.&nbsp;Conversions and Contexts">&sect;5 (<i>Conversions and Contexts</i>)</a>). The rules for determining the type of an
expression are explained separately below for each kind of
expression.
</p>
<p class="norm"><a name="jls-15.3-110"></a>The value of an expression is
assignment compatible (<a class="xref" href="jls-5.html#jls-5.2" title="5.2.&nbsp;Assignment Contexts">&sect;5.2</a>) with the type of the
expression, unless heap pollution occurs (<a class="xref" href="jls-4.html#jls-4.12.2" title="4.12.2.&nbsp;Variables of Reference Type">&sect;4.12.2</a>).
</p>
<p class="norm"><a name="jls-15.3-120"></a>Likewise, the value stored in
a variable is always compatible with the type of the variable, unless
heap pollution occurs.
</p>
<p class="norm"><a name="jls-15.3-130"></a>In other words, the value of
an expression whose type is <span class="type">T</span> is always suitable for assignment to a
variable of type <span class="type">T</span>.
</p>
<p class="norm"><a name="jls-15.3-200"></a>Note that an expression whose
type is a class type <span class="type">F</span> that is declared <code class="literal">final</code> is guaranteed to
have a value that is either a null reference or an object whose class
is <span class="type">F</span> itself, because <code class="literal">final</code> types have no subclasses.
</p>
</div>
<div class="section" title="15.4.&nbsp;FP-strict Expressions">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-15.4"></a>15.4.&nbsp;FP-strict Expressions
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.4-100"></a>If the type of an expression
is <code class="literal">float</code> or <code class="literal">double</code>, then there is a question as to what value set
(<a class="xref" href="jls-4.html#jls-4.2.3" title="4.2.3.&nbsp;Floating-Point Types, Formats, and Values">&sect;4.2.3</a>) the value of the expression is drawn
from. This is governed by the rules of value set conversion
(<a class="xref" href="jls-5.html#jls-5.1.13" title="5.1.13.&nbsp;Value Set Conversion">&sect;5.1.13</a>); these rules in turn depend on whether
or not the expression is <span class="emphasis"><em>FP-strict</em></span>.
</p>
<p class="norm"><a name="jls-15.4-110"></a>Every constant expression
(<a class="xref" href="jls-15.html#jls-15.28" title="15.28.&nbsp;Constant Expressions">&sect;15.28</a>) is FP-strict.
</p>
<p class="norm"><a name="jls-15.4-120"></a>If an expression is not a
constant expression, then consider all the class declarations,
interface declarations, and method declarations that contain the
expression. If <span class="emphasis"><em>any</em></span> such declaration bears the
<code class="literal">strictfp</code> modifier (<a class="xref" href="jls-8.html#jls-8.1.1.3" title="8.1.1.3.&nbsp;strictfp Classes">&sect;8.1.1.3</a>,
<a class="xref" href="jls-8.html#jls-8.4.3.5" title="8.4.3.5.&nbsp;strictfp Methods">&sect;8.4.3.5</a>, <a class="xref" href="jls-9.html#jls-9.1.1.2" title="9.1.1.2.&nbsp;strictfp Interfaces">&sect;9.1.1.2</a>), then
the expression is FP-strict.
</p>
<p class="norm"><a name="jls-15.4-130"></a>If a class, interface, or
method, <span class="type">X</span>, is declared <code class="literal">strictfp</code>, then <span class="type">X</span> and any class,
interface, method, constructor, instance initializer, static
initializer, or variable initializer within <span class="type">X</span> is said to
be <span class="emphasis"><em>FP-strict</em></span>.
</p>
<p class="note">Note that an annotation's element value
(<a class="xref" href="jls-9.html#jls-9.7" title="9.7.&nbsp;Annotations">&sect;9.7</a>) is always FP-strict, because it is always
a constant expression.
</p>
<p class="norm"><a name="jls-15.4-200"></a>It follows that an expression
is not FP-strict if and only if it is not a constant
expression <span class="emphasis"><em>and</em></span> it does not appear within any
declaration that has the <code class="literal">strictfp</code> modifier.
</p>
<p class="norm"><a name="jls-15.4-300"></a>Within an FP-strict
expression, all intermediate values must be elements of the float
value set or the double value set, implying that the results of all
FP-strict expressions must be those predicted by IEEE 754 arithmetic
on operands represented using single and double formats.
</p>
<p class="norm"><a name="jls-15.4-310"></a>Within an expression that is
not FP-strict, some leeway is granted for an implementation to use an
extended exponent range to represent intermediate results; the net
effect, roughly speaking, is that a calculation might produce "the
correct answer" in situations where exclusive use of the float value
set or double value set might result in overflow or underflow.
</p>
</div>
<div class="section" title="15.5.&nbsp;Expressions and Run-Time Checks">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-15.5"></a>15.5.&nbsp;Expressions and Run-Time Checks
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.5-100"></a>If the type of an expression
is a primitive type, then the value of the expression is of that same
primitive type.
</p>
<p class="norm"><a name="jls-15.5-110"></a>If the type of an expression
is a reference type, then the class of the referenced object, or even
whether the value is a reference to an object rather than <code class="literal">null</code>, is
not necessarily known at compile time. There are a few places in the
Java programming language where the actual class of a referenced object affects
program execution in a manner that cannot be deduced from the type of
the expression. They are as follows:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-15.5-110-A"></a>
Method invocation (<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>). The particular
method used for an invocation <code class="literal">o.m(...)</code> is
chosen based on the methods that are part of the class or
interface that is the type of <code class="literal">o</code>. For instance
methods, the class of the object referenced by the run-time
value of <code class="literal">o</code> participates because a subclass
may override a specific method already declared in a parent
class so that this overriding method is invoked. (The overriding
method may or may not choose to further invoke the original
overridden <code class="literal">m</code> method.)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.5-110-B"></a>
The <code class="literal">instanceof</code> operator (<a class="xref" href="jls-15.html#jls-15.20.2" title="15.20.2.&nbsp;Type Comparison Operator instanceof">&sect;15.20.2</a>). An
expression whose type is a reference type may be tested using
<code class="literal">instanceof</code> to find out whether the class of the object
referenced by the run-time value of the expression is assignment
compatible (<a class="xref" href="jls-5.html#jls-5.2" title="5.2.&nbsp;Assignment Contexts">&sect;5.2</a>) with some other reference
type.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.5-110-C"></a>
Casting (<a class="xref" href="jls-5.html#jls-5.5" title="5.5.&nbsp;Casting Contexts">&sect;5.5</a>, <a class="xref" href="jls-15.html#jls-15.16" title="15.16.&nbsp;Cast Expressions">&sect;15.16</a>).
The class of the object referenced by the run-time value of the
operand expression might not be compatible with the type
specified by the cast. For reference types, this may require a
run-time check that throws an exception if the class of the
referenced object, as determined at run time, is not assignment
compatible (<a class="xref" href="jls-5.html#jls-5.2" title="5.2.&nbsp;Assignment Contexts">&sect;5.2</a>) with the target
type.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.5-110-D"></a>
Assignment to an array component of reference type
(<a class="xref" href="jls-10.html#jls-10.5" title="10.5.&nbsp;Array Store Exception">&sect;10.5</a>, <a class="xref" href="jls-15.html#jls-15.13" title="15.13.&nbsp;Method Reference Expressions">&sect;15.13</a>,
<a class="xref" href="jls-15.html#jls-15.26.1" title="15.26.1.&nbsp;Simple Assignment Operator =">&sect;15.26.1</a>). The type-checking rules allow
the array type <span class="type">S</span><code class="literal">[]</code> to be treated as a subtype of
<span class="type">T</span><code class="literal">[]</code> if <span class="type">S</span> is a subtype of <span class="type">T</span>, but this requires a
run-time check for assignment to an array component, similar to
the check performed for a cast.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.5-110-E"></a>
Exception handling (<a class="xref" href="jls-14.html#jls-14.20" title="14.20.&nbsp;The try statement">&sect;14.20</a>). An exception
is caught by a <code class="literal">catch</code> clause only if the class of the thrown
exception object is an <code class="literal">instanceof</code> the type of the formal
parameter of the <code class="literal">catch</code> clause.
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-15.5-120"></a>Situations where the class of
an object is not statically known may lead to run-time type
errors.
</p>
<p class="norm"><a name="jls-15.5-200"></a>In addition, there are
situations where the statically known type may not be accurate at run
time. Such situations can arise in a program that gives rise to
compile-time unchecked warnings. Such warnings are given in response
to operations that cannot be statically guaranteed to be safe, and
cannot immediately be subjected to dynamic checking because they
involve non-reifiable types (<a class="xref" href="jls-4.html#jls-4.7" title="4.7.&nbsp;Reifiable Types">&sect;4.7</a>). As a result,
dynamic checks later in the course of program execution may detect
inconsistencies and result in run-time type errors.
</p>
<p class="norm"><a name="jls-15.5-210"></a>A run-time type error can occur
only in these situations:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-15.5-210-A"></a>
In a cast, when the actual class of the object referenced by the
value of the operand expression is not compatible with the
target type specified by the cast operator
(<a class="xref" href="jls-5.html#jls-5.5" title="5.5.&nbsp;Casting Contexts">&sect;5.5</a>, <a class="xref" href="jls-15.html#jls-15.16" title="15.16.&nbsp;Cast Expressions">&sect;15.16</a>); in
this case a <code class="literal">ClassCastException</code> is thrown.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.5-210-B"></a>
In an automatically generated
cast introduced to ensure the validity of an operation on a
non-reifiable type (<a class="xref" href="jls-4.html#jls-4.7" title="4.7.&nbsp;Reifiable Types">&sect;4.7</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.5-210-C"></a>
In an assignment to an array component of reference type, when
the actual class of the object referenced by the value to be
assigned is not compatible with the actual run-time component
type of the array (<a class="xref" href="jls-10.html#jls-10.5" title="10.5.&nbsp;Array Store Exception">&sect;10.5</a>,
<a class="xref" href="jls-15.html#jls-15.13" title="15.13.&nbsp;Method Reference Expressions">&sect;15.13</a>, <a class="xref" href="jls-15.html#jls-15.26.1" title="15.26.1.&nbsp;Simple Assignment Operator =">&sect;15.26.1</a>); in
this case an <code class="literal">ArrayStoreException</code> is thrown.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.5-210-D"></a>
When an exception is not caught by any <code class="literal">catch</code> clause of a <code class="literal">try</code>
statement (<a class="xref" href="jls-14.html#jls-14.20" title="14.20.&nbsp;The try statement">&sect;14.20</a>); in this case the thread
of control that encountered the exception first attempts to invoke an uncaught exception
handler (<a class="xref" href="jls-11.html#jls-11.3" title="11.3.&nbsp;Run-Time Handling of an Exception">&sect;11.3</a>) and then
terminates.
</p>
</li>
</ul>
</div>
</div>
<div class="section" title="15.6.&nbsp;Normal and Abrupt Completion of Evaluation">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-15.6"></a>15.6.&nbsp;Normal and Abrupt Completion of Evaluation
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.6-100"></a>Every expression has a normal
mode of evaluation in which certain computational steps are carried
out. The following sections describe the normal mode of evaluation for
each kind of expression.
</p>
<p class="norm"><a name="jls-15.6-110"></a>If all the steps are carried
out without an exception being thrown, the expression is said
to <span class="emphasis"><em>complete normally</em></span>.
</p>
<p class="norm"><a name="jls-15.6-120"></a>If, however, evaluation of an
expression throws an exception, then the expression is said
to <span class="emphasis"><em>complete abruptly</em></span>. An abrupt completion always
has an associated reason, which is always a <code class="literal">throw</code> with a given
value.
</p>
<p class="norm"><a name="jls-15.6-200"></a>Run-time exceptions are thrown
by the predefined operators as follows:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-15.6-200-A"></a>
A class instance creation expression
(<a class="xref" href="jls-15.html#jls-15.9.4" title="15.9.4.&nbsp;Run-Time Evaluation of Class Instance Creation Expressions">&sect;15.9.4</a>), array creation expression
(<a class="xref" href="jls-15.html#jls-15.10.2" title="15.10.2.&nbsp;Run-Time Evaluation of Array Creation Expressions">&sect;15.10.2</a>), method reference expression
(<a class="xref" href="jls-15.html#jls-15.13.3" title="15.13.3.&nbsp;Run-time Evaluation of Method References">&sect;15.13.3</a>), array initializer expression
(<a class="xref" href="jls-10.html#jls-10.6" title="10.6.&nbsp;Array Initializers">&sect;10.6</a>), string concatenation operator
expression (<a class="xref" href="jls-15.html#jls-15.18.1" title="15.18.1.&nbsp;String Concatenation Operator +">&sect;15.18.1</a>), or lambda expression
(<a class="xref" href="jls-15.html#jls-15.27.4" title="15.27.4.&nbsp;Run-time Evaluation of Lambda Expressions">&sect;15.27.4</a>) throws an <code class="literal">OutOfMemoryError</code> if there is
insufficient memory available.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.6-200-B"></a>
An array creation expression (<a class="xref" href="jls-15.html#jls-15.10.2" title="15.10.2.&nbsp;Run-Time Evaluation of Array Creation Expressions">&sect;15.10.2</a>)
throws a <code class="literal">NegativeArraySizeException</code> if the value of any
dimension expression is less than zero.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.6-200-C"></a>
An array access expression (<a class="xref" href="jls-15.html#jls-15.10.4" title="15.10.4.&nbsp;Run-Time Evaluation of Array Access Expressions">&sect;15.10.4</a>)
throws a <code class="literal">NullPointerException</code> if the value of the array reference expression is
<code class="literal">null</code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.6-200-D"></a>
An array access expression (<a class="xref" href="jls-15.html#jls-15.10.4" title="15.10.4.&nbsp;Run-Time Evaluation of Array Access Expressions">&sect;15.10.4</a>)
throws an <code class="literal">ArrayIndexOutOfBoundsException</code> if the value of the array index expression is
negative or greater than or equal to the <code class="literal">length</code> of the
array.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.6-200-E"></a>
A field access expression (<a class="xref" href="jls-15.html#jls-15.11" title="15.11.&nbsp;Field Access Expressions">&sect;15.11</a>) throws a
<code class="literal">NullPointerException</code> if the value of the object reference expression is
<code class="literal">null</code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.6-200-F"></a>
A method invocation expression (<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>)
that invokes an instance method throws a <code class="literal">NullPointerException</code> if the target
reference is <code class="literal">null</code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.6-200-G"></a>
A cast expression (<a class="xref" href="jls-15.html#jls-15.16" title="15.16.&nbsp;Cast Expressions">&sect;15.16</a>) throws a <code class="literal">ClassCastException</code>
if a cast is found to be impermissible at run time.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.6-200-H"></a>
An integer division (<a class="xref" href="jls-15.html#jls-15.17.2" title="15.17.2.&nbsp;Division Operator /">&sect;15.17.2</a>) or integer
remainder (<a class="xref" href="jls-15.html#jls-15.17.3" title="15.17.3.&nbsp;Remainder Operator %">&sect;15.17.3</a>) operator throws an
<code class="literal">ArithmeticException</code> if the value of the right-hand operand
expression is zero.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.6-200-I"></a>
An assignment to an array component of reference type
(<a class="xref" href="jls-15.html#jls-15.26.1" title="15.26.1.&nbsp;Simple Assignment Operator =">&sect;15.26.1</a>), a method invocation expression
(<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>), or a prefix or postfix increment
(<a class="xref" href="jls-15.html#jls-15.14.2" title="15.14.2.&nbsp;Postfix Increment Operator ++">&sect;15.14.2</a>, <a class="xref" href="jls-15.html#jls-15.15.1" title="15.15.1.&nbsp;Prefix Increment Operator ++">&sect;15.15.1</a>)
or decrement operator (<a class="xref" href="jls-15.html#jls-15.14.3" title="15.14.3.&nbsp;Postfix Decrement Operator --">&sect;15.14.3</a>,
<a class="xref" href="jls-15.html#jls-15.15.2" title="15.15.2.&nbsp;Prefix Decrement Operator --">&sect;15.15.2</a>) may all throw an <code class="literal">OutOfMemoryError</code> as a
result of boxing conversion
(<a class="xref" href="jls-5.html#jls-5.1.7" title="5.1.7.&nbsp;Boxing Conversion">&sect;5.1.7</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.6-200-J"></a>
An assignment to an array component of reference type
(<a class="xref" href="jls-15.html#jls-15.26.1" title="15.26.1.&nbsp;Simple Assignment Operator =">&sect;15.26.1</a>) throws an <code class="literal">ArrayStoreException</code> when the value
to be assigned is not compatible with the component type of the
array (<a class="xref" href="jls-10.html#jls-10.5" title="10.5.&nbsp;Array Store Exception">&sect;10.5</a>).
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-15.6-210"></a>A method invocation expression
can also result in an exception being thrown if an exception occurs
that causes execution of the method body to complete abruptly.
</p>
<p class="norm"><a name="jls-15.6-220"></a>A class instance creation
expression can also result in an exception being thrown if an
exception occurs that causes execution of the constructor to complete
abruptly.
</p>
<p class="norm"><a name="jls-15.6-230"></a>Various linkage and virtual
machine errors may also occur during the evaluation of an
expression. By their nature, such errors are difficult to predict and
difficult to handle.
</p>
<p class="norm"><a name="jls-15.6-300"></a>If an exception occurs, then
evaluation of one or more expressions may be terminated before all
steps of their normal mode of evaluation are complete; such
expressions are said to complete abruptly.
</p>
<p class="norm"><a name="jls-15.6-310"></a>If evaluation of an expression
requires evaluation of a subexpression, then abrupt completion of the
subexpression always causes the immediate abrupt completion of the
expression itself, with the same reason, and all succeeding steps in
the normal mode of evaluation are not performed.
</p>
<p class="norm"><a name="jls-15.6-400"></a>The terms "complete normally"
and "complete abruptly" are also applied to the execution of
statements (<a class="xref" href="jls-14.html#jls-14.1" title="14.1.&nbsp;Normal and Abrupt Completion of Statements">&sect;14.1</a>). A statement may complete
abruptly for a variety of reasons, not just because an exception is
thrown.
</p>
</div>
<div class="section" title="15.7.&nbsp;Evaluation Order">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-15.7"></a>15.7.&nbsp;Evaluation Order
</h2>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-15.7-100"></a>The
Java programming language guarantees that the operands of operators appear to be
evaluated in a specific <span class="emphasis"><em>evaluation order</em></span>, namely,
from left to right.
</p>
<p class="note">It is recommended that code not rely crucially on
this specification. Code is usually clearer when each expression
contains at most one side effect, as its outermost operation, and when
code does not depend on exactly which exception arises as a
consequence of the left-to-right evaluation of expressions.
</p>
<div class="section" title="15.7.1.&nbsp;Evaluate Left-Hand Operand First">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.7.1"></a>15.7.1.&nbsp;Evaluate Left-Hand Operand First
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.7.1-100"></a>The left-hand operand of a
binary operator appears to be fully evaluated before any part of the
right-hand operand is evaluated.
</p>
<p class="norm"><a name="jls-15.7.1-110"></a>If the operator is a
compound-assignment operator (<a class="xref" href="jls-15.html#jls-15.26.2" title="15.26.2.&nbsp;Compound Assignment Operators">&sect;15.26.2</a>), then
evaluation of the left-hand operand includes both remembering the
variable that the left-hand operand denotes and fetching and saving
that variable's value for use in the implied binary
operation.
</p>
<p class="norm"><a name="jls-15.7.1-120"></a>If evaluation of the
left-hand operand of a binary operator completes abruptly, no part of
the right-hand operand appears to have been evaluated.
</p>
<div class="example"><a name="d5e22964"></a><p class="title"><b>Example&nbsp;15.7.1-1.&nbsp;Left-Hand Operand Is Evaluated First</b></p>
<div class="example-contents">
<p class="note">In the following program, the <code class="literal">*</code> operator
has a left-hand operand that contains an assignment to a variable and
a right-hand operand that contains a reference to the same
variable. The value produced by the reference will reflect the fact
that the assignment occurred first.
</p><pre class="programlisting">
class Test1 {
public static void main(String[] args) {
int i = 2;
int j = (i=3) * i;
System.out.println(j);
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
9
</pre><p class="note">It is not permitted for evaluation of the <code class="literal">*</code>
operator to produce <code class="literal">6</code> instead
of <code class="literal">9</code>.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e22975"></a><p class="title"><b>Example&nbsp;15.7.1-2.&nbsp;Implicit Left-Hand Operand In Operator Of Compound Assigment</b></p>
<div class="example-contents">
<p class="note">In the following program, the two assignment
statements both fetch and remember the value of the left-hand operand,
which is <code class="literal">9</code>, before the right-hand operand of the
addition operator is evaluated, at which point the variable is set
to <code class="literal">3</code>.
</p><pre class="programlisting">
class Test2 {
public static void main(String[] args) {
int a = 9;
a += (a = 3); // first example
System.out.println(a);
int b = 9;
b = b + (b = 3); // second example
System.out.println(b);
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
12
12
</pre><p class="note">It is not permitted for either assignment (compound
for <code class="literal">a</code>, simple for <code class="literal">b</code>) to produce
the result <code class="literal">6</code>.
</p>
<p class="note">See also the example in
<a class="xref" href="jls-15.html#jls-15.26.2" title="15.26.2.&nbsp;Compound Assignment Operators">&sect;15.26.2</a>.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e22989"></a><p class="title"><b>Example&nbsp;15.7.1-3.&nbsp;Abrupt Completion of Evaluation of the Left-Hand Operand</b></p>
<div class="example-contents"><pre class="programlisting">
class Test3 {
public static void main(String[] args) {
int j = 1;
try {
int i = forgetIt() / (j = 2);
} catch (Exception e) {
System.out.println(e);
System.out.println("Now j = " + j);
}
}
static int forgetIt() throws Exception {
throw new Exception("I'm outta here!");
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
java.lang.Exception: I'm outta here!
Now j = 1
</pre><p class="note">That is, the left-hand
operand <code class="literal">forgetIt()</code> of the operator <code class="literal">/</code> throws
an exception before the right-hand operand is evaluated and its
embedded assignment of <code class="literal">2</code> to <code class="literal">j</code>
occurs.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="15.7.2.&nbsp;Evaluate Operands before Operation">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.7.2"></a>15.7.2.&nbsp;Evaluate Operands before Operation
</h3>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-15.7.2-100"></a>The
Java programming language guarantees that every operand of an operator (except the
conditional operators <code class="literal">&amp;&amp;</code>, <code class="literal">||</code>, and <code class="literal">? :</code>) appears to be
fully evaluated before any part of the operation itself is
performed.
</p>
<p class="norm-error"><a name="jls-15.7.2-110"></a>If the
binary operator is an integer division <code class="literal">/</code>
(<a class="xref" href="jls-15.html#jls-15.17.2" title="15.17.2.&nbsp;Division Operator /">&sect;15.17.2</a>) or integer remainder <code class="literal">%</code>
(<a class="xref" href="jls-15.html#jls-15.17.3" title="15.17.3.&nbsp;Remainder Operator %">&sect;15.17.3</a>), then its execution may raise an
<code class="literal">ArithmeticException</code>, but this exception is thrown only after both
operands of the binary operator have been evaluated and only if these
evaluations completed normally.
</p>
<div class="example"><a name="d5e23011"></a><p class="title"><b>Example&nbsp;15.7.2-1.&nbsp;Evaluation of Operands Before Operation</b></p>
<div class="example-contents"><pre class="programlisting">
class Test {
public static void main(String[] args) {
int divisor = 0;
try {
int i = 1 / (divisor * loseBig());
} catch (Exception e) {
System.out.println(e);
}
}
static int loseBig() throws Exception {
throw new Exception("Shuffle off to Buffalo!");
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
java.lang.Exception: Shuffle off to Buffalo!
</pre><p class="note">and not:</p><pre class="screen">
java.lang.ArithmeticException: / by zero
</pre><p class="note">since no part of the division operation, including
signaling of a divide-by-zero exception, may appear to occur before
the invocation of <code class="literal">loseBig</code> completes, even though
the implementation may be able to detect or infer that the division
operation would certainly result in a divide-by-zero exception.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="15.7.3.&nbsp;Evaluation Respects Parentheses and Precedence">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.7.3"></a>15.7.3.&nbsp;Evaluation Respects Parentheses and Precedence
</h3>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-15.7.3-100"></a>The
Java programming language respects the order of evaluation indicated explicitly by
parentheses and implicitly by operator precedence.
</p>
<p class="note">An implementation of the Java programming language may not take
advantage of algebraic identities such as the associative law to
rewrite expressions into a more convenient computational order unless
it can be proven that the replacement expression is equivalent in
value and in its observable side effects, even in the presence of
multiple threads of execution (using the thread execution model in
<a class="xref" href="jls-17.html" title="Chapter&nbsp;17.&nbsp;Threads and Locks">&sect;17 (<i>Threads and Locks</i>)</a>), for all possible computational values that
might be involved.
</p>
<p class="norm-dynamic"><a name="jls-15.7.3-110"></a>In the
case of floating-point calculations, this rule applies also for
infinity and not-a-number (NaN) values.
</p>
<p class="note">For example, <code class="literal">!(x&lt;y)</code> may not be
rewritten as <code class="literal">x&gt;=y</code>, because these expressions
have different values if either <code class="literal">x</code>
or <code class="literal">y</code> is NaN or both are NaN.
</p>
<p class="norm-dynamic"><a name="jls-15.7.3-120"></a>Specifically, floating-point calculations that
appear to be mathematically associative are unlikely to be
computationally associative. Such computations must not be naively
reordered.
</p>
<p class="note">For example, it is not correct for a Java compiler
to rewrite <code class="literal">4.0*x*0.5</code> as <code class="literal">2.0*x</code>;
while roundoff happens not to be an issue here, there are large values
of <code class="literal">x</code> for which the first expression produces
infinity (because of overflow) but the second expression produces a
finite result.
</p>
<div class="informalexample">
<p class="note">So, for example, the test program:</p><pre class="programlisting">
strictfp class Test {
public static void main(String[] args) {
double d = 8e+307;
System.out.println(4.0 * d * 0.5);
System.out.println(2.0 * d);
}
}
</pre><p class="note">prints:</p><pre class="screen">
Infinity
1.6e+308
</pre><p class="note">because the first expression overflows and the
second does not.
</p>
</div>
<p class="norm"><a name="jls-15.7.3-130"></a>In contrast, integer
addition and multiplication <span class="emphasis"><em>are</em></span> provably
associative in the Java programming language.
</p>
<p class="note">For example <code class="literal">a+b+c</code>,
where <code class="literal">a</code>, <code class="literal">b</code>,
and <code class="literal">c</code> are local variables (this simplifying
assumption avoids issues involving multiple threads
and <code class="literal">volatile</code> variables), will always produce the
same answer whether evaluated as <code class="literal">(a+b)+c</code>
or <code class="literal">a+(b+c)</code>; if the
expression <code class="literal">b+c</code> occurs nearby in the code, a smart
Java compiler may be able to use this common subexpression.
</p>
</div>
<div class="section" title="15.7.4.&nbsp;Argument Lists are Evaluated Left-to-Right">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.7.4"></a>15.7.4.&nbsp;Argument Lists are Evaluated Left-to-Right
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.7.4-100"></a>In a method or constructor
invocation or class instance creation expression, argument expressions
may appear within the parentheses, separated by commas. Each argument
expression appears to be fully evaluated before any part of any
argument expression to its right.
</p>
<p class="norm"><a name="jls-15.7.4-110"></a>If evaluation of an argument
expression completes abruptly, no part of any argument expression to
its right appears to have been evaluated.
</p>
<div class="example"><a name="d5e23057"></a><p class="title"><b>Example&nbsp;15.7.4-1.&nbsp;Evaluation Order At Method Invocation</b></p>
<div class="example-contents"><pre class="programlisting">
class Test1 {
public static void main(String[] args) {
String s = "going, ";
print3(s, s, s = "gone");
}
static void print3(String a, String b, String c) {
System.out.println(a + b + c);
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
going, going, gone
</pre><p class="note">because the assignment of the string
"<code class="literal">gone</code>" to <code class="literal">s</code> occurs after the
first two arguments to <code class="literal">print3</code> have been
evaluated.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e23066"></a><p class="title"><b>Example&nbsp;15.7.4-2.&nbsp;Abrupt Completion of Argument Expression</b></p>
<div class="example-contents"><pre class="programlisting">
class Test2 {
static int id;
public static void main(String[] args) {
try {
test(id = 1, oops(), id = 3);
} catch (Exception e) {
System.out.println(e + ", id=" + id);
}
}
static int test(int a, int b, int c) {
return a + b + c;
}
static int oops() throws Exception {
throw new Exception("oops");
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
java.lang.Exception: oops, id=1
</pre><p class="note">because the assignment of <code class="literal">3</code>
to <code class="literal">id</code> is not executed.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="15.7.5.&nbsp;Evaluation Order for Other Expressions">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.7.5"></a>15.7.5.&nbsp;Evaluation Order for Other Expressions
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.7.5-100"></a>The order of evaluation for
some expressions is not completely covered by these general rules,
because these expressions may raise exceptional conditions at times
that must be specified. See the detailed explanations of evaluation
order for the following kinds of expressions:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-15.7.5-100-A"></a>
class instance creation expressions (<a class="xref" href="jls-15.html#jls-15.9.4" title="15.9.4.&nbsp;Run-Time Evaluation of Class Instance Creation Expressions">&sect;15.9.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.7.5-100-B"></a>
array creation expressions (<a class="xref" href="jls-15.html#jls-15.10.2" title="15.10.2.&nbsp;Run-Time Evaluation of Array Creation Expressions">&sect;15.10.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.7.5-100-C"></a>
array access expressions (<a class="xref" href="jls-15.html#jls-15.10.4" title="15.10.4.&nbsp;Run-Time Evaluation of Array Access Expressions">&sect;15.10.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.7.5-100-D"></a>
method invocation expressions (<a class="xref" href="jls-15.html#jls-15.12.4" title="15.12.4.&nbsp;Run-Time Evaluation of Method Invocation">&sect;15.12.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.7.5-100-G"></a>
method reference expressions (<a class="xref" href="jls-15.html#jls-15.13.3" title="15.13.3.&nbsp;Run-time Evaluation of Method References">&sect;15.13.3</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.7.5-100-E"></a>
assignments involving array components
(<a class="xref" href="jls-15.html#jls-15.26" title="15.26.&nbsp;Assignment Operators">&sect;15.26</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.7.5-100-F"></a>
lambda expressions (<a class="xref" href="jls-15.html#jls-15.27.4" title="15.27.4.&nbsp;Run-time Evaluation of Lambda Expressions">&sect;15.27.4</a>)
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="section" title="15.8.&nbsp;Primary Expressions">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-15.8"></a>15.8.&nbsp;Primary Expressions
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.8-100"></a>Primary expressions include
most of the simplest kinds of expressions, from which all others are
constructed: literals, object creations, field accesses, method
invocations, method references, and array accesses. A parenthesized
expression is also treated syntactically as a primary
expression.
</p>
<div id="jls-15.8-110" class="productionset"><a name="jls-15.8-110"></a>
<div class="production"><a name="jls-Primary"></a>
<div class="lhs">Primary:</div>
<div class="rhs">
<a href="jls-15.html#jls-PrimaryNoNewArray" title="PrimaryNoNewArray">PrimaryNoNewArray</a> <br>
<a href="jls-15.html#jls-ArrayCreationExpression" title="ArrayCreationExpression">ArrayCreationExpression</a>
</div>
</div>
<div class="production"><a name="jls-PrimaryNoNewArray"></a>
<div class="lhs">PrimaryNoNewArray:</div>
<div class="rhs">
<a href="jls-3.html#jls-Literal" title="Literal">Literal</a> <br>
<a href="jls-6.html#jls-TypeName" title="TypeName">TypeName</a> {<code class="literal">[</code> <code class="literal">]</code>} <code class="literal">.</code> <code class="literal">class</code> <br>
<code class="literal">void</code> <code class="literal">.</code> <code class="literal">class</code> <br>
<code class="literal">this</code> <br>
<a href="jls-6.html#jls-TypeName" title="TypeName">TypeName</a> <code class="literal">.</code> <code class="literal">this</code> <br>
<code class="literal">(</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <code class="literal">)</code> <br>
<a href="jls-15.html#jls-ClassInstanceCreationExpression" title="ClassInstanceCreationExpression">ClassInstanceCreationExpression</a> <br>
<a href="jls-15.html#jls-FieldAccess" title="FieldAccess">FieldAccess</a> <br>
<a href="jls-15.html#jls-ArrayAccess" title="ArrayAccess">ArrayAccess</a> <br>
<a href="jls-15.html#jls-MethodInvocation" title="MethodInvocation">MethodInvocation</a> <br>
<a href="jls-15.html#jls-MethodReference" title="MethodReference">MethodReference</a>
</div>
</div>
</div>
<p class="note">This part of the
grammar of the Java programming language is unusual, in two ways. First, one might
expect simple names, such as names of local variables and method
parameters, to be primary expressions. For technical reasons, names
are grouped together with primary expressions a little later when
postfix expressions are introduced
(<a class="xref" href="jls-15.html#jls-15.14" title="15.14.&nbsp;Postfix Expressions">&sect;15.14</a>).
</p>
<p class="note">The technical
reasons have to do with allowing left-to-right parsing of Java
programs with only one-token lookahead. Consider the
expressions <code class="literal">(z[3])</code>
and <code class="literal">(z[])</code>. The first is a parenthesized array
access (<a class="xref" href="jls-15.html#jls-15.10.3" title="15.10.3.&nbsp;Array Access Expressions">&sect;15.10.3</a>) and the second is the start of a
cast (<a class="xref" href="jls-15.html#jls-15.16" title="15.16.&nbsp;Cast Expressions">&sect;15.16</a>). At the point that the look-ahead
symbol is <code class="literal">[</code>, a left-to-right parse will have reduced
the <code class="literal">z</code> to the nonterminal
<span class="emphasis"><em>Name</em></span>. In the context of a cast we prefer not to
have to reduce the name to a <span class="emphasis"><em>Primary</em></span>, but
if <span class="emphasis"><em>Name</em></span> were one of the alternatives for
<span class="emphasis"><em>Primary</em></span>, then we could not tell whether to do the reduction (that
is, we could not determine whether the current situation would turn
out to be a parenthesized array access or a cast) without looking
ahead two tokens, to the token following the <code class="literal">[</code>. The grammar
presented here avoids the problem by keeping <span class="emphasis"><em>Name</em></span>
and <span class="emphasis"><em>Primary</em></span> separate and allowing either in certain other syntax
rules (those
for <span class="emphasis"><em>ClassInstanceCreationExpression</em></span>,
<span class="emphasis"><em>MethodInvocation</em></span>, <span class="emphasis"><em>ArrayAccess</em></span>,
and <span class="emphasis"><em>PostfixExpression</em></span>, but not
for <span class="emphasis"><em>FieldAccess</em></span> because this uses an identifier directly). This strategy effectively defers
the question of whether a <span class="emphasis"><em>Name</em></span> should be treated
as a <span class="emphasis"><em>Primary</em></span> until more context can be examined.
</p>
<p class="note">The second unusual
feature avoids a potential grammatical ambiguity in the expression
"<code class="literal">new int[3][3]</code>" which in Java always means a single
creation of a multidimensional array, but which, without appropriate
grammatical finesse, might also be interpreted as meaning the same as
"<code class="literal">(new int[3])[3]</code>".
</p>
<p class="note">This ambiguity is
eliminated by splitting the expected definition of <span class="emphasis"><em>Primary</em></span> into
<span class="emphasis"><em>Primary</em></span> and <span class="emphasis"><em>PrimaryNoNewArray</em></span>. (This may be
compared to the splitting of <span class="emphasis"><em>Statement</em></span>
into <span class="emphasis"><em>Statement</em></span>
and <span class="emphasis"><em>StatementNoShortIf</em></span>
(<a class="xref" href="jls-14.html#jls-14.5" title="14.5.&nbsp;Statements">&sect;14.5</a>) to avoid the "dangling else"
problem.)
</p>
<div class="section" title="15.8.1.&nbsp;Lexical Literals">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.8.1"></a>15.8.1.&nbsp;Lexical Literals
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.8.1-100"></a>A literal
(<a class="xref" href="jls-3.html#jls-3.10" title="3.10.&nbsp;Literals">&sect;3.10</a>) denotes a fixed, unchanging value.
</p>
<p class="note">The following production from
<a class="xref" href="jls-3.html#jls-3.10" title="3.10.&nbsp;Literals">&sect;3.10</a> is shown here for convenience:
</p>
<div id="d5e23172" class="productionset"><a name="d5e23172"></a>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">Literal:</div>
<div class="rhs">
<a href="jls-3.html#jls-IntegerLiteral" title="IntegerLiteral">IntegerLiteral</a> <br>
<a href="jls-3.html#jls-FloatingPointLiteral" title="FloatingPointLiteral">FloatingPointLiteral</a> <br>
<a href="jls-3.html#jls-BooleanLiteral" title="BooleanLiteral">BooleanLiteral</a> <br>
<a href="jls-3.html#jls-CharacterLiteral" title="CharacterLiteral">CharacterLiteral</a> <br>
<a href="jls-3.html#jls-StringLiteral" title="StringLiteral">StringLiteral</a> <br>
<a href="jls-3.html#jls-NullLiteral" title="NullLiteral">NullLiteral</a>
</div>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.8.1-200"></a>The type of a literal is
determined as follows:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-15.8.1-200-A"></a>The type of an integer
literal (<a class="xref" href="jls-3.html#jls-3.10.1" title="3.10.1.&nbsp;Integer Literals">&sect;3.10.1</a>) that ends
with <code class="literal">L</code> or <code class="literal">l</code> is <code class="literal">long</code>
(<a class="xref" href="jls-4.html#jls-4.2.1" title="4.2.1.&nbsp;Integral Types and Values">&sect;4.2.1</a>).
</p>
<p class="norm"><a name="jls-15.8.1-200-A.1"></a>The type of any
other integer literal is <code class="literal">int</code>
(<a class="xref" href="jls-4.html#jls-4.2.1" title="4.2.1.&nbsp;Integral Types and Values">&sect;4.2.1</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.8.1-200-B"></a>The type of a
floating-point literal (<a class="xref" href="jls-3.html#jls-3.10.2" title="3.10.2.&nbsp;Floating-Point Literals">&sect;3.10.2</a>) that ends
with <code class="literal">F</code> or <code class="literal">f</code> is <code class="literal">float</code> and
its value must be an element of the float value set
(<a class="xref" href="jls-4.html#jls-4.2.3" title="4.2.3.&nbsp;Floating-Point Types, Formats, and Values">&sect;4.2.3</a>).
</p>
<p class="norm"><a name="jls-15.8.1-200-B.1"></a>The type of any
other floating-point literal is <code class="literal">double</code> and its value must be an
element of the double value set
(<a class="xref" href="jls-4.html#jls-4.2.3" title="4.2.3.&nbsp;Floating-Point Types, Formats, and Values">&sect;4.2.3</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.8.1-200-C"></a>The type of a boolean
literal (<a class="xref" href="jls-3.html#jls-3.10.3" title="3.10.3.&nbsp;Boolean Literals">&sect;3.10.3</a>) is <code class="literal">boolean</code>
(<a class="xref" href="jls-4.html#jls-4.2.5" title="4.2.5.&nbsp;The boolean Type and boolean Values">&sect;4.2.5</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.8.1-200-D"></a>The type of a
character literal (<a class="xref" href="jls-3.html#jls-3.10.4" title="3.10.4.&nbsp;Character Literals">&sect;3.10.4</a>) is <code class="literal">char</code>
(<a class="xref" href="jls-4.html#jls-4.2.1" title="4.2.1.&nbsp;Integral Types and Values">&sect;4.2.1</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.8.1-200-E"></a>The type of a string
literal (<a class="xref" href="jls-3.html#jls-3.10.5" title="3.10.5.&nbsp;String Literals">&sect;3.10.5</a>) is <code class="literal">String</code>
(<a class="xref" href="jls-4.html#jls-4.3.3" title="4.3.3.&nbsp;The Class String">&sect;4.3.3</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.8.1-200-F"></a>The type of the null
literal <code class="literal">null</code> (<a class="xref" href="jls-3.html#jls-3.10.7" title="3.10.7.&nbsp;The Null Literal">&sect;3.10.7</a>) is the null type
(<a class="xref" href="jls-4.html#jls-4.1" title="4.1.&nbsp;The Kinds of Types and Values">&sect;4.1</a>); its value is the null
reference.
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-15.8.1-210"></a>Evaluation of a lexical
literal always completes normally.
</p>
</div>
<div class="section" title="15.8.2.&nbsp;Class Literals">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.8.2"></a>15.8.2.&nbsp;Class Literals
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-15.8.2-100"></a>A <span class="emphasis"><em>class
literal</em></span> is an expression consisting of the name of a class,
interface, array, or primitive type, or the pseudo-type <code class="literal">void</code>,
followed by a '<code class="literal">.</code>' and the token <code class="literal">class</code>.
</p>
<p class="norm-static"><a name="jls-15.8.2-110"></a>The type
of <span class="type">C</span><code class="literal">.</code><code class="literal">class</code>, where <span class="type">C</span> is the name of a class, interface, or
array type (<a class="xref" href="jls-4.html#jls-4.3" title="4.3.&nbsp;Reference Types and Values">&sect;4.3</a>), is
<code class="literal">Class</code><code class="literal">&lt;</code><span class="type">C</span><code class="literal">&gt;</code>.
</p>
<p class="norm-static"><a name="jls-15.8.2-120"></a>The type
of <span class="type">p</span><code class="literal">.</code><code class="literal">class</code>, where <span class="type">p</span> is the name of a
primitive type (<a class="xref" href="jls-4.html#jls-4.2" title="4.2.&nbsp;Primitive Types and Values">&sect;4.2</a>), is
<code class="literal">Class</code><code class="literal">&lt;</code><span class="type">B</span><code class="literal">&gt;</code>, where <span class="type">B</span> is the
type of an expression of type <span class="type">p</span> after boxing conversion
(<a class="xref" href="jls-5.html#jls-5.1.7" title="5.1.7.&nbsp;Boxing Conversion">&sect;5.1.7</a>).
</p>
<p class="norm"><a name="jls-15.8.2-130"></a>The type of
<code class="literal">void</code><code class="literal">.</code><code class="literal">class</code> (<a class="xref" href="jls-8.html#jls-8.4.5" title="8.4.5.&nbsp;Method Result">&sect;8.4.5</a>) is
<code class="literal">Class</code><code class="literal">&lt;</code><code class="literal">Void</code><code class="literal">&gt;</code>.
</p>
<p class="norm-error"><a name="jls-15.8.2-200"></a>It is a
compile-time error if the named type is a type variable
(<a class="xref" href="jls-4.html#jls-4.4" title="4.4.&nbsp;Type Variables">&sect;4.4</a>) or a parameterized type
(<a class="xref" href="jls-4.html#jls-4.5" title="4.5.&nbsp;Parameterized Types">&sect;4.5</a>) or an array whose element type is a type
variable or parameterized type.
</p>
<p class="norm-error"><a name="jls-15.8.2-210"></a>It is a
compile-time error if the named type does not denote a type that is
accessible (<a class="xref" href="jls-6.html#jls-6.6" title="6.6.&nbsp;Access Control">&sect;6.6</a>) and in scope
(<a class="xref" href="jls-6.html#jls-6.3" title="6.3.&nbsp;Scope of a Declaration">&sect;6.3</a>) at the point where the class literal
appears.
</p>
<p class="norm-dynamic"><a name="jls-15.8.2-300"></a>A class
literal evaluates to the <code class="literal">Class</code> object for the named type (or for
<code class="literal">void</code>) as defined by the defining class loader
(<a class="xref" href="jls-12.html#jls-12.2" title="12.2.&nbsp;Loading of Classes and Interfaces">&sect;12.2</a>) of the class of the current
instance.
</p>
</div>
<div class="section" title="15.8.3.&nbsp;this">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.8.3"></a>15.8.3.&nbsp;<code class="literal">this</code></h3>
</div>
</div>
</div>
<p class="norm-error"><a name="jls-15.8.3-100"></a>The keyword
<code class="literal">this</code> may be used only in the body of an instance method or default
method, or in the body of a constructor of a class, or in an instance
initializer of a class, or in the initializer of an instance variable
of a class. If it appears anywhere else, a compile-time error
occurs.
</p>
<p class="norm-error"><a name="jls-15.8.3-110"></a>The keyword <code class="literal">this</code>
may be used in a lambda expression only if it is allowed in the
context in which the lambda expression appears. Otherwise, a
compile-time error occurs.
</p>
<p class="norm-dynamic"><a name="jls-15.8.3-200"></a>When used as a
primary expression, the keyword <code class="literal">this</code> denotes a value that is a
reference to the object for which the instance method or default
method was invoked (<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>), or to the object
being constructed. The value denoted by <code class="literal">this</code> in a lambda body is the
same as the value denoted by <code class="literal">this</code> in the surrounding context.
</p>
<p class="note">The keyword <code class="literal">this</code> is also used in explicit
constructor invocation statements (<a class="xref" href="jls-8.html#jls-8.8.7.1" title="8.8.7.1.&nbsp;Explicit Constructor Invocations">&sect;8.8.7.1</a>).
</p>
<p class="norm-static"><a name="jls-15.8.3-300"></a>The type of
<code class="literal">this</code> is the class or interface type <span class="type">T</span> within which the keyword
<code class="literal">this</code> occurs.
</p>
<p class="note">Default methods provide the unique ability to access
<code class="literal">this</code> inside an interface. (All other interface methods are either
<code class="literal">abstract</code> or <code class="literal">static</code>, so provide no access to <code class="literal">this</code>.) As a result,
it is possible for <code class="literal">this</code> to have an interface type.
</p>
<p class="norm-dynamic"><a name="jls-15.8.3-400"></a>At run time, the
class of the actual object referred to may be <span class="type">T</span>, if <span class="type">T</span> is a class
type, or a class that is a subtype of <span class="type">T</span>.
</p>
<div class="example"><a name="d5e23295"></a><p class="title"><b>Example&nbsp;15.8.3-1.&nbsp;The <code class="literal">this</code> Expression</b></p>
<div class="example-contents"><pre class="programlisting">
class IntVector {
int[] v;
boolean equals(IntVector other) {
if (this == other)
return true;
if (v.length != other.v.length)
return false;
for (int i = 0; i &lt; v.length; i++) {
if (v[i] != other.v[i]) return false;
}
return true;
}
}
</pre><p class="note">Here, the class <code class="literal">IntVector</code>
implements a method <code class="literal">equals</code>, which compares two
vectors. If the other vector is the same vector object as the one for
which the <code class="literal">equals</code> method was invoked, then the check
can skip the length and value
comparisons. The <code class="literal">equals</code> method implements this
check by comparing the reference to the other object to <code class="literal">this</code>.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="15.8.4.&nbsp;Qualified this">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.8.4"></a>15.8.4.&nbsp;Qualified <code class="literal">this</code></h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-15.8.4-100"></a>Any
lexically enclosing instance (<a class="xref" href="jls-8.html#jls-8.1.3" title="8.1.3.&nbsp;Inner Classes and Enclosing Instances">&sect;8.1.3</a>) can be
referred to by explicitly qualifying the keyword <code class="literal">this</code>.
</p>
<p class="norm-static"><a name="jls-15.8.4-110"></a>Let <span class="type">T</span> be the
type denoted by <span class="emphasis"><em>TypeName</em></span>. Let <span class="emphasis"><em>n</em></span> be an
integer such that <span class="type">T</span> is the <span class="emphasis"><em>n</em></span>'th lexically enclosing type
declaration of the class or interface in which the qualified <code class="literal">this</code>
expression appears.
</p>
<p class="norm-static"><a name="jls-15.8.4-120"></a>The value of an
expression of the form <span class="emphasis"><em>TypeName</em></span><code class="literal">.</code><code class="literal">this</code> is the
<span class="emphasis"><em>n</em></span>'th lexically enclosing instance of <code class="literal">this</code>.
</p>
<p class="norm-static"><a name="jls-15.8.4-200"></a>The type of the
expression is <span class="type">T</span>.
</p>
<p class="norm-error"><a name="jls-15.8.4-210"></a>It is a
compile-time error if the expression occurs in a class or interface
which is not an inner class of class <span class="type">T</span> or <span class="type">T</span> itself.
</p>
</div>
<div class="section" title="15.8.5.&nbsp;Parenthesized Expressions">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.8.5"></a>15.8.5.&nbsp;Parenthesized Expressions
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-15.8.5-100"></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. If the contained expression denotes
a variable then the parenthesized expression also denotes that
variable.
</p>
<p class="norm-static"><a name="jls-15.8.5-110"></a>The use
of parentheses affects only the <span class="emphasis"><em>order</em></span> of
evaluation, except for a corner case
whereby <code class="literal">(-2147483648)</code>
and <code class="literal">(-9223372036854775808L)</code> are legal
but <code class="literal">-(2147483648)</code>
and <code class="literal">-(9223372036854775808L)</code> are illegal.
</p>
<p class="note">This is because the decimal literals
<code class="literal">2147483648</code>
and <code class="literal">9223372036854775808L</code> are allowed only as an
operand of the unary minus operator
(<a class="xref" href="jls-3.html#jls-3.10.1" title="3.10.1.&nbsp;Integer Literals">&sect;3.10.1</a>).
</p>
<p class="norm-static"><a name="jls-15.8.5-120"></a>In
particular, the presence or absence of parentheses around an
expression does not (except for the case noted above) affect in any
way:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-15.8.5-120-A"></a>the choice of value
set (<a class="xref" href="jls-4.html#jls-4.2.3" title="4.2.3.&nbsp;Floating-Point Types, Formats, and Values">&sect;4.2.3</a>) for the value of an expression
of type <code class="literal">float</code> or <code class="literal">double</code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.8.5-120-B"></a>whether a variable is
definitely assigned, definitely assigned when <code class="literal">true</code>, definitely
assigned when <code class="literal">false</code>, definitely unassigned, definitely
unassigned when <code class="literal">true</code>, or definitely unassigned when <code class="literal">false</code>
(<a class="xref" href="jls-16.html" title="Chapter&nbsp;16.&nbsp;Definite Assignment">&sect;16 (<i>Definite Assignment</i>)</a>).
</p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-15.8.5-200"></a>If a parenthesized
expression appears in a context of a particular kind with target type
<span class="type">T</span> (<a class="xref" href="jls-5.html" title="Chapter&nbsp;5.&nbsp;Conversions and Contexts">&sect;5 (<i>Conversions and Contexts</i>)</a>), its contained expression similarly
appears in a context of the same kind with target type <span class="type">T</span>.
</p>
<p class="norm-static"><a name="jls-15.8.5-210"></a>If the contained
expression is a poly expression (<a class="xref" href="jls-15.html#jls-15.2" title="15.2.&nbsp;Forms of Expressions">&sect;15.2</a>), the
parenthesized expression is also a poly expression. Otherwise, it is a
standalone expression.
</p>
</div>
</div>
<div class="section" title="15.9.&nbsp;Class Instance Creation Expressions">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-15.9"></a>15.9.&nbsp;Class Instance Creation
Expressions
</h2>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-15.9-100"></a>A class
instance creation expression is used to create new objects that are
instances of classes.
</p>
<div id="jls-15.9-110" class="productionset"><a name="jls-15.9-110"></a>
<div class="production"><a name="jls-ClassInstanceCreationExpression"></a>
<div class="lhs">ClassInstanceCreationExpression:</div>
<div class="rhs">
<code class="literal">new</code> [<a href="jls-4.html#jls-TypeArguments" title="TypeArguments">TypeArguments</a>]
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>} <a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
[<a href="jls-15.html#jls-TypeArgumentsOrDiamond" title="TypeArgumentsOrDiamond">TypeArgumentsOrDiamond</a>]
<code class="literal">(</code> [<a href="jls-15.html#jls-ArgumentList" title="ArgumentList">ArgumentList</a>] <code class="literal">)</code> [<a href="jls-8.html#jls-ClassBody" title="ClassBody">ClassBody</a>] <br>
<a href="jls-6.html#jls-ExpressionName" title="ExpressionName">ExpressionName</a> <code class="literal">.</code> <code class="literal">new</code> [<a href="jls-4.html#jls-TypeArguments" title="TypeArguments">TypeArguments</a>]
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>} <a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
[<a href="jls-15.html#jls-TypeArgumentsOrDiamond" title="TypeArgumentsOrDiamond">TypeArgumentsOrDiamond</a>]
<code class="literal">(</code> [<a href="jls-15.html#jls-ArgumentList" title="ArgumentList">ArgumentList</a>] <code class="literal">)</code> [<a href="jls-8.html#jls-ClassBody" title="ClassBody">ClassBody</a>] <br>
<a href="jls-15.html#jls-Primary" title="Primary">Primary</a> <code class="literal">.</code> <code class="literal">new</code> [<a href="jls-4.html#jls-TypeArguments" title="TypeArguments">TypeArguments</a>]
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>} <a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
[<a href="jls-15.html#jls-TypeArgumentsOrDiamond" title="TypeArgumentsOrDiamond">TypeArgumentsOrDiamond</a>]
<code class="literal">(</code> [<a href="jls-15.html#jls-ArgumentList" title="ArgumentList">ArgumentList</a>] <code class="literal">)</code> [<a href="jls-8.html#jls-ClassBody" title="ClassBody">ClassBody</a>]
</div>
</div>
<div class="production"><a name="jls-TypeArgumentsOrDiamond"></a>
<div class="lhs">TypeArgumentsOrDiamond:</div>
<div class="rhs">
<a href="jls-4.html#jls-TypeArguments" title="TypeArguments">TypeArguments</a> <br>
<code class="literal">&lt;&gt;</code>
</div>
</div>
</div>
<p class="note">The following production from
<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a> is shown here for convenience:
</p>
<div id="d5e23410" class="productionset"><a name="d5e23410"></a>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">ArgumentList:</div>
<div class="rhs">
<a href="jls-15.html#jls-Expression" title="Expression">Expression</a> {<code class="literal">,</code> <a href="jls-15.html#jls-Expression" title="Expression">Expression</a>}
</div>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-15.9-200"></a>A class instance
creation expression specifies a class to be instantiated, possibly
followed by type arguments (<a class="xref" href="jls-4.html#jls-4.5.1" title="4.5.1.&nbsp;Type Arguments of Parameterized Types">&sect;4.5.1</a>) or
a <span class="emphasis"><em>diamond</em></span> (<code class="literal">&lt;&gt;</code>) if the class being
instantiated is generic (<a class="xref" href="jls-8.html#jls-8.1.2" title="8.1.2.&nbsp;Generic Classes and Type Parameters">&sect;8.1.2</a>), followed by (a
possibly empty) list of actual value arguments to the
constructor.
</p>
<p class="norm-static"><a name="jls-15.9-210"></a>If the type
argument list to the class is empty &#8212; the diamond form <code class="literal">&lt;&gt;</code> &#8212;
the type arguments of the class are inferred. It is legal, though
strongly discouraged as a matter of style, to have white space between
the "<code class="literal">&lt;</code>" and "<code class="literal">&gt;</code>" of a diamond.
</p>
<p class="norm-static"><a name="jls-15.9-220"></a>If the
constructor is generic (<a class="xref" href="jls-8.html#jls-8.8.4" title="8.8.4.&nbsp;Generic Constructors">&sect;8.8.4</a>), the type
arguments to the constructor may similarly either be inferred or
passed explicitly. If passed explicitly, the type arguments to the
constructor immediately follow the keyword <code class="literal">new</code>.
</p>
<p class="norm-error"><a name="jls-15.9-300"></a>It is a
compile-time error if a class instance creation expression provides
type arguments to a constructor but uses the diamond form for type
arguments to the class.
</p>
<p class="note">This rule is introduced because inference of a
generic class's type arguments may influence the constraints on a
generic constructor's type arguments.
</p>
<p class="norm-error"><a name="jls-15.9-310"></a>If
<span class="emphasis"><em>TypeArguments</em></span> is present between <code class="literal">new</code> and
<span class="emphasis"><em>Identifier</em></span>, or between <span class="emphasis"><em>Identifier</em></span> and <code class="literal">(</code>, then it is a
compile-time error if any of the type arguments are wildcards
(<a class="xref" href="jls-4.html#jls-4.5.1" title="4.5.1.&nbsp;Type Arguments of Parameterized Types">&sect;4.5.1</a>).
</p>
<p class="norm-static"><a name="jls-15.9-320"></a>The
exception types that a class instance creation expression can throw
are specified in <a class="xref" href="jls-11.html#jls-11.2.1" title="11.2.1.&nbsp;Exception Analysis of Expressions">&sect;11.2.1</a>.
</p>
<p class="norm"><a name="jls-15.9-400"></a>Class instance creation
expressions have two forms:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-15.9-400-A"></a>
<span class="emphasis"><em>Unqualified class instance creation
expressions</em></span> begin with the keyword <code class="literal">new</code>.
</p>
<p class="norm"><a name="jls-15.9-400-A.1"></a>
An unqualified class instance creation expression may be used to
create an instance of a class, regardless of whether the class
is a top level (<a class="xref" href="jls-7.html#jls-7.6" title="7.6.&nbsp;Top Level Type Declarations">&sect;7.6</a>), member
(<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>, <a class="xref" href="jls-9.html#jls-9.5" title="9.5.&nbsp;Member Type Declarations">&sect;9.5</a>), local
(<a class="xref" href="jls-14.html#jls-14.3" title="14.3.&nbsp;Local Class Declarations">&sect;14.3</a>), or anonymous class
(<a class="xref" href="jls-15.html#jls-15.9.5" title="15.9.5.&nbsp;Anonymous Class Declarations">&sect;15.9.5</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.9-400-B"></a>
<span class="emphasis"><em>Qualified class instance creation
expressions</em></span> begin with a <span class="emphasis"><em>Primary</em></span> expression or an
<span class="emphasis"><em>ExpressionName</em></span>.
</p>
<p class="norm"><a name="jls-15.9-400-B.1"></a>
A qualified class instance creation expression enables the
creation of instances of inner member classes and their
anonymous subclasses.
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-15.9-410"></a>Both unqualified and qualified
class instance creation expressions may optionally end with a class
body. Such a class instance creation expression declares
an <span class="emphasis"><em>anonymous class</em></span> (<a class="xref" href="jls-15.html#jls-15.9.5" title="15.9.5.&nbsp;Anonymous Class Declarations">&sect;15.9.5</a>)
and creates an instance of it.
</p>
<p class="norm-static"><a name="jls-15.9-500"></a>A class instance
creation expression is a poly expression (<a class="xref" href="jls-15.html#jls-15.2" title="15.2.&nbsp;Forms of Expressions">&sect;15.2</a>)
if it uses the diamond form for type arguments to the class, and it
appears in an assignment context or an invocation context
(<a class="xref" href="jls-5.html#jls-5.2" title="5.2.&nbsp;Assignment Contexts">&sect;5.2</a>, <a class="xref" href="jls-5.html#jls-5.3" title="5.3.&nbsp;Invocation Contexts">&sect;5.3</a>). Otherwise, it
is a standalone expression.
</p>
<p class="norm"><a name="jls-15.9-600"></a>We say that a class
is <span class="emphasis"><em>instantiated</em></span> when an instance of the class is
created by a class instance creation expression. Class instantiation
involves determining the class to be instantiated
(<a class="xref" href="jls-15.html#jls-15.9.1" title="15.9.1.&nbsp;Determining the Class being Instantiated">&sect;15.9.1</a>), the enclosing instances (if any) of
the newly created instance (<a class="xref" href="jls-15.html#jls-15.9.2" title="15.9.2.&nbsp;Determining Enclosing Instances">&sect;15.9.2</a>), and the
constructor to be invoked to create the new instance
(<a class="xref" href="jls-15.html#jls-15.9.3" title="15.9.3.&nbsp;Choosing the Constructor and its Arguments">&sect;15.9.3</a>).
</p>
<div class="section" title="15.9.1.&nbsp;Determining the Class being Instantiated">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.9.1"></a>15.9.1.&nbsp;Determining the Class being Instantiated
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-15.9.1-100"></a>If the
class instance creation expression ends in a class body, then the
class being instantiated is an anonymous class. Then:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-error"><a name="jls-15.9.1-100-A"></a>
If the class instance creation expression is unqualified:
</p>
<p class="norm-error"><a name="jls-15.9.1-100-A.1"></a>
The <span class="emphasis"><em>Identifier</em></span> after the <code class="literal">new</code> token must denote a class that
is accessible, non-<code class="literal">final</code>, and not an enum type; or denote an
interface that is accessible. Otherwise a compile-time error
occurs.
</p>
<p class="norm-error"><a name="jls-15.9.1-100-A.2"></a>
If <span class="emphasis"><em>TypeArgumentsOrDiamond</em></span> is present after the <span class="emphasis"><em>Identifier</em></span>,
and denotes <code class="literal">&lt;&gt;</code>, then a compile-time error occurs
</p>
<p class="norm-static"><a name="jls-15.9.1-100-A.3"></a>
Let <span class="type">T</span> be the type denoted by <span class="emphasis"><em>Identifier</em></span>
<span class="emphasis"><em>[TypeArgumentsOrDiamond]</em></span> after the <code class="literal">new</code> token. If
<span class="emphasis"><em>TypeArguments</em></span> is present after the <span class="emphasis"><em>Identifier</em></span>, then <span class="type">T</span> must
denote a well-formed parameterized type
(<a class="xref" href="jls-4.html#jls-4.5" title="4.5.&nbsp;Parameterized Types">&sect;4.5</a>), or a compile-time error
occurs.
</p>
<p class="norm-error"><a name="jls-15.9.1-100-A.4"></a>
If <span class="type">T</span> denotes a class, then an anonymous direct subclass of <span class="type">T</span>
is declared. If <span class="type">T</span> denotes an interface, then an anonymous
direct subclass of <code class="literal">Object</code> that implements <span class="type">T</span> is
declared. In either case, the body of the subclass is
the <span class="emphasis"><em>ClassBody</em></span> given in the class instance
creation expression.
</p>
<p class="norm-static"><a name="jls-15.9.1-100-A.5"></a>
The class being instantiated is the anonymous subclass.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.1-100-B"></a>
If the class instance creation expression is qualified:
</p>
<p class="norm-error"><a name="jls-15.9.1-100-B.1"></a>
The <span class="emphasis"><em>Identifier</em></span> after the <code class="literal">new</code> token must unambiguously denote
an inner class that is accessible, non-<code class="literal">final</code>, not an enum
type, and a member of the compile-time type of the <span class="emphasis"><em>Primary</em></span>
expression or the <span class="emphasis"><em>ExpressionName</em></span>. Otherwise, a compile-time
error occurs.
</p>
<p class="norm-error"><a name="jls-15.9.1-100-B.2"></a>
If <span class="emphasis"><em>TypeArgumentsOrDiamond</em></span> is present after the <span class="emphasis"><em>Identifier</em></span>,
and denotes <code class="literal">&lt;&gt;</code>, then a compile-time error occurs.
</p>
<p class="norm-static"><a name="jls-15.9.100-B.3"></a>
Let <span class="type">T</span> be the type denoted by <span class="emphasis"><em>Identifier</em></span>
<span class="emphasis"><em>[TypeArgumentsOrDiamond]</em></span> after the <code class="literal">new</code> token. If
<span class="emphasis"><em>TypeArguments</em></span> is present after the <span class="emphasis"><em>Identifier</em></span>, then <span class="type">T</span> must
denote a well-formed parameterized type, or a compile-time error
occurs.
</p>
<p class="norm-static"><a name="jls-15.9.1-100-B.5"></a>
An anonymous direct subclass of <span class="type">T</span> is declared. The body of the
subclass is the <span class="emphasis"><em>ClassBody</em></span> given in the
class instance creation expression.
</p>
<p class="norm-static"><a name="jls-15.9.1-100-B.6"></a>
The class being instantiated is the anonymous subclass.
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-15.9.1-200"></a>If a class instance creation
expression does not declare an anonymous class, then:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-error"><a name="jls-15.9.1-200-A"></a>
If the class instance creation expression is unqualified:
</p>
<p class="norm-error"><a name="jls-15.9.1-200-A.1"></a>
The <span class="emphasis"><em>Identifier</em></span> after the <code class="literal">new</code> token must denote a class that
is accessible, non-<code class="literal">abstract</code>, and not an enum type. Otherwise,
a compile-time error occurs.
</p>
<p class="norm-error"><a name="jls-15.9.1-200-A.2"></a>
It <span class="emphasis"><em>TypeArgumentsOrDiamond</em></span> is present after the <span class="emphasis"><em>Identifier</em></span>,
and denotes <code class="literal">&lt;&gt;</code>, and the class denoted by <span class="emphasis"><em>Identifier</em></span> is
not generic, then a compile-time error occurs.
</p>
<p class="norm-static"><a name="jls-15.9.200-A.3"></a>
Let <span class="type">T</span> be the type denoted by <span class="emphasis"><em>Identifier</em></span>
<span class="emphasis"><em>[TypeArgumentsOrDiamond]</em></span> after the <code class="literal">new</code> token. If
<span class="emphasis"><em>TypeArguments</em></span> is present after the <span class="emphasis"><em>Identifier</em></span>, then <span class="type">T</span> must
denote a well-formed parameterized type, or a compile-time error
occurs.
</p>
<p class="norm-static"><a name="jls-15.9.1-200-A.4"></a>
The class being instantiated is the class denoted by
<span class="emphasis"><em>Identifier</em></span>.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.1-200-B"></a>
If the class instance creation expression is qualified:
</p>
<p class="norm-error"><a name="jls-15.9.1-200-B.1"></a>
The <span class="emphasis"><em>Identifier</em></span> after the <code class="literal">new</code> token must unambiguously denote
an inner class that is accessible, non-<code class="literal">abstract</code>, not an enum
type, and a member of the compile-time type of the <span class="emphasis"><em>Primary</em></span>
expression or the <span class="emphasis"><em>ExpressionName</em></span>.
</p>
<p class="norm-error"><a name="jls-15.9.1-200-B.2"></a>
If <span class="emphasis"><em>TypeArgumentsOrDiamond</em></span> is present after the <span class="emphasis"><em>Identifier</em></span>,
and denotes <code class="literal">&lt;&gt;</code>, and the class denoted by <span class="emphasis"><em>Identifier</em></span> is
not generic, then a compile-time error occurs.
</p>
<p class="norm-static"><a name="jls-15.9.200-B.3"></a>
Let <span class="type">T</span> be the type denoted by <span class="emphasis"><em>Identifier</em></span>
<span class="emphasis"><em>[TypeArgumentsOrDiamond]</em></span> after the <code class="literal">new</code> token. If
<span class="emphasis"><em>TypeArguments</em></span> is present after the <span class="emphasis"><em>Identifier</em></span>, then <span class="type">T</span> must
denote a well-formed parameterized type, or a compile-time error
occurs.
</p>
<p class="norm-static"><a name="jls-15.9.1-200-B.4"></a>
The class being instantiated is the class denoted by
<span class="emphasis"><em>Identifier</em></span>.
</p>
</li>
</ul>
</div>
</div>
<div class="section" title="15.9.2.&nbsp;Determining Enclosing Instances">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.9.2"></a>15.9.2.&nbsp;Determining Enclosing Instances
</h3>
</div>
</div>
</div>
<p class="norm-dynamic"><a name="jls-15.9.2-100"></a>Let <span class="type">C</span>
be the class being instantiated, and let <code class="varname">i</code> be the instance being
created. If <span class="type">C</span> is an inner class, then <code class="varname">i</code> may have
an <span class="emphasis"><em>immediately enclosing instance</em></span>
(<a class="xref" href="jls-8.html#jls-8.1.3" title="8.1.3.&nbsp;Inner Classes and Enclosing Instances">&sect;8.1.3</a>), determined as follows:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-100-A"></a>
If <span class="type">C</span> is an anonymous class, then:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-100-A-A"></a>
If the class instance creation expression occurs in a static
context, then <code class="varname">i</code> has no immediately enclosing
instance.
</p>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-100-A-B"></a>
Otherwise, the immediately enclosing instance of <code class="varname">i</code> is
<code class="literal">this</code>.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-100-B"></a>
If <span class="type">C</span> is a local class, then:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-100-B-A"></a>
If <span class="type">C</span> occurs in a static context, then <code class="varname">i</code> has no
immediately enclosing instance.
</p>
</li>
<li class="listitem">
<p class="norm-error"><a name="jls-15.9.2-100-B-B"></a>
Otherwise, if the class instance creation expression occurs
in a static context, then a compile-time error
occurs.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.2-100-B-C"></a>
Otherwise, let <span class="type">O</span> be the immediately enclosing class of
<span class="type">C</span>. Let <span class="emphasis"><em>n</em></span> be an integer such that <span class="type">O</span> is the <span class="emphasis"><em>n</em></span>'th
lexically enclosing type declaration of the class in which
the class instance creation expression appears.
</p>
<p class="norm-dynamic"><a name="jls-15.9.2-100-B-C.1"></a>
The immediately enclosing instance of <code class="varname">i</code> is the <span class="emphasis"><em>n</em></span>'th
lexically enclosing instance of <code class="literal">this</code>.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-100-C"></a>
If <span class="type">C</span> is an inner member class, then:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-100-C-A"></a>
If the class instance creation expression is unqualified,
then:
</p>
<div class="norm">
<ul class="norm" type="square">
<li class="listitem">
<p class="norm-error"><a name="jls-15.9.2-100-C-A-A"></a>
If the class instance creation expression occurs in a
static context, then a compile-time error occurs.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.2-100-C-A-B"></a>
Otherwise, if <span class="type">C</span> is a member of a class enclosing the
class in which the class instance creation expression
appears, then let <span class="type">O</span> be the immediately enclosing class
of which <span class="type">C</span> is a member. Let <span class="emphasis"><em>n</em></span> be an integer such
that <span class="type">O</span> is the <span class="emphasis"><em>n</em></span>'th lexically enclosing type
declaration of the class in which the class instance
creation expression appears.
</p>
<p class="norm-dynamic"><a name="jls-15.9.2-100-C-A-B.1"></a>
The immediately enclosing instance of <code class="varname">i</code> is the
<span class="emphasis"><em>n</em></span>'th lexically enclosing instance of <code class="literal">this</code>.
</p>
</li>
<li class="listitem">
<p class="norm-error"><a name="jls-15.9.2-100-C-A-C"></a>
Otherwise, a compile-time error occurs.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-100-C-B"></a>
If the class instance creation expression is qualified, then
the immediately enclosing instance of <code class="varname">i</code> is the object that
is the value of the <span class="emphasis"><em>Primary</em></span> expression or the
<span class="emphasis"><em>ExpressionName</em></span>.
</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<p class="norm-dynamic"><a name="jls-15.9.2-200"></a>If <span class="type">C</span>
is an anonymous class, and its direct superclass <span class="type">S</span> is an inner
class, then <code class="varname">i</code> may have an <span class="emphasis"><em>immediately enclosing instance
with respect to <span class="type">S</span></em></span>, determined as follows:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-200-A"></a>
If <span class="type">S</span> is a local class, then:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-200-A-A"></a>
If <span class="type">S</span> occurs in a static context, then <code class="varname">i</code> has no
immediately enclosing instance with respect to <span class="type">S</span>.
</p>
</li>
<li class="listitem">
<p class="norm-error"><a name="jls-15.9.2-200-A-B"></a>
Otherwise, if the class instance creation expression occurs
in a static context, then a compile-time error
occurs.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.2-200-A-C"></a>
Otherwise, let <span class="type">O</span> be the immediately enclosing class of
<span class="type">S</span>. Let <span class="emphasis"><em>n</em></span> be an integer such that <span class="type">O</span> is the <span class="emphasis"><em>n</em></span>'th
lexically enclosing type declaration of the class in which
the class instance creation expression appears.
</p>
<p class="norm-dynamic"><a name="jls-15.9.2-300-A-C.1"></a>
The immediately enclosing instance of <code class="varname">i</code> with respect to
<span class="type">S</span> is the <span class="emphasis"><em>n</em></span>'th lexically enclosing instance of
<code class="literal">this</code>.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-200-B"></a>
If <span class="type">S</span> is an inner member class, then:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-200-B-A"></a>
If the class instance creation expression is unqualified,
then:
</p>
<div class="norm">
<ul class="norm" type="square">
<li class="listitem">
<p class="norm-error"><a name="jls-15.9.2-200-B-A-A"></a>
If the class instance creation expression occurs in a
static context, then a compile-time error occurs.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.2-200-B-A-B"></a>
Otherwise, if <span class="type">S</span> is a member of a class enclosing the class in which the class
instance creation expression appears, then let <span class="type">O</span> be
the immediately enclosing class of which <span class="type">S</span> is a
member. Let <span class="emphasis"><em>n</em></span> be an integer such that <span class="type">O</span> is the
<span class="emphasis"><em>n</em></span>'th lexically enclosing type declaration of the
class in which the class instance creation expression
appears.
</p>
<p class="norm-dynamic"><a name="jls-15.9.2-200-B-A-B.1"></a>
The immediately enclosing instance of <code class="varname">i</code> with respect
to <span class="type">S</span> is the <span class="emphasis"><em>n</em></span>'th lexically enclosing instance of
<code class="literal">this</code>.
</p>
</li>
<li class="listitem">
<p class="norm-error"><a name="jls-15.9.2-200-B-A-C"></a>
Otherwise, a compile-time error occurs.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-dynamic"><a name="jls-15.9.2-200-B-B"></a>
If the class instance creation expression is qualified, then
the immediately enclosing instance of <code class="varname">i</code> with respect to
<span class="type">S</span> is the object that is the value of the <span class="emphasis"><em>Primary</em></span>
expression or the <span class="emphasis"><em>ExpressionName</em></span>.
</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div class="section" title="15.9.3.&nbsp;Choosing the Constructor and its Arguments">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-15.9.3"></a>15.9.3.&nbsp;Choosing the Constructor and its Arguments
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-15.9.3-100"></a>Let <span class="type">C</span>
be the class being instantiated. To create an instance of <span class="type">C</span>, <code class="varname">i</code>, a
constructor of <span class="type">C</span> is chosen at compile time by the following
rules:
</p>
<p class="norm-static"><a name="jls-15.9.3-110"></a>First,
the actual arguments to the constructor invocation are
determined:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.3-110-A"></a>
If <span class="type">C</span> is an anonymous class with direct superclass <span class="type">S</span>,
then:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.3-110-A-A"></a>
If <span class="type">S</span> is not an inner class, or if <span class="type">S</span> is a local class
that occurs in a static context, then the arguments to the
constructor are the arguments in the argument list of the
class instance creation expression, if any, in the order
they appear in the expression.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.3-110-A-B"></a>
Otherwise, the first argument to the constructor is the
immediately enclosing instance of <code class="varname">i</code> with respect to <span class="type">S</span>
(<a class="xref" href="jls-15.html#jls-15.9.2" title="15.9.2.&nbsp;Determining Enclosing Instances">&sect;15.9.2</a>), and the subsequent arguments
to the constructor are the arguments in the argument list of
the class instance creation expression, if any, in the order
they appear in the class instance creation
expression.
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.3-110-B"></a>
If <span class="type">C</span> is a local class or a <code class="literal">private</code> inner member class, then
the arguments to the constructor are the arguments in the
argument list of the class instance creation expression, if any,
in the order they appear in the class instance creation
expression.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.3-110-C"></a>
If <span class="type">C</span> is a non-<code class="literal">private</code> inner member class, then the first
argument to the constructor is the immediately enclosing
instance of <code class="varname">i</code>
(<a class="xref" href="jls-8.html#jls-8.8.1" title="8.8.1.&nbsp;Formal Parameters">&sect;8.8.1</a>, <a class="xref" href="jls-15.html#jls-15.9.2" title="15.9.2.&nbsp;Determining Enclosing Instances">&sect;15.9.2</a>), and
the subsequent arguments to its constructor are the arguments in
the argument list of the class instance creation expression, if
any, in the order they appear in the class instance creation
expression.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.3-110-D"></a>
Otherwise, the arguments to the constructor are the arguments in
the argument list of the class instance creation expression, if
any, in the order they appear in the expression.
</p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-15.9.3-120"></a>Second, a
constructor of <span class="type">C</span> and corresponding return type and <code class="literal">throws</code> clause
are determined:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.3-120-A"></a>
If the class instance creation expression uses <code class="literal">&lt;&gt;</code> to
elide class type arguments, a list of methods <code class="varname">m<sub>1</sub></code>...<code class="varname">m<sub>n</sub></code> is
defined for the purpose of overload resolution and type argument
inference.
</p>
<p class="norm"><a name="jls-15.9.3-120-A.1"></a>
Let <code class="varname">c<sub>1</sub></code>...<code class="varname">c<sub>n</sub></code> be the constructors of class
<span class="type">C</span>. Let <code class="literal">#m</code> be an automatically generated
name that is distinct from all constructor and method names in
<span class="type">C</span>. For all <span class="emphasis"><em>j</em></span> (1 <span class="symbol">&#8804;</span> <span class="emphasis"><em>j</em></span> <span class="symbol">&#8804;</span> <span class="emphasis"><em>n</em></span>), <code class="varname">m<sub>j</sub></code> is
defined in terms of <code class="varname">c<sub>j</sub></code> as follows:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm"><a name="jls-15.9.3-120-A.1-A"></a>
A substitution <span class="symbol">&#952;</span><code class="varname"><sub>j</sub></code> is first defined to instantiate the
types in <code class="varname">c<sub>j</sub></code>.
</p>
<p class="norm"><a name="jls-15.9.3-120-A.1-A.1"></a>
Let <span class="type">F<sub>1</sub></span>...<span class="type">F<sub>p</sub></span> be the type parameters of <span class="type">C</span>, and let
<span class="type">G<sub>1</sub></span>...<span class="type">G<sub>q</sub></span> be the type parameters (if any) of <code class="varname">c<sub>j</sub></code>. Let
<span class="type">X<sub>1</sub></span>...<span class="type">X<sub>p</sub></span> and <span class="type">Y<sub>1</sub></span>...<span class="type">Y<sub>q</sub></span> be type variables with distinct
names that are not in scope in the body of <span class="type">C</span>.
</p>
<p class="norm"><a name="jls-15.9.3-120-A.1-A.2"></a>
<span class="symbol">&#952;</span><code class="varname"><sub>j</sub></code> is <code class="literal">[<span class="type">F<sub>1</sub></span>:=<span class="type">X<sub>1</sub></span>, ..., <span class="type">F<sub>p</sub></span>:=<span class="type">X<sub>p</sub></span>,
<span class="type">G<sub>1</sub></span>:=<span class="type">Y<sub>1</sub></span>, ..., <span class="type">G<sub>q</sub></span>:=<span class="type">Y<sub>q</sub></span>]</code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.9.3-120-A.1-B"></a>
The modifiers of <code class="varname">m<sub>j</sub></code> are those of <code class="varname">c<sub>j</sub></code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.9.3-120-A.1-C"></a>
The type parameters of <code class="varname">m<sub>j</sub></code> are <span class="type">X<sub>1</sub></span>...<span class="type">X<sub>p</sub></span>,<span class="type">Y<sub>1</sub></span>...<span class="type">Y<sub>q</sub></span>. The
bound of each parameter, if any, is <span class="symbol">&#952;</span><code class="varname"><sub>j</sub></code> applied to the
corresponding parameter bound in <span class="type">C</span> or <code class="varname">c<sub>j</sub></code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.9.3-120-A.1-D"></a>
The return type of <code class="varname">m<sub>j</sub></code> is <span class="symbol">&#952;</span><code class="varname"><sub>j</sub></code> applied to
<span class="type">C</span><code class="literal">&lt;</code><span class="type">F<sub>1</sub></span>,...,<span class="type">F<sub>p</sub></span><code class="literal">&gt;</code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.9.3-120-A.1-E"></a>
The name of <code class="varname">m<sub>j</sub></code> is <code class="literal">#m</code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.9.3-120-A.1-F"></a>
The (possibly empty) list of argument types of <code class="varname">m<sub>j</sub></code> is
<span class="symbol">&#952;</span><code class="varname"><sub>j</sub></code> applied to the argument types of <code class="varname">c<sub>j</sub></code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.9.3-120-A.1-G"></a>
The (possibly empty) list of thrown types of <code class="varname">m<sub>j</sub></code> is
<span class="symbol">&#952;</span><code class="varname"><sub>j</sub></code> applied to the thrown types of <code class="varname">c<sub>j</sub></code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-15.9.3-120-A.1-H"></a>
The body of <code class="varname">m<sub>j</sub></code> is irrelevant.
</p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-15.9.3-120-A.2"></a>
To choose a constructor, we temporarily consider <code class="varname">m<sub>1</sub></code>...<code class="varname">m<sub>n</sub></code> to
be members of <span class="type">C</span>. Then one of <code class="varname">m<sub>1</sub></code>...<code class="varname">m<sub>n</sub></code> is selected, as
determined by the class instance creation's argument
expressions, using the process specified in
<a class="xref" href="jls-15.html#jls-15.12.2" title="15.12.2.&nbsp;Compile-Time Step 2: Determine Method Signature">&sect;15.12.2</a>.
</p>
<p class="norm-error"><a name="jls-15.9.3-120-A.3"></a>
If there is no unique most specific method that is both
applicable and accessible, then a compile-time error
occurs.
</p>
<p class="norm-static"><a name="jls-15.9.3-120-A.4"></a>
Otherwise, where <code class="varname">m<sub>j</sub></code> is the selected method, <code class="varname">c<sub>j</sub></code> is the chosen
constructor. The return type and <code class="literal">throws</code> clause of <code class="varname">c<sub>j</sub></code> are the
same as the return type and <code class="literal">throws</code> clause determined for <code class="varname">m<sub>j</sub></code>
(<a class="xref" href="jls-15.html#jls-15.12.2.6" title="15.12.2.6.&nbsp;Method Invocation Type">&sect;15.12.2.6</a>).
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-15.9.3-120-B"></a>
Otherwise, the class instance creation expression does not use
<code class="literal">&lt;&gt;</code> to elide class type arguments.
</p>
<p class="norm-static"><a name="jls-15.9.3-120-B.1"></a>
Let <span class="type">T</span> be the type denoted by <span class="type">C</span> followed by any class type
arguments in the expression. The process specified in
<a class="xref" href="jls-15.html#jls-15.12.2" title="15.12.2.&nbsp;Compile-Time Step 2: Determine Method Signature">&sect;15.12.2</a>, modified to handle constructors,
is used to select one of the constructors of <span class="type">T</span> and determine
its <code class="literal">throws</code> clause.
</p>
<p class="norm-error"><a name="jls-15.9.3-120-B.2"></a>
If there is no unique most-specific constructor that is both
applicable and accessible, then a compile-time error occurs (as
in method invocations).
</p>
<p class="norm-static"><a name="jls-15.9.3-120-B.3"></a>
Otherwise, the return type is <span class="type">T</span>.
</p>
</li>
</ul>
</div>
<p class="norm-error"><a name="jls-15.9.3-200"></a>It is a
compile-time error if an argument to a class instance creation
expression is not compatible with its target type, as derived from the
invocation type (<a class="xref" href="jls-15.html#jls-15.12.2.6" title="15.12.2.6.&nbsp;Method Invocation Type">&sect;15.12.2.6</a>).
</p>
<p class="norm-error"><