Java : Best way to pass int by reference

JavaPass by-Reference

Java Problem Overview


I have a parsing function that parses an encoded length from a byte buffer, it returns the parsed length as an int, and takes an index into the buffer as an integer arg. I want the function to update the index according to what it's parsed, i.e. want to pass that index by reference. In C I'd just pass an int *. What's the cleanest way to do this in Java? I'm currently looking at passing the index arg. as an int[], but it's a bit ugly.

Java Solutions


Solution 1 - Java

You can try using org.apache.commons.lang.mutable.MutableInt from Apache Commons library. There is no direct way of doing this in the language itself.

Solution 2 - Java

This isn't possible in Java. As you've suggested one way is to pass an int[]. Another would be do have a little class e.g. IntHolder that wrapped an int.

Solution 3 - Java

You cannot pass arguments by reference in Java.

What you can do is wrap your integer value in a mutable object. Using Apache Commons' MutableInt is a good option. Another, slightly more obfuscated way, is to use an int[] like you suggested. I wouldn't use it as it is unclear as to why you are wrapping an int in a single-celled array.

Note that java.lang.Integer is immutable.

Solution 4 - Java

You can use java.util.concurrent.atomic.AtomicInteger.

Solution 5 - Java

Wrap the byte buffer and index into a ByteBuffer object. A ByteBuffer encapsulates the concept of a buffer+position and allows you to read and write from the indexed position, which it updates as you go along.

Solution 6 - Java

You can design new class like this:

public class Inte{
       public int x=0;
}

later you can create object of this class :

Inte inte=new Inte();

then you can pass inte as argument where you want to pass an integer variable:

public void function(Inte inte) {
some code
}

so for update the integer value:

inte.x=value;

for getting value:

Variable=inte.x;

Solution 7 - Java

You can create a Reference class to wrap primitives:

public class Ref<T>
{
    public T Value;

    public Ref(T value)
    {
        Value = value;
    }
}

Then you can create functions that take a Reference as a parameters:

public class Utils
{
    public static <T> void Swap(Ref<T> t1, Ref<T> t2)
    {
        T temp = t1.Value;
        t1.Value = t2.Value;
        t2.Value = temp;
    }
}

Usage:

Ref<Integer> x = 2;
Ref<Integer> y = 9;
Utils.Swap(x, y);

System.out.println("x is now equal to " + x.Value + " and y is now equal to " + y.Value";
// Will print: x is now equal to 9 and y is now equal to 2

Hope this helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionfred bassetView Question on Stackoverflow
Solution 1 - Javauser319799View Answer on Stackoverflow
Solution 2 - JavamikejView Answer on Stackoverflow
Solution 3 - JavaYuval AdamView Answer on Stackoverflow
Solution 4 - Javauser3070377View Answer on Stackoverflow
Solution 5 - JavaJohn KugelmanView Answer on Stackoverflow
Solution 6 - JavaAnoos SbView Answer on Stackoverflow
Solution 7 - JavaMickael Bergeron NéronView Answer on Stackoverflow