I’m trying to build a litle gam for android GSMs. I hava lots of units and all of them have destinations. To calculate destination I’m using a function called CalDes. This function CalDes is calculating my unit’s speed. So I’m sending some variables into which I have to edit and use em again. For this I have to send these variables with reference. Here is a simple for C++ How to do that in java?
void swap(SomeType& arg1, Sometype& arg2) {
SomeType temp = arg1;
arg1 = arg2;
arg2 = temp;
}
...
SomeType var1 = ...; // value "A"
SomeType var2 = ...; // value "B"
swap(var1, var2); // swaps their values!
Java doesn’t have pass-by-reference at all. You have a couple of options:
Pass the data in an array:
…but using it is a pain in the calling code:
Create an object that has public
SomeTypemembers, and pass in the object reference.Which is very similar to #1, but probably more convenient to work with from calling code.