10

As we know both language are pass-by-value when passing parameters to methods. But C# supports ref and out keywords to pass-by-reference of primitive types. I am looking for the same keywords and technique in Java?

My guess is using Integer wrapper class instead of int in Java to pass in.

Any suggestions and examples?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Tarik
  • 79,711
  • 83
  • 236
  • 349

6 Answers6

12

Your guess is correct. A wrapper is needed (but not Integer as it is immutable).

Some people use single element arrays for this purpose:

int[] x = { 0 };
int[] y = { 0 };
someMethod(x, y);
return x[0] + y[0];

Many will rank that technique right up there with GOTO.

Some people define a generic holder class:

public class Holder<T> {
    private T _value;
    private Holder(T value) { _value = value; }
    public static of(T value) { return new Holder<T>(value); }
    public T getValue() { return _value; }
    public void setValue(T value) { _value = value; }
}

...

Holder<String> x = Holder.of("123");
Holder<String> y = Holder.of("456");
someMethod(x, y);
return x.getValue() + y.getValue();

Some define a purpose-built type:

SomeMethodResult result = someMethod(x, y);
return result.getX() + result.getY();

Some would arrange for the work to be done inside the method, avoiding the need for by-reference arguments in the first place:

return someMethod(x, y);

Each of these techniques has advantages and disadvantages:

  • arrays: simple vs. ugly, relies on array having exactly one element
  • holder: safe vs. verbose, boxing
  • purpose-built type: safe vs. verbose, possible overkill
  • change method: safe, clean vs. not always possible

Personally, I think that Java messed up on this one. I'd rather avoid by-reference arguments, but I wish Java permitted multiple return values from a method. But, truthfully, I don't trip over this one very often. I wouldn't give a kidney for this feature. :)

WReach
  • 18,098
  • 3
  • 49
  • 93
  • I like the `int[]` technique. I've tried it and it worked nicely. Thanks. – Tarik Dec 12 '10 at 20:07
  • +1 Comprehensive answer and I pretty much agree with the last paragraph. – BoltClock Dec 12 '10 at 20:23
  • IMO `int[]` is ugly but nowhere near " `goto` -level". It's hacking around the lack of a language feature (real pass-by-reference) in the simplest way, which makes it pretty much exactly the same thing as "passing by pointer" in C. We don't need it as often in Java as in C, but that's basically only because of how the two languages deal with memory management and, on a more conceptual level, "object management". – Karl Knechtel Dec 12 '10 at 20:58
  • ... Actually... thinking about that some more... the real question is "why do you want to pass a primitive by reference?" In a language that offers multiple return values (or, equivalently, anonymous tuple construction), the reasons are vanishingly few. – Karl Knechtel Dec 12 '10 at 21:02
  • Worth mentioning that temporarily wrappIng your primitives/references in one of this wrappers just for the duration of the method call isn't enough to modify your original value, it's only the value within the wrapper that will be modified once the method returns. You have to interrogate the wrapper afterwards to pull out the modified value, and may want to assign it to your original variable. (unless the wrapper is the only place you're storing the variable anyway). – bacar Nov 28 '11 at 09:07
5

Java does not support this feature.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

Java does not support passing primitive types by reference.

Wrapping an int as an Integer won't help, since it is an immutable type (i.e. cannot be changed once created).

Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
1

Java does not have this built-in. Integer won't do, because it is immutable. You can't change its state.

You will have to create your own mutable wrapper class. But this is very "dangerous" and may lead to unexpected results, so try to avoid it.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

The only way to pass value back is to pass a reference to a mutable value like

public void calc(int[] value) { value[0] = 1; }
public void calc(AtomicInteger value) { value.set(1); }

however the simplest thing to do is to return all changed values

public int calc() { return 1; }
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Java dont have this feature

here is a link to Java vs C# comparison

http://www.harding.edu/fmccown/java_csharp_comparison.html

thats why they call Java a non pure OOP language

Saif al Harthi
  • 2,948
  • 1
  • 21
  • 26