Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Will this allocate a new memory location? Or just replace the original value?</p> </blockquote> <p>Java does not really make any guarantees that variables will correspond to memory locations; for example, your method might be optimized in such a way that <code>i</code> is stored in a register &mdash; or might not even be stored at all, if the compiler can see that you never actually use its value, or if it can trace through the code and use the appropriate values directly.</p> <p>But setting that aside . . . if we take the abstraction here to be that a local variable denotes a memory location on the call stack, then <code>i = 11</code> will simply modify the value at that memory location. It will not need to use a new memory location, because the variable <code>i</code> was the only thing referring to the old location.</p> <blockquote> <p>Does this mean that primitives are immutable?</p> </blockquote> <p>Yes and no: yes, primitives are immutable, but no, that's not because of the above.</p> <p>When we say that something is mutable, we mean that it can be mutated: changed while still having the same identity. For example, when you grow out your hair, you are mutating yourself: you're still you, but one of your attributes is different.</p> <p>In the case of primitives, all of their attributes are fully determined by their identity; <code>1</code> always means <code>1</code>, no matter what, and <code>1 + 1</code> is always <code>2</code>. You can't change that.</p> <p>If a given <code>int</code> variable has the value <code>1</code>, you can change it to have the value <code>2</code> instead, but that's a total change of identity: it no longer has the same value it had before. That's like changing <code>me</code> to point to someone else instead of to me: it doesn't actually change <em>me</em>, it just changes <code>me</code>.</p> <p>With objects, of course, you can often do both:</p> <pre><code>StringBuilder sb = new StringBuilder("foo"); sb.append("bar"); // mutate the object identified by sb sb = new StringBuilder(); // change sb to identify a different object sb = null; // change sb not to identify any object at all </code></pre> <p>In common parlance, both of these will be described as "changing <code>sb</code>", because people will use "<code>sb</code>" both to refer the <em>variable</em> (which contains a reference) and to the <em>object</em> that it refers to (when it refers to one). This sort of looseness is fine, as long as you remember the distinction when it matters.</p>
    singulars
    1. This table or related slice is empty.
    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. 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