Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The idiomatic way is to use ternary/conditional operator (<a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25" rel="noreferrer">JLS 15.25</a>):</p> <pre><code>String myString = (someCondition ? "something" : "something else"); </code></pre> <p>But you can also do the more verbose <code>if-else</code> statement if you really feel you must:</p> <pre><code>final String myString; if(someCondition) { myString = "something"; } else { myString = "something else"; } </code></pre> <p>Note that I've added <code>final</code> modifier in the above snippet. If you're planning on further reassignments to the variable, then of course it can't be <code>final</code>, so you can remove the modifier and of course the code would still work.</p> <hr> <h3>Why <code>final</code>?</h3> <p>The point of the <code>final</code> in the above snippet is to show that the <code>if-else</code> construct will assign to <code>myString</code> once and exactly once in all possible execution paths. That is the main idea of the proposed <code>if-else</code> solution: if you're going to assign a value to a local variable only once, even if it can be one of several possibilities, then make it <code>final</code> to enhance readability.</p> <p>Contrast that with this "alternative" proposal for example:</p> <pre><code>// DON'T DO THIS! Example only! String myString = "something else"; if (someCondition) myString = "something"; </code></pre> <p>With this construct, you may be assigning to <code>myString</code> twice, thus you couldn't put <code>final</code> here even if there was no further reassignment. You also couldn't put <code>final</code> in either of the original <code>= null;</code> or <code>= "";</code> proposals, and this is one of the main reasons why they're not recommendable.</p> <p>There's no point in assigning a value to a variable if you're just going to overwrite it before you're going to use it. It hurts readability, and may potentially even hide bugs, e.g. when one execution path fails to overwrite this "initial" value.</p> <h3>References</h3> <ul> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.4" rel="noreferrer">JLS 4.12.4 <code>final</code> Variables</a></li> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/defAssign.html" rel="noreferrer">JLS 16 Definite Assignment</a></li> </ul> <hr> <h3>Summary</h3> <ul> <li>Don't "initialize" a local variable just for the sake of doing it if you're going to overwrite it anyway <ul> <li>Let it be uninitialized, so that <em>the compiler can help you</em> identify a possible bug by pointing out any use of the variable while it's still uninitialized</li> <li>If the code compiles, then the variable is assigned a "real" value <em>at least once</em> before all uses</li> </ul></li> <li>If you don't need to reassign a local variable, make it <code>final</code> to enhance readability <ul> <li><code>final</code> immediately assures readers that no further reassignments are possible</li> <li>The <em>compiler can help you</em> prevent making the mistake of subsequent reassignment</li> <li>If the code compiles, then the variable is assigned a "real" value <em>exactly once</em> before all uses</li> </ul></li> <li>Generally speaking, you should <em>let the compiler help you</em> write the best, most readable code.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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