Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Actually, in Java, the <strong>references are passed-by-value</strong>.</p> <p>In this case, the reference is a <code>byte[]</code> object. Any changes that affect the object itself will be seen from the caller method.</p> <p>However, if you try to replace the reference, for example using <code>new byte[length]</code>, you are only replacing the reference that you obtained by pass-by-value, so you are not changing the reference in the caller method.</p> <p>Here's an interesting read about this issue: <a href="http://javadude.com/articles/passbyvalue.htm" rel="noreferrer">Java is Pass-by-Value Dammit!</a></p> <hr> <p>Here's an concrete example:</p> <pre><code>public class PassByValue { public static void modifyArray(byte[] array) { System.out.println("Method Entry: Length: " + array.length); array = new byte[16]; System.out.println("Method Exit: Length: " + array.length); } public static void main(String[] args) { byte[] array = new byte[8]; System.out.println("Before Method: Length: " + array.length); modifyArray(array); System.out.println("After Method: Length: " + array.length); } } </code></pre> <p>This program will create a <code>byte</code> array of length <code>8</code> in the <code>main</code> method, which will call the <code>modifyArray</code> method, where the a new <code>byte</code> array of length <code>16</code> is created.</p> <p>It may appear that by creating a new <code>byte</code> array in the <code>modifyArray</code> method, that the length of the <code>byte</code> array upon returning to the <code>main</code> method will be <code>16</code>, however, running this program reveals something different:</p> <pre><code>Before Method: Length: 8 Method Entry: Length: 8 Method Exit: Length: 16 After Method: Length: 8 </code></pre> <p>The length of the <code>byte</code> array upon returning from the <code>modifyArray</code> method reverts to <code>8</code> instead of <code>16</code>.</p> <p>Why is that?</p> <p>That's because the <code>main</code> method called the <code>modifyArray</code> method and sent <strong>a copied reference to the <code>new byte[8]</code></strong> by using <strong>pass-by-value</strong>. Then, the <code>modifyArray</code> method <strong>threw away the copied reference by creating a <code>new byte[16]</code></strong>. By the time we leave <code>modifyArray</code>, the reference to the <code>new byte[16]</code> is out of scope (and eventually will be garbage collected.) However, the <code>main</code> method still has reference to the <code>new byte[8]</code> as it only <strong>sent the copied reference</strong> and not an actual reference to the reference.</p> <p>That should demonstrate that Java will pass reference using pass-by-value.</p>
 

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