You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -754,7 +754,7 @@ <h2 id="_groovy_as_a_calculator">Groovy as a calculator</h2>
754
754
</div>
755
755
</div>
756
756
<divclass="paragraph">
757
-
<p>This little example introduces some new concepts and syntax. First, it declares and uses a few local variables using the keyword <code>def</code>. Variables declared in this way can contain values of any type, not just numbers. In other wirds, the variables are untyped.</p>
757
+
<p>This little example introduces some new concepts and syntax. First, it declares and uses a few local variables using the keyword <code>def</code>. Variables declared in this way can contain values of any type, not just numbers. In other words, the variables are untyped.</p>
758
758
</div>
759
759
<divclass="paragraph">
760
760
<p>The second item of interest is the <code>sqrt()</code> method call. The syntax is absolutely identical to Java’s and in this case you’re seeing a <em>static</em> method. This is called on the class itself (<code>lava.lang.Math</code>) rather than on an instance of that class.</p>
@@ -867,7 +867,7 @@ <h2 id="_groovy_as_a_calculator">Groovy as a calculator</h2>
867
867
<p>Another interesting facet of the language is that you’re seeing runtime errors for unknown methods and properties. This is because Groovy resolves properties and methods at runtime by default rather than at compile time. This can be changed, as you’ll see in chapter TODO. You can also make use of this runtime resolution to do interesting things such as creating builders (chapter TODO) or mock objects (chapter TODO).</p>
868
868
</div>
869
869
<divclass="paragraph">
870
-
<p>Let’s return to the world of numbers and look at some more examples. In the following script, I perform a floatingpoint multiplication repeatedly and time how long it takes:</p>
870
+
<p>Let’s return to the world of numbers and look at some more examples. In the following script, I perform a floating-point multiplication repeatedly and time how long it takes:</p>
871
871
</div>
872
872
<divclass="listingblock">
873
873
<divclass="content">
@@ -1001,7 +1001,7 @@ <h2 id="_groovy_as_a_calculator">Groovy as a calculator</h2>
1001
1001
</div>
1002
1002
</div>
1003
1003
<divclass="paragraph">
1004
-
<p>Were you expecting a negative number? Whether you were or not, it’s certainly not correct. The reason for this is that <code>int</code> and <code>Integer</code> can only store integers up to a size of 2<sup>31</sup>. If your value goes beyond that, then you end up with something called <em>integer overflow</em>.</p>
1004
+
<p>Were you expecting a negative number? Whether you were or not, that’s what happens and it’s certainly not correct. The reason for this is that <code>int</code> and <code>Integer</code> can only store integers up to a size of 2<sup>31</sup>. If your value goes beyond that, then you end up with something called <em>integer overflow</em>.</p>
1005
1005
</div>
1006
1006
<divclass="paragraph">
1007
1007
<p>You can force Groovy to use a wider integer type by changing the specified type. Long integers are 64 bit, so that will allow us to calculate larger factorials:</p>
@@ -1034,7 +1034,7 @@ <h2 id="_groovy_as_a_calculator">Groovy as a calculator</h2>
@@ -993,11 +993,11 @@ <h2 id="_collections_ranges_and_maps">Collections, ranges and maps</h2>
993
993
<divclass="paragraph">
994
994
<p>We’ll start with ordered collections, which are represented by the <ahref="https://docs.oracle.com/javase/8/docs/api/?java/util/List.html"><code>java.util.List</code></a> interface. In the following example, I create a list of words (strings) and then proceed to iterate over them to calculate the shortest, longest and average word lengths. Before you critize the implementation (there are better ways to do this), remember that I’m trying to introduce new concepts and APIs gradually!</p>
995
995
</div>
996
-
<divclass="listingblock">
996
+
<divid="wordStats" class="listingblock">
997
997
<divclass="content">
998
-
<preclass="CodeRay highlight"><code>def words = ["orange", "sit", "test", "flabbergasted", "honorific"] <bclass="conum">(1)</b>
998
+
<preclass="CodeRay highlight"><code>final words = ["orange", "sit", "test", "flabbergasted", "honorific"] <bclass="conum">(1)</b>
@@ -1008,7 +1008,7 @@ <h2 id="_collections_ranges_and_maps">Collections, ranges and maps</h2>
1008
1008
1009
1009
println "Min word length: ${minWordLength}"
1010
1010
println "Max word length: ${maxWordLength}"
1011
-
println "Avg word length: ${totalLength / words.size()}" <bclass="conum">(3)</b></code></pre>
1011
+
println "Avg word length: ${totalLength / words.size()}" <bclass="conum">(3)</b></code></pre>
1012
1012
</div>
1013
1013
</div>
1014
1014
<divclass="colist arabic">
@@ -1055,7 +1055,7 @@ <h2 id="_collections_ranges_and_maps">Collections, ranges and maps</h2>
1055
1055
<divclass="paragraph">
1056
1056
<p>Sometimes a list literal isn’t sufficient and you need to build one dynamically. Consider a method that creates a list of the powers of two, i.e. <em>f(x) = 2<sup>x</sup></em>:</p>
0 commit comments