-2

I am aware that there exists the 'out' keyword in C# for passing arguments by reference in a method. Does there exist an equivalent of the 'out' keyword in Java (for passing arguments by reference)?

Questionnaire
  • 656
  • 2
  • 11
  • 25
  • 1
    java is always *pass by value*. *passing by reference* is nothing in java. More : [Is Java “pass-by-reference” or “pass-by-value”?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1) – dumbPotato21 Apr 27 '17 at 12:58
  • no java doesn't have such feature. – Ousmane D. Apr 27 '17 at 12:58
  • 6
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – SomeJavaGuy Apr 27 '17 at 12:58

2 Answers2

0

No there does not exist something like this in Java as far as I know.

However there are wrapper classes available for most types like Integer which you may use as parameter. Then you have objects you work with.

Serraniel
  • 296
  • 1
  • 14
0

The equivalent in Java is to use an object to hold the reference so it can be returned. This is relatively ugly and best avoided but if you want a one for one translation you can do something like.

 ReturnValue method(out int arg, out String x);

translates to

 int[] argRef = { arg };
 String[] xRef = { x };
 // call to
 ReturnValue method(int[] argRef, String[] xRef);

 arg = argRef[0];
 x = xRef[0];
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    "Ugly" is not an engineering term, but nonetheless, why would you call it that? I shouldn't question emotions, I know, but I got curious. Holder objects are ubiquitous and useful. Some might find Java's uncomplicated approach lovely. Or was it that you showed arrays as the holders rather than a proper holder object that you deemed "ugly"? I would agree that the use of arrays is less idiomatic and direct than the use of a holder type. – Lew Bloch Apr 27 '17 at 19:08