fred basset fred basset - 1 year ago 146
Java Question

Java : Best way to pass int by reference

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
, but it's a bit ugly.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download