<div dir="ltr"><div>It is not only deterministic but also JIT (or an advanced version) could reduce this code to the following single line in main().<br></div><div><br></div><div>System.out.println("Total red weight = 123");</div><div><br></div><div>Since there are no assignments to the Widget fields after construction, they are effectively final.  I wouldn't be surprised if JIT assumes that they are final.  Also, there is no need to put final in the main() body since these values are effectively final.  (You can lookup effectively final.)<br></div><div><br></div><div>I feel like you might be confusing final with deterministic.  Final requires the value to not change.  Deterministic requires that a given input always produces the same output.  The variables can change values throughout the program's computation but the program will still be deterministic if the same input gives the same output.  A nondeterministic program would use SecureRandom.next___() or some other truly random source during its computation.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Nov 22, 2023 at 8:25 AM Archie Cobbs <<a href="mailto:archie.cobbs@gmail.com">archie.cobbs@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Argh, sorry I pasted the wrong example. The Widget fields "color" and "weight" are supposed to NOT be final:</div><div><br></div><div style="margin-left:40px"><span style="font-family:monospace">@@ -5,8 +5,8 @@<br> <br> public class Widget {<br> <br>-    private final Color color;<br>-    private final int weight;<br>+    private Color color;<br>+    private int weight;<br> <br>     public Widget(Color color, int weight) {<br>         this.color = color;</span></div><div><br></div><div>The question remains the same - is the program deterministic?</div><div><br></div><div>-Archie<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Nov 22, 2023 at 10:06 AM Archie Cobbs <<a href="mailto:archie.cobbs@gmail.com" target="_blank">archie.cobbs@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I have an API design question about Streams, but instead of asking the question outright I thought I'd ask it indirectly with a simple example.<br></div><div><br></div><div>The Javadoc for <span style="font-family:monospace">Stream</span> opens with this example:</div><div><pre><code>        int sum = widgets.stream()
                      .filter(w -> w.getColor() == RED)
                      .mapToInt(w -> w.getWeight())
                      .sum();<br></code></pre></div><div><div><span class="gmail_signature_prefix">OK, looks reasonable - we're computing the sum of the weights of all the red widgets.<br></span></div><div><span class="gmail_signature_prefix"><br></span></div><div><span class="gmail_signature_prefix">Here's the question: Is the following program deterministic?<br></span></div><div><span class="gmail_signature_prefix"><br></span></div><div style="margin-left:40px"><span style="font-family:monospace">import java.awt.*;<br>import java.util.function.*;<br>import java.util.stream.*;<br>import static java.awt.Color.*;<br><br>public class Widget {<br><br>    private final Color color;<br>    private final int weight;<br><br>    public Widget(Color color, int weight) {<br>        this.color = color;<br>        this.weight = weight;<br>    }<br><br>    public Color getColor() {<br>        return this.color;<br>    }<br><br>    public int getWeight() {<br>        return this.weight;<br>    }<br><br>    public static int totalRedWeight(Widget... widgets) {<br>        return Stream.of(widgets)<br>                .filter(w -> w.getColor() == RED)<br>                .mapToInt(w -> w.getWeight())<br>                .sum();<br>    }<br><br>// Test method<br><br>    public static void main(String[] args) {<br>        final Widget w1 = new Widget(RED, 123);<br>        final Widget w2 = new Widget(BLUE, 456);<br>        final int total = Widget.totalRedWeight(w1, w2);<br>        System.out.println("Total red weight = " + total);<br>    }<br>}</span></div><div><br></div><div>-Archie<br></div><div><span class="gmail_signature_prefix"><br></span></div><div><span class="gmail_signature_prefix">-- </span></div><div><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div></div></div>
</blockquote></div><br clear="all"><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div>
</blockquote></div>