-1

The parameter passing mechanism confuses me a lot. I've read tons of articles about that such as Is Java "pass-by-reference" or "pass-by-value"?. I know they pass it by value. However, they did not talk about one case: passing by the object itself.

I ran into this problem while solving a leetcode question.

Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"

And there is an recursion code

public class Solution {
public List<String> generateParenthesis(int n) {
    List<String> result = new ArrayList<String>();
    String str = new String("");
    helper(result, str, 0, 0, n);
    return result;
}

public void helper(List<String> result, String str, int left, int right, int n){
    if(left == n && right == n){
        result.add(str);
        return;
    }

    if(left < right){
        return;
    }

    if(left < n){
        helper(result, str + "(", left + 1, right, n);
    }

    if(right < n){
        helper(result, str + ")", left, right + 1, n);
    } 
}    
}

I am struggling understanding what the code does, especially:

    if(left < n){
        helper(result, str + "(", left + 1, right, n);
    }

    if(right < n){
        helper(result, str + ")", left, right + 1, n);
    } 

The second parameter of the helper method is passed by a string instead of a variable name of the string, what would happen in this circumstance? I think maybe that is the part which hinders me understanding the code. Could anyone tell me why does this code work? I really spend a lot of time reading parameter passing mechanism and recursion in Java but it still confuses me.

Thanks a lot.

Community
  • 1
  • 1
Yun Li
  • 327
  • 4
  • 10
  • Please elaborate the question ? – SarthAk Feb 15 '16 at 18:52
  • The evaluation result of the expression `str + "("` is a reference to a String object that is the concatenation of the String referenced by `str`and of the String `"("`. So, you're still passing a reference to an object of type String, by value. Whether you're using a variable or an expression doesn't change anything. – JB Nizet Feb 15 '16 at 18:55
  • It's neither, nor. You pass a copy of the reference to an object. So there is some String in memory. A new refernce to it is generated on the fly, when you call a method. – user unknown Feb 15 '16 at 18:57

1 Answers1

0

Primitives in Java are Pass-by-value, but objects pass a copy of the reference. Also the last four lines of this codeblock shown, 3/4 are curly braces. What exactly are you trying to understand?

So this code basically provides a List of strings as a result, using a recursion method. I don't know how much you know about recursion, but it basically is calling the method within the same method. It can be confusing at first, but if you can understand it you can write beautiful and short code. Basically this is adding a String to the List result. left and right are the number of left facing parenthesis and right facing parenthesis in the current String. I commented the code to maybe help you understand a little better.

    public class Solution {
public List<String> generateParenthesis(int n) {
    List<String> result = new ArrayList<String>(); //The list that gets returned at the end of generateParenthesis method.
    String str = new String(""); //Initialized string that contains nothing.
    helper(result, str, 0, 0, n); //First call of helper method.
    return result; //Final result gets returned to caller.
}

public void helper(List<String> result, String str, int left, int right, int n){
    //If number of left parenthesis and number of right parenthesis is the number of parenthesis total we need, which in your example is 3 of each, add the string to the List result and return to the last caller.
    if(left == n && right == n){
        result.add(str);
        return;
    }

//If the number of left parenthesis is less than the number of right parenthesis, return to the last call of method.
        if(left < right){
            return;
        }
    //If the number of left parenthesis is less than the number of total required, add a left parenthesis to String str and incremement number of left parenthesis by one.
        if(left < n){
            helper(result, str + "(", left + 1, right, n); //call helper via recursion and make the new value of str = str+"(" and increment number of left parenthesis by 1.
        }



  //If number of right parenthesis is less than the number of total parenthesis required, add a right facing parenthesis to String str and increment number of right parenthesis by one.
        if(right < n){
            helper(result, str + ")", left, right + 1, n);
        } 
    }  
Orin
  • 890
  • 5
  • 16