Next: Conditional operator. Up: Bits and pieces. Previous: None.


Parameters

Textbook: Section 7.4

We've pretty much finished up everything, but there are some issues that we haven't really covered completely. Today I want to pick up three different issues, which I feel we should cover to finish with Java completely, but which I have put off because there was never time to do it. The first of these has to do with parameters.

Parameters and arguments

A parameter is a named value defined at the time a method is called. An argument is the particular value given into the method. This distinction is somewhat weird, but you'll find that people use it quite often.

For example, consider the following very simple program.

import csbsju.cs160.*;

public class Example {
    public static double square(double x) {
        return x * x;
    }
    public static void main(String[] args) {
        IO.println(square(10));
    }
}
In this program, the parameter of square() is x, and the argument passed into square within main() is 10 (which x represents for the duration of the function.

Parameter passing

Java uses a technique for passing parameters called call by value. That is, an argument's value is copied into the parameter variable, so that changes to the parameter variable do not affect anything except for the method itself. Consider the following program.

import csbsju.cs160.*;

public class Example {
    public static double square(double x) {
        x = x * x;
        return x;
    }
    public static void main(String[] args) {
        int y = 10;
        IO.println(square(y));
        IO.println(square(y));
    }
}
In this program, the variable y within main() holds 10. We pass that in as the value for the parameter x in square(), and that method immediately changes x to be its square, 100. But that change of x does not affect the value of y within main()! This is because the value 10 is copied into the local parameter variable x, and subsequent changes to x only affects that single local variable.

(Some languages pass parameters differently, and some give you a choice. Java isn't among them.)

Passing objects (and arrays)

Objects and arrays work similarly - but it's important to remember that the value of an object variable is actually a pointer to the actual object. So this memory address is what actually gets copied, not the entire object.

Consider the following program using the Account class we defined earlier.

import csbsju.cs160.*;

public class Example {
    public static void doubleAccount(Account acct) {
        acct.deposit(acct.getBalance());
    }
    public static void main(String[] args) {
        Account mine = new Account();
        mine.deposit(100.0);
        doubleAccount(mine);
        IO.println(mine.getBalance());
    }
}
Here, the mine variable's value is the argument to doubleAccount(), and so acct holds the same memory address as mine does. So, actually, acct and mine point to the same object. Thus, the deposit() method is actually also depositing into the object pointed to by mine.

Naturally, this doesn't extend to changes to the actual parameter value. Say we instead wrote doubleAccount() as follows.

    public static void doubleAccount(Account acct) {
        Account new_acct = new Account();
        new_acct.deposit(acct.getBalance());
        new_acct.deposit(acct.getBalance());
        acct = new_acct;
    }
This wouldn't work the same, because the final line here is changing acct to point to a different object. It doesn't actually affect the object pointed to by mine. The net effect of this method is simply to waste time - it creates a new account, but other methods have no way of accessing it as written.

Arrays work the same - which isn't too surprising, since arrays in Java are objects too. For example, you might write the following method to zero out an array.

    public static void zeroArray(int[] array) {
        for(int i = 0; i < array.length; i++) array[i] = 0;
    }

Namespaces

You've probably already figured this out, but it's important to know that each method's variables lives in its own space of names. You can't refer to a method's variables within another method.

In my earlier examples, I was careful to give different variables different names, even if they're in different methods. But that was just for clarity's sake. In the last example program, we could have used acct in place of mine, and it would work the same, since the acct within main() has no relationship to the acct within doubleAccount() - they're completely different variables.


Next: Conditional operator. Up: Bits and pieces. Previous: None.