1
public class gas_station {
    int start = 0;

    public int commonMethod (int[] gas, int[] cost) {
        int len = gas.length;

        while (this.start < len) {
            if (canComplete(this.start, gas, cost)) {
                return this.start;
            }
        }

        return -1;
    }

    public boolean canComplete (int index, int[] gas, int[] cost) {
        int len = gas.length, sum = 0;
        for (int i = index; i < len + index; i++) {
            sum += gas[i % len] - cost[i % len];
            if (sum < 0) {
                this.start = i + 1;
                return false;
            }
        }

        return true;
    }
}

As you can see, in the canComplete function, I make changes to class member start, and in the commonMethod function, I use it as the loop variable, my question is, is there a way I can make change in the canComplete function and influence the variable in the caller function (here, commonMethod function) instead of making a variable (start) a class member, I know in C++, I can pass a pointer to a function, but what should I do in Java? Thanks!

Haobo_X
  • 77
  • 1
  • 8
  • 1
    Nope, Java is purely [pass by value](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). You can modify a member variable, you can return a value, or you can pass in a modifiable object, but you cannot modify the *variable* in the calling code. – azurefrog Oct 09 '17 at 21:49
  • OK, I know if I pass parameters other than 8 primitive types such as a array, the changes would reflect in the calling function, so there is no way about primitive types such as int, what if I pass a Integer type of parameter other than an int type? – Haobo_X Oct 09 '17 at 21:58
  • `Integer` is just a wrapper, and is immutable, but you could pass an array or an [`AtomicInteger`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html) – azurefrog Oct 09 '17 at 22:00

2 Answers2

2

A typical java pattern is to have a class searching or calculating something, maintaining a state like that start field. On a boolean success one can provide something like getStart().

The reason for not having variables passed by reference to a method, was a design decision to make code quality better. Wherever you see f(x) you know x is not assigned to. The compiler can take advantage of that, the garbage collector.

Java SE examples: Matcher, PreparedStatement, ResultSet

Pattern pat = Pattern.compile("(\\w+)\\s*=\\s*[1-9X]+;");
Matcher m = pat.matcher(s);
while (m.find()) {
    System.out.println(m.group(1) + "=" + m.group(2));
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

is there a way I can make change in the canComplete function and influence the variable in the caller function (here, commonMethod function) instead of making a variable (start) a class member, I know in C++, I can pass a pointer to a function

You can't pass a pointer or by reference in Java. All arguments are passed by value. However if you want the int variable to change in the method. There are a few work around solutions:

  1. Return the value to be changed:

    public int myMethod(){
        //your code
        return val;
    }
    int val = myMethod();
    
  2. Wrap the primitive variable into a class:

    class MyClass{
        private int val;
    }
    
    public void myMethod(MyClass val){
        //changing val here change its origin as well
    }
    
  3. Create a size 1 array:

    int[] val = new int[1];
    public void myMethod(int[] val){
        //changing val here change its origin as well
    }
    

Important point to note: Java had this design for a reason. What I have provided is just to show you some possibilities. Personally, I don't recommend the above mentioned approach, but instead, try a different design in your implementation to achieve what you need.

user3437460
  • 17,253
  • 15
  • 58
  • 106