Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I feel like arguing about "pass-by-reference vs pass-by-value" is not super-helpful.</p> <p>If you say, "Java is pass-by-whatever (reference/value)", in either case, you're not provide a complete answer. Here's some additional information that will hopefully aid in understanding what's happening in memory.</p> <p>Crash course on stack/heap before we get to the Java implementation: Values go on and off the stack in a nice orderly fashion, like a stack of plates at a cafeteria. Memory in the heap (also known as dynamic memory) is haphazard and disorganized. The JVM just finds space wherever it can, and frees it up as the variables that use it are no longer needed.</p> <p>Okay. First off, local primitives go on the stack. So this code:</p> <pre><code>int x = 3; float y = 101.1f; boolean amIAwesome = true; </code></pre> <p>results in this:</p> <p><img src="https://i.stack.imgur.com/7nGKU.png" alt="primitives on the stack"></p> <p>When you declare and instantiate an object. The actual object goes on the heap. What goes on the stack? The address of the object on the heap. C++ programmers would call this a pointer, but some Java developers are against the word "pointer". Whatever. Just know that the address of the object goes on the stack.</p> <p>Like so:</p> <pre><code>int problems = 99; String name = "Jay-Z"; </code></pre> <p><img src="https://i.stack.imgur.com/yTIYp.png" alt="a b*7ch aint one!"></p> <p>An array is an object, so it goes on the heap as well. And what about the objects in the array? They get their own heap space, and the address of each object goes inside the array.</p> <pre><code>JButton[] marxBros = new JButton[3]; marxBros[0] = new JButton("Groucho"); marxBros[1] = new JButton("Zeppo"); marxBros[2] = new JButton("Harpo"); </code></pre> <p><img src="https://i.stack.imgur.com/v2b33.png" alt="marx brothers"></p> <p>So, what gets passed in when you call a method? If you pass in an object, what you're actually passing in is the address of the object. Some might say the "value" of the address, and some say it's just a reference to the object. This is the genesis of the holy war between "reference" and "value" proponents. What you call it isn't as important as that you understand that what's getting passed in is the address to the object.</p> <pre><code>private static void shout(String name){ System.out.println("There goes " + name + "!"); } public static void main(String[] args){ String hisName = "John J. Jingleheimerschmitz"; String myName = hisName; shout(myName); } </code></pre> <p>One String gets created and space for it is allocated in the heap, and the address to the string is stored on the stack and given the identifier <code>hisName</code>, since the address of the second String is the same as the first, no new String is created and no new heap space is allocated, but a new identifier is created on the stack. Then we call <code>shout()</code>: a new stack frame is created and a new identifier, <code>name</code> is created and assigned the address of the already-existing String.</p> <p><img src="https://i.stack.imgur.com/q0prc.png" alt="la da di da da da da"></p> <p>So, value, reference? You say "potato".</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. 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