How to use references in Java?

An ordinary Java parameter already is closer to a C++ reference than to C++ pass-by-value or pass-by-pointer. So, all your Java methods are already like this.

int and other primitives are special in Java, however; the above is true for object references.


Edit: More precisely, as stated @fatih, all Java invocations are pass-by-value. However, when you pass an object you are passing a reference by value. So, as a first approximation, the above statement is correct: An ordinary Java parameter is more similar to a C++ reference than to C++ pass-by-value or pass-by-pointer.


Objects are passed by reference by default Objects are accessed 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.

public void sum(int[] i){
   i[0] = ...;
}

or

public void sum(MyInt i){
   i.value = ...;
}
public class MyInt{
   public int value; 
}

for your example something like the following could work

public int sum(int v){
   return ...;
}

or

public int sum(){
   return ...;
}

Update:

Additional/Better description of object references:

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.

example:

Modify a primitive within method, this only affects the internal copy of i and not the passed value.

void primitive(int i){
  i = 0;
}

Modify a reference within method, this only affects the internal copy of ref and not the passed value.

 void reference(Object ref){
    ref = new Object();//points to new Object() only within this method
 }

Modify an object, visible globally

void object(List l){
   l.add(new Object());//modifies the object instead of the reference
}

Both the array and MyInt above are based on the modification of an object.


Required reading on understanding Java's Pass By Value semantics:

http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html http://javadude.com/articles/passbyvalue.htm http://javachannel.net/wiki/pmwiki.php/FAQ/PassingVariables (links to several other pages)

Completely remove the notion from your head that Java can have anything passed by reference. Let's look at an example, shall we?

public class App
{
    public static void main( String[] args )
    {
        Foo f1 = new Foo();
        doSomethingToFoo(f1);
        System.out.println(f1.bar); //Hey guess what, f1.bar is still 0 because JAVA IS PASS BY VALUE!!!
    }

    static void doSomethingToFoo(Foo f) {
        f = new Foo();
        f.bar = 99;
    }

    static class Foo {
        int bar = 0;
    }
}