Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to return 2 ints

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
I come from a C++ background and I can't figure out how to do this in Java.
Code:
void twice(int& a, int& b)
{
   a += a;
   b += b;
}
There doesn't seem to be a concept of passing int by reference. The & doesn't exist as a reference operator so
Code:
void twice (int a, int b)
{
   a += a;
   b += b;
}
does not work. I can pass a class in, ending up with something like
Code:
public class IntByRef
{
   public IntByRef () {}
   public int value;
}
...
void twice(IntByRef a, IntByRef b)
{
   a.value += a.value;
   b.value += b.value;
}
This seems unduly complicated. I've been browsing the net for the past few hours. I can't find an example that returns any basic type. All the examples in books and on the net only return objects.

Am I missing something? All I want to do is return some basic types.
 
Java does not pass method arguments by reference, only by value.

So I would recommend doing the following:
Code:
public int twice(int inValue) {
    int returnValue = inValue * 2;

    return returnValue;
}

int someValue = twice(someValue);
int anotherValue = twice(anotherValue);
int yetAnotherValue = twice(someValue);
 
Java does not pass method arguments by reference, only by value.
That's not correct.
Java passes copies of references, so you may call a method on an passed object to modify it, but you can't let it refer to another object for the calling context.

The idiom "copy of reference" is true, also for the embeded types (int, long, boolean, ...).

To return two values, just use an array:
Code:
int [] both (int a)
{
        return new int [] {2*a-1, 3*a+1};
}
A List, Queue, Stack, Map, Set or some other Collection might be used as well.
For different types a compound type could be used, while Object[] would work, but violate the idea of typesafety:
Code:
public class DingDong 
{
        Ding ding;
        Dong dong;
        // ...
        public DingDong (Ding i, Dong o) {ding=i; dong=o;}
        // ...
}

int [] both ()
{
        return new DingDong (new Ding (7), new Dong ("foo", 8));
}
If you use an array to hand the arguments in, you could use it, to get values back - since the reference to the array is copied, you may modify the object, which is referenced:
Code:
void swap (int [] ab)
{
        tmp = ab[0];
        ab[0] = ab[1];
        ab[1] = tmp;
}

don't visit my homepage:
 
The problem is I'm writing some JNI code to some C++ DLL. The C++ DLL routines have signatures which are functions returning ints (the status) and values in some of their parameters.

I want to keep the signatures the similar otherwise there will be a whole new load of documentation to write. I can autogenerate the JNI stuff but converting doxygen to javadoc is something else. javadoc doesn't have as many goodies as doxygen.

Guess I'll have to stick to my IntByReference.
 
xwb said:
I want to keep the signatures the similar otherwise there will be a whole new load of documentation to write.
Most of the books I've been reading of late indicate that it's a good idea to wrap calls to third party (or native) code with the interface you'd like it to have, rather than the one it offers. This allows you to write a set of JUnits that validate the way you use the service, and these protect you when the native library gets a fix release or goes up a version. You just swap out the library, and rerun your JUnits. If it all still works, you are good to go.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks for that.

I still couldn't decide after the last posting so I implemented a test program both ways and showed them to the customer.

Anyway, the customer has asked me to keep it the same as the native interface so I'll go with that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top