0

I'm very new to Java/Android development, coming from C/C++. I'm working on a JNI lib and its java API. I need a java function (or any other beast) that takes a long variable as a parameter, uses its value, and assigns another value to it. In simple words, an equivalent C/C++ code would look like this:

void ReleaseObj(long& nObj)
{
    if (nObj)
    {
        // call a native method that actually releases it
        // ...
        nObj = 0; // assign the variable to 0
    }
}

Now, I know there are no references in Java, as well as there's no way to get a pointer to the variable. But is there a trick that may achieve this? Perhaps a macro (are there macros in Java?).

To work this around currently I've created a wrapper java class that just wraps this long variable and has a public method that does exactly this, but this seems an overkill to me.

Thanks in advance.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
valdo
  • 12,632
  • 2
  • 37
  • 67
  • 2
    There aren't macros, and everything is passed by value, so you cannot modify a reference like you did in C. What you can do is pass in a wrapper object and modify its data state (such as a List, array, etc.), like you are already doing. When you find yourself doing this, you might want to revisit your design. – azurefrog Sep 21 '15 at 19:53
  • @azurefrog: well, that's what I suspected. And I already did exactly what you mentioned. Just hoped there's an alternative. – valdo Sep 21 '15 at 19:54
  • 1
    You write java code differently. Because there is a way to `return` changes to your input. I.e. `long releaseObj(long in){ return 0; }` just not through the parameters – zapl Sep 21 '15 at 19:57
  • @zapl: it's a good idea, thanks. Is there a way I can **enforce** using the function this way? I mean, is there a way to enforce the caller to assign the return value to a variable? – valdo Sep 21 '15 at 20:00
  • 1
    Unfortunately there isn't. They always have the option to throw away the return value. The closest thing I can think of to what you're trying to do would be to use a `long[]` or other wrapper class, which you already are doing... – azurefrog Sep 21 '15 at 20:01
  • Not really. But it's rarely an issue. APIs designed from ground up for Java will rarely have that problem. Compare for example quick sort implementations for Java and C. Without having pointers it looks very different but still does the same in the end. E.g. `swap` methods: `void swap(Object[] array, int index1, int index2)` vs `swap(void *x, void *y)` – zapl Sep 21 '15 at 20:04
  • 2
    There's a good discussion of ways to "fake" pass-by-reference for primitive types in Java [in this question](http://stackoverflow.com/questions/5614562/how-to-do-the-equivalent-of-pass-by-reference-for-primitives-in-java) that I think covers all the options fairly well. – azurefrog Sep 21 '15 at 20:08
  • @azurefrog: good discussion. I think (1) and (2) are the most sane choices. (1) seems to be the safest, whereas (2) is the simplest with presumable less overhead (no need to create a wrapper class instance). – valdo Sep 21 '15 at 20:15
  • [I've got no votes left.](http://stackoverflow.com/questions/5614562/how-to-do-the-equivalent-of-pass-by-reference-for-primitives-in-java) – Sotirios Delimanolis Sep 21 '15 at 20:16
  • Just pass a one-element array. Easy. – Boann Sep 21 '15 at 20:29

0 Answers0