In C ++, we have links. But in Java, are we best translating the same copy-by-value code?

Maybe I'm just getting confused, but here goes:

There are many cases where we have a recursive algorithm, the values ​​we need change as we move from one recursion to the next. Examples can be min



, etc.

In C ++ you can pass various parameters as references and everything works fine.

In Java, though, this may not work as everything is copied by value, so an additional class needs to be created for the argument as owner to change it inside the function.
So this is in C ++:

int findLargestSeq(Tree *p, int &min, int &max,Tree *& seqTree)

cannot be "translated" as in Java, but it should be similar:
int findLargestSeq(Tree p, Params p)

where Params

will it encapsulate min


, etc. to update.

I was wondering is this the only way to go?
Is there a cleaner approach or standard pattern in Java for this kind of algorithm?

I think that perhaps the fact that we modify the arguments that are passed as a reference in C ++ is a processed language from C programming, while in Java, which is pure OO, I also get stuck thinking about it is in this procedural manner that I do not see that I have to somehow deal with such problems in different ways.

Any input is appreciated


source to share

5 answers

Everything is copied in Java. However, apart from primitives (int, boolean, char, etc.), everything else is a pointer.

In Java, if you want to return multiple values, you create an aggregator class that contains multiple values. If you really want to have a void method, or a method in which these parameters are changed by a method to return results, you can use Holder .



When the algorithm is complex enough to require recursive calls to be able to change state variables as they continue, then a very simple solution is to use private fields in your class.



Can I pass parameters by reference in Java?

Use the AtomicReference template. It looks ugly and long as hell, but I think it will give you the behavior you are looking for.



Java prefers to use objects and only supports object references, not primitives.

One way to reinterpret

int findLargestSeq(Tree *p, int &min, int &max,Tree *& seqTree)


is an

int findLargestSeq(Tree p, int[] min, int[] max,Tree[] seqTree)


which can be called

Tree p = ...
int[] min = { 0 };
int[] max = { Integer.MAX_VALUE };
Tree[] seqTree = { tree };

int ret = findLargestSeq(p, min, max, seqTree);




In Java, you are not passing a copy of the value, but a copy of the help.

You cannot change a reference by pointing to another object within a method, but - if the object has methods to change it - change the referenced object.

void failToModify (Foo foo) {
    foo = new Bar (); // Useless

void modify (Foo foo) {
    foo.setFoo (new Bar ()); 


Note that this matches arrays:

void failToModify (int[] ia) {
    ia = {3, 4, 5}; // No influence for the caller

void modify (int[] ia) {
    ia[0] = 3;
    ia[1] = 4;
    ia[2] = 5;


And with built-in types too:

void failToModify (int a) {
    a = 8; // No influence for the caller

void modifyImpossible (int a) {
    // a build in type is immutable. 
    // You can't modify 8 to be 7.
    // Only the local copy of the reference is decremented.

modifyImpossible (8);
int a = 7;
failToModify (a);


There are 2 options left: either change the parameter if it is not immutable, or return a new, new object. In a recursive method, you can pass sofar-Object step by step (like minSofar).



All Articles