Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strike>Objects are passed by reference by default</strike> Objects are <strong>accessed</strong> by reference, but there is no way to create a reference to a primitive value (byte, short,int, long). You either have to create an object to wrap the integer or use a single element array.</p> <pre><code>public void sum(int[] i){ i[0] = ...; } </code></pre> <p>or </p> <pre><code>public void sum(MyInt i){ i.value = ...; } public class MyInt{ public int value; } </code></pre> <p>for your example something like the following could work</p> <pre><code>public int sum(int v){ return ...; } </code></pre> <p>or</p> <pre><code>public int sum(){ return ...; } </code></pre> <p><strong>Update:</strong></p> <p>Additional/Better description of object references:</p> <p>Java Objects are always accessed by a reference. Like the primitive types this reference is passed by value (e.g. copied). Since everything a programmer can access in java is passed by copying it (references, primitives) and there is no way to create a reference to a primitive type, any modification to a method parameter (references, primitives) only affects the local copy within the method. Objects can be modified within a method since both copies of the reference (local and other) still point to the same object instance.</p> <p>example:</p> <p>Modify a primitive within method, this only affects the internal copy of i and not the passed value.</p> <pre><code>void primitive(int i){ i = 0; } </code></pre> <p>Modify a reference within method, this only affects the internal copy of ref and not the passed value.</p> <pre><code> void reference(Object ref){ ref = new Object();//points to new Object() only within this method } </code></pre> <p>Modify an object, visible globally</p> <pre><code>void object(List l){ l.add(new Object());//modifies the object instead of the reference } </code></pre> <p>Both the array and MyInt above are based on the modification of an object.</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