-1

I am confused about the pass of parameters between two functions, here are two examples with opposite results:

Example 1: If I run the java code below, it will print false on the console. From my understanding, it is related to function scope so that the variable called flag would remain the same in the main function. And function called dfs can not change the variable from the main function.

class HelloWorld {
    public static void main(String[] args) {
        boolean flag=false;
        dfs(flag);
        System.out.println(flag); //this prints false
    }
    
    public static void dfs(boolean flag){
        flag=true;
    }
}

Example 2: this code below is a solution to leetcode 17(i.e., https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) my question is that why the variable called combinations here would be changed(combinations here is acted as a container to store all the satisfying results) in the main function? And why function called doCombination can change the variable from the main function?

private static final String[] KEYS = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

public List<String> letterCombinations(String digits) {
    List<String> combinations = new ArrayList<>();
    if (digits == null || digits.length() == 0) {
        return combinations;
    }
    doCombination(new StringBuilder(), combinations, digits);
    return combinations;
}

private void doCombination(StringBuilder prefix, List<String> combinations, final String digits) {
    if (prefix.length() == digits.length()) {
        combinations.add(prefix.toString());
        return;
    }
    int curDigits = digits.charAt(prefix.length()) - '0';
    String letters = KEYS[curDigits];
    for (char c : letters.toCharArray()) {
        prefix.append(c);                         
        doCombination(prefix, combinations, digits);
        prefix.deleteCharAt(prefix.length() - 1); 
    }
}

Many thanks in advance

I tried to compare those two examples mentioned above. However, I am still confused that why they would have completely different results.

Progman
  • 16,827
  • 6
  • 33
  • 48
xjqing
  • 7
  • 1

1 Answers1

-2

In Java, primitive types, including boolean, are passed by value, meaning they cannot be modified by other methods. Class types, including List, are passed by reference, which means that they can be modified by other methods. (As pointed out by @WJS, the list cannot be modified directly, only its contents, so this is sometimes called "call by sharing", but it is very similar)

R Z
  • 422
  • 4
  • 13
  • 1
    Actually, only the `List` contents can be modified, not the actual reference itself. – WJS Jul 30 '23 at 16:57
  • thank you for your reply, it makes huge sense! thanks!!! – xjqing Jul 30 '23 at 16:58
  • Ouch! [Java is always pass-by-value](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – Seelenvirtuose Jul 30 '23 at 17:01
  • @Seelenvirtuose Even if that is technically correct, it is not useful for describing the behaviour of Java, since the references are implicit. If you want the precision, just use call by sharing or some other equivalent term. – R Z Jul 30 '23 at 17:25
  • The chatter about whether Java is pass-by-value or pass-by-reference seems mostly along the 'angels on the point of a pin' level of pointless, since irrefutably, the called routine gets a reference to a (non-primitive) object. But one argument (ha!) in favour of regarding it as passing (a reference) by value is that it makes parameter passing the same as assignment, and similar to as comparison with '==', etc. – Arfur Narf Jul 30 '23 at 17:38
  • Huh? What did you let think that I wanted to "[describe] the behavior of Java" with my comment? A mere comment is obviously not enough for that. This is the reason, why I linked the very well answered question on this topic. (Btw: Because of this already answered question, you should not do it again.) What I wanted to say here is: Do not use wrong terms for explaining complicated techniques, especially to newcomers. Always use the correct terms and explain them ... and for a complicated issue as that ... do it in depth. – Seelenvirtuose Jul 30 '23 at 17:49