Ways to avoid passing-by-reference in Java
Ways to avoid passing-by-reference in Java
This question is probably as old as the hills. Nevertheless, after reading lots of articles and forums I still don't see a good solution to my problem.
I have to transfer some Monte Carlo simulation programs from C++ to Java. The problem is that it heavily relies on passing variables by reference in functions, like:
void make_step(int &a, int &b, double &c) { a++; b += a; c *= 1.1; }
There is no passing by reference in Java, as well as possible analogs, like multiple return values or nested functions. Solutions, which I have read on this site, usually involve encapsulation of primitive types into objects. Alternatively - break functions to single-return ones. However, in my case it results in too long and complicated code (few lines of C++ code grow to almost pages in Java). Also, since these algorithms are hard to debug, I want to avoid dramatical changes in code while porting.
What I do now is substituting of all primitive type variables with arrays of length 1 (which are objects and can be modified inside functions). But it doesn't look like very elegant solutions. Does anyone have better ideas?
Answer by brso05 for Ways to avoid passing-by-reference in Java
There are mutable Integer and Decimal classes out there you can use those to pass by reference. Check out Apache Commons libraries here
Answer by Abhinav Y for Ways to avoid passing-by-reference in Java
When writing same logic from c++ to java Rely more on .equals and comparators over == >< for comparisions Use member variables and for local variable return the updated objects back to the called methods to get the changes.
Answer by icza for Ways to avoid passing-by-reference in Java
If you want the parameters to be "in-out" variables, they have to be object references.
With Mutable Wrappers
You can use mutable wrappers but this will require you to slightly modify your existing code (the algorithm):
class Int { int v; } class MDouble { double v; } void make_step(Int a, Int b, MDouble c) { a.v++; b.v += a.v; c.v *= 1.1; }
With Local Variable Copies
If you want to keep your original code, you can create local variable copies of the wrapped parameters, and you can use those without having to modify your code.
At the end of your method (before return
) copy back the local variables into the wrappers and you're done. A tip for this is to use a try-finally
block so your local variables will get copied back into the wrappers no matter how or where your method returns:
void make_step(int[] aa, int[] bb, double[] cc) { int a = aa[0]; int b = bb[0]; double c = cc[0]; try { // all your original code comes here a++; b += a; c *= 1.1; } finally { aa[0] = a; bb[0] = b; cc[0] = c; } }
Answer by David Rabinowitz for Ways to avoid passing-by-reference in Java
There are few options:
- Wrap all parameters in an object. The simple version indeed makes the program longer, but if you can wrap the entire algorithm in an object then those parameters become the object members.
- Use mutable integers (objects) - either Java's AtomicInteger which has get()/set() methods, or the commnos-lang version
Answer by John Humphreys - w00te for Ways to avoid passing-by-reference in Java
You can use "atomic references" to wrap your object in a reference.
There are atomicXXX classes for most basic types as well. These will work similarly to normal references aside from the overhead of creating them in the first place.
Arrays of length 1 are just as a solution, but if you dislike the syntax or appearance of them, this is the only other solution that I know of that can mimic the same behavior without a general refactor.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment