Mahdi_Nine
Mahdi_Nine

Reputation: 14751

How to use references in Java?

I want to use reference in Java but I don't know how! for example in C++ we write:

void sum(int& x)
{
    ...
}

but in Java & sign is a compiler error! please help me to understand references in Java.

Upvotes: 12

Views: 63994

Answers (4)

rghome
rghome

Reputation: 8819

The MutableInt class in Apache Commons will do what you want, although it's not pretty.

MutableInt

void sum(MutableInt mx)
{
    int x = mx.getValue();
    x = ...
    mx.setValue(x);
}

...

MutableInt mx = new MutableInt(5);
sum(mx);
int result = mx.getValue();

Additional classes are provided for other primitive types, and also for objects.

There is some overhead involved in creating an additional object simply to provide a reference, so the solution is not optimal, but in most cases you should be ok.

In general, it is always best to find a way to return a result from a method. Unfortunately, Java only allows one value to be returned in this way.

Upvotes: 0

josefx
josefx

Reputation: 15656

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.

Upvotes: 14

Joshua Fox
Joshua Fox

Reputation: 19655

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.

Upvotes: 7

whaley
whaley

Reputation: 16265

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;
    }
}

Upvotes: 5

Related Questions