Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The issues that you need to learn are:</p> <ul> <li>The scope of variables</li> <li>Block declaration and how that creates new scopes</li> <li>Why you should prefer to use <code>{...}</code> block for <code>if</code> statements</li> <li>How to guarantee definite assignment</li> <li>When to use <code>if-else</code> instead of <code>if (something) {...} if (!something) {...}</code></li> </ul> <p>By the way, the idiomatic way to find the difference of two values is:</p> <pre><code>int difference = Math.abs(firstVar - secondVar); </code></pre> <p>Do note that <code>Math.abs</code> has a corner case: when the argument is <code>Integer.MIN_VALUE</code>, the returned value is <code>Integer.MIN_VALUE</code>. That's because <code>-Integer.MIN_VALUE == Integer.MIN_VALUE</code>. The issue here is 32-bit two's complement representation of numbers.</p> <h3>References</h3> <ul> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/defAssign.html" rel="noreferrer">JLS 16 Definite Assignment</a></li> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.4.2" rel="noreferrer">JLS 14.4.2 Scope of Local Variable Declarations</a></li> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.9.2" rel="noreferrer">JLS 14.9.2 The if-then-else Statement</a></li> <li><a href="http://download.oracle.com/javase/6/docs/api/java/lang/Math.html#abs%28int%29" rel="noreferrer"><code>Math.abs(int)</code></a></li> <li><a href="http://en.wikipedia.org/wiki/Two%27s_complement" rel="noreferrer">Wikipedia/Two's complement</a> (read: the most negative number)</li> </ul> <hr> <h3>Attempt #1: Declaring before the <code>if</code></h3> <p>Here's one attempt to fix the snippet, by declaring the variable before the <code>if</code></p> <pre><code>int difference; if (first_var &gt; second_var) { difference = first_var - second_var; } if (first_var &lt; second_var) { difference = second_var - first_var; } // note: difference is not definitely assigned here! // (but at least it's in scope!) </code></pre> <p>We now have a problem with definite assignment: if <code>first_var == second_var</code>, the variable <code>difference</code> is still not assigned a value.</p> <hr> <h3>Attempt #2: Initializing at declaration</h3> <p>Here's a second attempt:</p> <pre><code>int difference = 0; if (first_var &gt; second_var) { difference = first_var - second_var; } if (first_var &lt; second_var) { difference = second_var - first_var; } // note: difference is in scope here, and definitely assigned </code></pre> <p>Beginners tend to do this, but this precludes the possibility of making <code>difference</code> a <code>final</code> local variable, because it's possibly assigned value twice.</p> <hr> <h3>Attempt #3: <code>if-else</code></h3> <p>Here's a better attempt:</p> <pre><code>final int difference; if (first_var &gt; second_var) { difference = first_var - second_var; } else { difference = second_var - first_var; } // note: difference is in scope, and is definitely assigned here, // (and declared final) </code></pre> <p>There are still ways to improve on this.</p> <hr> <h3>Attempt #4: The ternary/conditional operator</h3> <p>Once you're more comfortable with the language and programming, you may use the following idiom:</p> <pre><code>final int difference = (first_var &gt; second_var) ? first_var - second_var : second_var - first_var; </code></pre> <p>This uses the <code>?:</code> ternary/conditional operator. Do be careful with this operator; it has some behaviors that may be surprising, and it definitely can be abused. Use carefully, judiciously, idiomatically.</p> <h3>References</h3> <ul> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25" rel="noreferrer">JLS 15.25 Conditional Operator <code>?:</code></a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload