JDK-5076739 : RFE: Need limited pass-by-reference feature in the language
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-07-21
  • Updated: 2006-11-15
  • Resolved: 2006-11-15
Related Reports
Relates :  
Name: rmT116609			Date: 07/21/2004

Java would greatly benefit from the introduction of pass-by-reference for primitive types.  It might also be useful in the case of mutable classes.

This would fix the problem of what to do when you have a method that must return, for example, two ints.  Currently, your solutions are to make a custom return value object for that one method (ugly) or to return one int and pass the other value in wrapped in an int array (also ugly).

The prototype using refs would look like so:
    void myfunc(ref val1, ref val2);

And calling this method would look like so:
    int a,b;
    myfunc(ref a, ref b);

One could implement a MutableInteger class to fix the above problem, but using a 'ref' keyword as per C#, would have some additional benefits - for a start, we could write a proper scanf function that would look like this:

myStaticClass.scanf("%d %f2.4", ref myInt, ref myFloat);

The C# ref keyword avoids the usual problem with pass-by-reference - namely that you cannot tell by looking at a method call whether the values passed in can be modified.  The requirement to have the 'ref' keyword in the method call ensures that the reader knows that the value is liable to be changed.

It removes the neccessity to implement one of two anti-patterns (implementing a class to handle the return type for an  arbitrary method, or boxing an int in an int array to make a mutable return type)

It adds the possiblity to add extremely useful functions like C's scanf (but with safety!) into Java.  The power of this feature would be an immense addition to the language.

Using the C# ref keyword avoids the problem of Pascal and C++'s reference passing - i.e.: that you cannot tell when reading code whether any given value passed to a method will or will not be altered (because the ref keyword is required)

Use the C#  ref  keyword.  Protoype example:

    void myfunc(ref val1, ref val2);

Calling example:
    int a,b;
    myfunc(ref a, ref b);


  To avoid aliasing issues, references should be non assignable.  One reference cannot be assigned to another reference object, the equivallent of saying (in C or C++):

int *a, *b;
a = b;

The only allowable usage of a reference parameter should be to assign a value to it of the type of the referenced object or primitve. (if we take a reference to an int, only ints can be assigned to the reference).

This would be useful also for mutable classes, such as String.

Currently you cannot elegantly handle returning >1 primitive from a method, nor implement a scanf-like function
(Incident Review ID: 281731) 

EVALUATION This is one of those features which unquestionably helps you out in certain cases. It raises many interesting questions: - If you allow by-ref passing for primitives, why not allow it for objects of reference type too? - How does by-ref for primitives interact with boxing conversion? - Can a formal be both ref and final? - Can you pass literals to a ref formal? - If a method completes abruptly, should there be any special semantics for the values of ref formals? - Are by-ref parameters a good way to handle multiple return values? The question is, does we really need it for 2007? I don't think so. Even with 'ref' at the call site, a programmer still has to reason about what's going on. Beyond the language spec, it would be a deep, deep change to the compiler and/or VM, and reduces the possibility of optimisations. Finally, a C-style scanf is not a compelling addition to Java.