0

Right now I am practicing for AP Computer Science, right? (btw I am self-studying, so gaps in my learning may be large, be warned)

Here is the code that the question will talk about:

    public void changer(String x, int y) {
         x = x + "peace";
         y = y * 2 ;
    }
    public void test() {
         String s = "world";
         int n = 6;
         changer(s,n);
         /*End of method*/
    }

Now, at the point "End of method" String s SHOULD equal to "worldpeace" and int n SHOULD equal to 12. But in the answers, it says that s is still "world" and n is still 6.

Now, obviously this means that the variables have not changed, at least up until that point. Can someone explain past this? When would the variable have changed? I honestly am so confused why s is not "worldpeace" and n is not 12.

1 Answers1

0

Java is not pass by reference. It's pass by value. It just makes copy of variables when method is called. It's just like local variables in a method. Those value in the variables will be lost when method ends.

Shahid
  • 2,288
  • 1
  • 14
  • 24