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!

3
  • 1
    Nope, Java is purely 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. Commented Oct 9, 2017 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? Commented Oct 9, 2017 at 21:58
  • Integer is just a wrapper, and is immutable, but you could pass an array or an AtomicInteger Commented Oct 9, 2017 at 22:00

2 Answers 2

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));
}
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

Thanks! I just wanna make sure. got it.
I did, but don't know why can't be displayed.
@Haobo_X What about now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.