How to use C ++ reference parameter equivalent in Java?

Suppose I have this in C ++:

void test(int &i, int &j) { ++i; ++j; } 

Values โ€‹โ€‹are changed internally and then used externally. How can I write code that does the same in Java? I suppose I can return a class that encapsulates both values, but that seems very cumbersome.

+3
java c ++ reference
Jan 10 '09 at 3:06
source share
11 answers

Imitation of a link with wrappers.

One way that you can have this behavior in some way simulated is to create a common shell.

 public class _<E> { E ref; public _( E e ){ ref = e; } public E g() { return ref; } public void s( E e ){ this.ref = e; } public String toString() { return ref.toString(); } } 

I am not too convinced of the meaning of this code, I could not help it, I had to code it :)

So there it is.

Sample Usage:

 public class Test { public static void main ( String [] args ) { _<Integer> iByRef = new _<Integer>( 1 ); addOne( iByRef ); System.out.println( iByRef ); // prints 2 _<String> sByRef = new _<String>( "Hola" ); reverse( sByRef ); System.out.println( sByRef ); // prints aloH } // Change the value of ref by adding 1 public static void addOne( _<Integer> ref ) { int i = ref.g(); ref.s( ++i ); // or //int i = ref.g(); //ref.s( i + 1 ); } // Reverse the vale of a string. public static void reverse( _<String> otherRef ) { String v = otherRef.g(); String reversed = new StringBuilder( v ).reverse().toString(); otherRef.s( reversed ); } } 

It's funny that the common name of the wrapper class is "_", which is a valid class identifier. Thus, the declaration reads:

For an integer:

 _<Integer> iByRef = new _<Integer>( 1 ); 

For the string:

 _<String> sByRef = new _<String>( "Hola" ); 

For any other class

 _<Employee> employee = new _<Employee>( Employee.byId(123) ); 

Methods "s" and "g" mean a lot and get: P

+14
Jan 10 '09 at 15:47
source share

Java has no equivalent C ++ references. The only way to get this to work is to encapsulate the values โ€‹โ€‹in another class and exchange the values โ€‹โ€‹inside the class.

Here is a long discussion of the problem: http://www.yoda.arachsys.com/java/passing.html

+13
Jan 10 '09 at 3:10
source share

Java has no passing by reference. You must encapsulate to achieve the desired functionality. Jon Skeet has a brief explanation of why link passing was excluded from Java.

+7
Jan 10 '09 at 3:12
source share

Well, there are a few workarounds. You mentioned it yourself. Another one:

 public void test(int[] values) { ++values[0]; ++values[1]; } 

I would go with a custom object. This is a much cleaner way. Also try reinstalling your problem so that one method does not return two values.

+6
Jan 10 '09 at 3:11
source share

The best question is: why are you creating methods with such side effects?

Typically, this is strong evidence that you should retrieve data in a separate class using publicly available accessories that describe why this operation occurs.

+4
Jan 10 '09 at 16:23
source share

You need to somehow insert it (your path).

The whole is immutable. so useless. int is mutable, but since java is passed by value, then it is not used again in this case. See these pages for more explanation: Java is Pass-by-Value, Dammit! and int vs Integer

Apache commons lang has a MutableInt class. Or you could write it yourself.

In any case, it should not be so bad, because it should not happen often. If so, then you must finally change the way you program in Java.

0
Jan 10 '09 at 3:52
source share

Are these two integers related? How is the x / y coordinate pair? If so, I would put the integers in a new class and pass this class to this method.

 class A{ public void test(Coord c) { c.x++; c.y++; } private class Coord{ public int x, y; } } 

If two integers are not related to each other, you might think about why you pass them to the same method.

0
Jan 11 '09 at 20:33
source share

The simplest solution is to use the org.apache.commons.lang.mutable.MutableInt class, which you do not need to write yourself.

0
Nov 29 '12 at 19:26
source share

Java passes parameters by value and does not have a mechanism that allows passing by reference.

0
Feb 14 '15 at 17:52
source share

Although its design is poor IMHO, another possible solution is

 static void test(AtomicInteger i, AtomicInteger j) { i.incrementAndGet(); j.incrementAndGet(); } 
-one
Jan 23 '09 at 7:29
source share

You can create boxed objects, i.e.

 Integer iObj = new Integer(i); Integer jObj = new Integer(j); 

and write your procedure as

 public void test(Integer i, Integer j){ i = i.add(1); j = j.add(1); } 

For whatever reason, Java developers felt that the gain was better; they intentionally did not include a call by reference method. (Strictly, they pass copies of object references, with a special case for primitive types, which they purely call by value. But the effect is the same.)

-four
Jan 10 '09 at 3:14
source share



All Articles