First of all: Java does not allow passing by reference. In addition, out-parameters (when the calculations / results of a function are placed in one or more variables passed to it) are not used; instead, something is return ed from a method like this:
b = a(b);
Otherwise, in Java, you pass objects as pointers ( which are incorrectly called references ). Unfortunately (in your case) most types matching int ( Integer , BigInteger , etc.) are immutable, so you cannot change the properties of an object without creating a new one. However, you can make your own implementation:
public static class MutableInteger { public int value; public MutableInteger(int value) { this.value = value; } } public static void main(String[] args) { MutableInteger b = new MutableInteger(2); increment(b); System.out.println(b.value); } public static void increment(MutableInteger mutableInteger) { mutableInteger.value++; }
When you run this code on the console, the following will be printed:
3
At the end of the day, using the above requires a strong argument as part of the programmer.
source share