Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Declaration is not to declare "value" to a variable; it's to declare the <em>type</em> of the variable.</p> <p>Assignment is simply the storing of a value to a variable.</p> <p>Initialization is the assignment of a value to a variable <em>at the time of declaration</em>.</p> <p>These definitions also applies to fields.</p> <pre><code>int i; // simple declaration i = 42 // simple assignment int[] arr = { 1, 2, 3 }; // declaration with initialization, allows special shorthand syntax for arrays arr = { 4, 5, 6 }; // doesn't compile, special initializer syntax invalid here arr = new int[] { 4, 5, 6 }; // simple assignment, compiles fine </code></pre> <hr> <p>However, it should be mentioned that "initialization" also has a more relaxed definition of "the first assignment to a variable", regardless of where it happens.</p> <pre><code>int i; // local variable declaration if (something) i = 42; System.out.println(i); // compile time error: The local variable i may not have been initialized </code></pre> <p>This, however, compiles:</p> <pre><code>int i; // the following also compiles if i were declared final if (something) i = 42; else i = 666; System.out.println(i); </code></pre> <p>Here <code>i</code> can be "initialized" from two possible locations, by simple assignments. Because of that, if <code>i</code> was an array, you can't use the special array initializer shorthand syntax with this construct.</p> <p>So basically "initialization" has two possible definitions, depending on context:</p> <ul> <li>In its narrowest form, it's when an assignment is comboed with declaration. <ul> <li>It allows, among other things, special array shorthand initializer syntax</li> </ul></li> <li>More generally, it's when an assignment is first made to a variable. <ul> <li>It allows, among other things, assignments to a <code>final</code> variable at multiple places. <ul> <li>The compiler would do its best to ensure that exactly one of those assignments can happen, thus "initializing" the <code>final</code> variable</li> </ul></li> </ul></li> </ul> <hr> <p>There's also JVM-context class and instance initialization, OOP-context object initialization, etc.</p>
    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