How do I use an equivalent to C++ reference parameters in Java?

后端 未结 11 1326
借酒劲吻你
借酒劲吻你 2020-12-09 14:33

Suppose I have this in C++:

void test(int &i, int &j)
{
    ++i;
    ++j;
}

The values are altered inside the function and then use

11条回答
  •  臣服心动
    2020-12-09 15:15

    You have to box it (your way) somehow.

    Integer is immutable. so useless. int is mutable but since java is pass by value then its unusable again in that case. See these pages for more explanations: Java is Pass-by-Value, Dammit! and int vs Integer

    Apache commons lang has a MutableInt class. Or you could write it yourself.

    In any case, it should not be that bad because it should not happen often. If it does, then you should definitively change the way you code in Java.

提交回复
热议问题