0

I have a problem: how do I pass a primitive long type as a reference in method extendedEuclid? I found that it isn't possible in java, is there any other solution?

Parameter long a must be passed by reference, here is a code below.

public long extendedEuclid(long a, long b) //a have to be passed as a reference
{
    long x = 0;
    long y = 1;
    long lx = 1;
    long ly = 0;
    long temp_a;
    List quotient = new ArrayList<>();

    while(b != 0)
    {
       quotient.add(a/b);
       temp_a = a;
       a = b;
       b = temp_a % b;
    }

    long temp_x = x;
    long temp_y = y;

    for(int i=0; i<quotient.size()-1; i++)
    {
        x = lx - quotient.indexOf(i) * x;
        y = ly - quotient.indexOf(i) * y;

        lx = x;
        ly = y;

        i++;
        if (i == quotient.size() - 1)
            break;

        x = temp_x - quotient.indexOf(i) * x;
        y = temp_y - quotient.indexOf(i) * y;

        temp_x = x;
        temp_y = y;
    }
    return x;
}
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Strausa
  • 53
  • 8
  • 2
    @YousufUmar Java passes everything by value. One way is to wrap the primitive type in a component type, and pass that type. – Rohit Jain Oct 22 '13 at 08:51

5 Answers5

5

Basically: you can't do this with primitive types like long, int etc. in Java as they're allways passed by-value. Check out Oracles Java tutorials for some background

You could workaround this problem very simple if you use a custom return value containing class like

public class EuclidReturnValues {
    long gcd;
    long latestA;
    long latestB;
}

and change the signature of your method to (assumed you changed your code as well!)

public EuclidReturnValues extendedEuclid(long a, long b)

Edit:

It might also be a good idea to nest this class into your euclid-algorithm-providing class so it's thematically coherent

xmoex
  • 2,602
  • 22
  • 36
1

I found that it doesn't possible in java...

Correct, Java is entirely pass-by-value.

...is any other solution? Parameter long a must be passed by reference.

There are at least two "correct" ways, and a hack:

  1. Don't try to change the passed-in argument; instead, return the updated value as the result of the function (along with x, there are various ways to do that).

  2. Pass in an object instance that has a field, a, which you can update.

  3. (The hack) Pass in a long[1] array; a would be the only entry, which you can update.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Well, almost. If you want to be able to change the value and have it reflected in the calling code, you can do something like this:

class ValueHolder {
    long value;
    //getter, setters, etc
}

And pass this instead of your long.

Note that this isn't passing by reference, you are just passing the value of the ValueHolder reference instead of your long value.

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • 1
    or simply use http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/mutable/package-summary.html – Marco Forberg Oct 22 '13 at 08:52
0

You can use LongByReference:

https://jna.java.net/javadoc/com/sun/jna/ptr/LongByReference.html

Community
  • 1
  • 1
AlonL
  • 6,100
  • 3
  • 33
  • 32
0

You can wrap your primitive value into an object and expose a getLongValue() and a setLongValue() method.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207