3

I'm a new comer form C#, and I know clearly that "Java is always pass-by-value."

But pass-by-reference is useful when we want to get multiple outputs from one method.

How can we get multiple outputs from one method in java, as in C#.

I know one way to do this -- use a generic wrapper class, and get value from the field.

class Wrapper<T> {
    public Wrapper(T value) {
        Value = value;
    }
    public T Value;
}

Is there another way to realize this effect?

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Harry Wang
  • 85
  • 7
  • Sadly, no. (15 chars) – Mike G May 21 '13 at 14:18
  • In Java everything is pass by value. –  May 21 '13 at 14:18
  • This is the general idea behind all of them at least. – Keppil May 21 '13 at 14:18
  • Why do you, and @LutzHorn think Java is pass-by-value only? Java passes primitives (int, long, double, char, etc.) by value, and **all** object instances are passed by reference. The `T value` parameter in your Wrapper example is passed by reference! – rolfl May 21 '13 at 14:21
  • 10
    @rolfi, Do you really want to re-open [that can of worms](http://stackoverflow.com/questions/40480/is-java-pass-by-reference)? – Mike Samuel May 21 '13 at 14:22
  • 2
    @Rolfi - ::sigh:: **Because Java is only pass by value**. No. Really. The greatest sin ever committed by James Gosling was using the word 'reference' anywhere near Java. – Brian Roach May 21 '13 at 14:24
  • Let's not argue definitions, all you need to know - you can happily modify an object passed to a function, it will modify the underlying object, but assigning a new value to it won't work (won't modify underlying object). Primitives work differently. It's the way C# works as well to my knowledge. – Bernhard Barker May 21 '13 at 14:26
  • No, I don't want to open that can of worms again.... :0 – rolfl May 21 '13 at 14:52
  • The most rational way I've found of getting multiple returns from a method in java is to wrap them all up in a lightweight "struct like" class and return that. Usually all the returned objects are logically connected so a rational name for the stuct-like-class can usually be found – Richard Tingle May 21 '13 at 15:04
  • The OP clearly states that they know that Java is pass-by-value, so this question is *not* a duplicate of the canonical http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1 – Raedwald Apr 30 '14 at 12:42

2 Answers2

3

No, Java does not have out parameters. You can pass an object reference that the method is to modify to pretend that it has out parameters, but this isn't usually the best design and runs into other issues (multithreading and mutable state for one).

The best way to achieve a method that returns multiple values is to have the method return a type that contains multiple values.

matt b
  • 138,234
  • 66
  • 282
  • 345
  • Okey, I agree with you. I'm using java other than C#. There are so many different and I need to adapt to these. – Harry Wang May 21 '13 at 14:48
1

Another way to simulate call by reference in Java is to pass a one-element array as a parameter.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216