tag:blogger.com,1999:blog-34075059384834849562024-02-20T17:14:23.562-08:00Java Language Specification - JLS7Nitesh Kumarhttp://www.blogger.com/profile/15413750278395382414noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-3407505938483484956.post-65139163434208485582013-12-24T02:47:00.003-08:002014-05-07T23:36:00.276-07:00Poor Usage of Language in the Specs<div dir="ltr" style="text-align: left;" trbidi="on">
A snippet from JLS 7<br />
<a href="http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf">http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf</a><br />
<br />
Section : <b>4.12.4 final Variables reads</b><br />
A variable can be declared final. A final variable may only be assigned to once.<br />
Declaring a variable final can serve as useful documentation that its value will<br />
not change and can help avoid programming errors.<br />
<b><i><span style="color: red;">It is a compile-time error if a final variable is assigned to unless it is definitely</span></i></b><br />
<b><i><span style="color: red;">unassigned (§16) immediately prior to the assignment.</span></i></b><br />
<b><i><br /></i></b>
The lines marked in italics do not explain properly what the writer intends to speak.<br />
<br />
The corrected version:<br />
<br />
<b><i><span style="color: #674ea7;">It is a compile-time error if a final variable is assigned to, unless it is definitely</span></i></b><br />
<b><i><span style="color: #674ea7;">unassigned (§16), immediately prior to the assignment.</span></i></b><br />
<div>
<b><i><br /></i></b>
<b><i><br /></i></b>
<b><i>Another Snippet From JLS 7</i></b><br />
<b><i><br /></i></b>
<b><i>Section 17.5.1 Semantics of final Fields</i></b><br />
<b><i><br /></i></b>
<b><i>What does these lines mean :</i></b><br />
<b><i><br />Is this paragraph even readable:</i></b><br />
<b><i><br /></i></b>
<div class="norm" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.799999237060547px;">
Let <span class="emphasis"><em>o</em></span> be an object, and <span class="emphasis"><em>c</em></span> be a constructor for <span class="emphasis"><em>o</em></span> in which a <code class="literal">final</code> field <span class="emphasis"><em>f</em></span> is written. A <span class="emphasis"><em>freeze</em></span> action on <code class="literal">final</code> field <span class="emphasis"><em>f</em></span> of <span class="emphasis"><em>o</em></span> takes place when <span class="emphasis"><em>c</em></span> exits, either normally or abruptly.</div>
<div class="norm" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.799999237060547px;">
<a href="https://www.blogger.com/null" name="jls-17.5.1-110"></a>Note that if one constructor invokes another constructor, and the invoked constructor sets a <code class="literal">final</code> field, the freeze for the <code class="literal">final</code>field takes place at the end of the invoked constructor.</div>
<div class="norm" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.799999237060547px;">
<a href="https://www.blogger.com/null" name="jls-17.5.1-200"></a>For each execution, the behavior of reads is influenced by two additional partial orders, the dereference chain <span class="emphasis"><em>dereferences()</em></span> and the memory chain <span class="emphasis"><em>mc()</em></span>, which are considered to be part of the execution (and thus, fixed for any particular execution). These partial orders must satisfy the following constraints (which need not have a unique solution):</div>
<div class="norm" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.799999237060547px;">
<ul class="norm" type="disc">
<li class="listitem" style="margin-left: -20px;"><div class="norm">
<a href="https://www.blogger.com/null" name="jls-17.5.1-200-A"></a>Dereference Chain: If an action <span class="emphasis"><em>a</em></span> is a read or write of a field or element of an object <span class="emphasis"><em>o</em></span> by a thread <span class="emphasis"><em>t</em></span> that did not initialize <span class="emphasis"><em>o</em></span>, then there must exist some read <span class="emphasis"><em>r</em></span> by thread <span class="emphasis"><em>t</em></span> that sees the address of <span class="emphasis"><em>o</em></span> such that <span class="emphasis"><em>r</em></span> <span class="emphasis"><em>dereferences(r, a)</em></span>.</div>
</li>
<li class="listitem" style="margin-left: -20px;"><div class="norm">
<a href="https://www.blogger.com/null" name="jls-17.5.1-200-B"></a>Memory Chain: There are several constraints on the memory chain ordering:</div>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem" style="margin-left: -20px;"><div class="norm">
<a href="https://www.blogger.com/null" name="jls-17.5.1-200-B-1"></a>If <span class="emphasis"><em>r</em></span> is a read that sees a write <span class="emphasis"><em>w</em></span>, then it must be the case that <span class="emphasis"><em>mc(w, r)</em></span>.</div>
</li>
<li class="listitem" style="margin-left: -20px;"><div class="norm">
<a href="https://www.blogger.com/null" name="jls-17.5.1-200-B-2"></a>If <span class="emphasis"><em>r</em></span> and <span class="emphasis"><em>a</em></span> are actions such that <span class="emphasis"><em>dereferences(r, a)</em></span>, then it must be the case that <span class="emphasis"><em>mc(r, a)</em></span>.</div>
</li>
<li class="listitem" style="margin-left: -20px;"><div class="norm">
<a href="https://www.blogger.com/null" name="jls-17.5.1-200-B-3"></a>If <span class="emphasis"><em>w</em></span> is a write of the address of an object <span class="emphasis"><em>o</em></span> by a thread <span class="emphasis"><em>t</em></span> that did not initialize <span class="emphasis"><em>o</em></span>, then there must exist some read <span class="emphasis"><em>r</em></span> by thread <span class="emphasis"><em>t</em></span>that sees the address of <span class="emphasis"><em>o</em></span> such that <span class="emphasis"><em>mc(r, w)</em></span>.</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="norm" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.799999237060547px;">
<a href="https://www.blogger.com/null" name="jls-17.5.1-300"></a>Given a write <span class="emphasis"><em>w</em></span>, a freeze <span class="emphasis"><em>f</em></span>, an action <span class="emphasis"><em>a</em></span> (that is not a read of a <code class="literal">final</code> field), a read <span class="emphasis"><em>r<sub>1</sub></em></span> of the <code class="literal">final</code> field frozen by <span class="emphasis"><em>f</em></span>, and a read <span class="emphasis"><em>r<sub>2</sub></em></span> such that <span class="emphasis"><em>hb(w, f)</em></span>, <span class="emphasis"><em>hb(f, a)</em></span>, <span class="emphasis"><em>mc(a, r<sub>1</sub>)</em></span>, and <span class="emphasis"><em>dereferences(r<sub>1</sub>, r<sub>2</sub>)</em></span>, then when determining which values can be seen by <span class="emphasis"><em>r<sub>2</sub></em></span>, we consider <span class="emphasis"><em>hb(w, r<sub>2</sub>)</em></span>. (This <span class="emphasis"><em>happens-before</em></span> ordering does not transitively close with other <span class="emphasis"><em>happens-before</em></span> orderings.)</div>
<div class="norm" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.799999237060547px;">
<a href="https://www.blogger.com/null" name="jls-17.5.1-310"></a>Note that the <span class="emphasis"><em>dereferences</em></span> order is reflexive, and <span class="emphasis"><em>r<sub>1</sub></em></span> can be the same as <span class="emphasis"><em>r<sub>2</sub></em></span>.</div>
<div class="norm" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.799999237060547px;">
<a href="https://www.blogger.com/null" name="jls-17.5.1-320"></a>For reads of <code class="literal">final</code> fields, the only writes that are deemed to come before the read of the <code class="literal">final</code> field are the ones derived through the <code class="literal">final</code> field semantics.</div>
<div class="norm" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.799999237060547px;">
<br /></div>
<div class="norm" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.799999237060547px;">
<b><i>What a piece of dirty, unfathomable spec writing. </i></b></div>
<div class="norm" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.799999237060547px;">
<br /></div>
</div>
</div>
Nitesh Kumarhttp://www.blogger.com/profile/15413750278395382414noreply@blogger.com0