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.

- 25,590
- 9
- 67
- 77

- 9,774
- 28
- 88
- 138
-
14`Integer` is immutable. – Yuval Adam Jul 24 '10 at 17:36
-
1If you want to avoid the apache library, you can pass the int and return the updated value. If it's two values, I recommend using Point as a container. Otherwise, the array option or one of the other suggestions here is fine. – EntangledLoops Feb 20 '15 at 23:52
-
You should be returning an int or an object; If you really need more than one, perhaps you should rethink your 'classes' design – Ujjwal Singh Sep 20 '15 at 08:41
7 Answers
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.
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
.

- 65,295
- 17
- 152
- 131
You can use java.util.concurrent.atomic.AtomicInteger
.

- 5,070
- 8
- 48
- 70

- 441
- 4
- 10
-
According to me, it won't be a good idea to use AtomicInteger, just because we want to pass variable by reference, this is a type under concurrent package, which means this class should be used in cases where you want single variable to be accessed by multiple thread at any instance, better to avoid it in single thread access case, better to use a custom wrapper class over primitive type to get the work done. – Arjun Sharma Jun 27 '23 at 18:29
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.

- 161,610
- 92
- 305
- 395
-
What about the usage of AtomicInteger ? Is this mutable and passed via reference ? (Afaik AtomicReference
does this correctly ). – icbytes May 23 '14 at 08:05
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.

- 349,597
- 67
- 533
- 578
-
6Exactly. Don't force Java to do it your way, do it the Java way. Java is not C. Attempts to make it act like C will always be ugly hacks. – Skip Head Jul 24 '10 at 18:11
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;

- 405
- 2
- 6
- 16
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.

- 649
- 9
- 16

- 1,472
- 1
- 18
- 31