How to implement int in/out params in java

前端 未结 6 2105

Apologies for asking such a primitive question.

I wrote a function which took one parameter; this parameter was supposed to be an in/out parameter.

After d

6条回答
  •  余生分开走
    2020-12-21 12:45

    Integer is immutable, that's why its value cant be changed.

    why doesn't the compiler (interpreter?) complain when I do currentFoo = currentFoo + 1

    because when u do currentFoo = currentFoo + 1 a new Integer is created on heap and its reference is assigned to the currentFoo.

    For Integer, if you want to get changed value outside the function, either you can return the value from the function, OR you can put the integer into array/ArrayList and pass that array/ArrayList to the function. Now get change the value, it will be reflected outside the function.

提交回复
热议问题