Describe how data items can be passed to and from actions as parameters.


Teaching Note:

Parameters will be restricted to pass-by-value of one of the four types in D.1.6. (JSR note: probably meant to be D.1.9, not D.1.6.) Actions may return at most one data item.


Sample Question:


JSR Notes:

This assessment statement sticks out like a bit of a soar thumb here in the middle of some OOP material. It surely will have been covered in the middle of Topic 4. But being in the Option, we can go into a bit more depth. First, here's some "deja vu" notes for a kick start if you need one.

         Additional Coding NotesMethods Notes - Part 2 - The Return Part
         Additional Coding NotesMethods Notes - Part 3 - Parameter Passing

Parameter Basics

Paremeters are values sent to and "taken in" by a method, which the method requires to function as intended. In both the method definition, and the method call, the parameters list follows the method name, and is enclosed in a pair of parentheses. If there are not parameters required to be received (nor, therefore sent), the parentheses remain empty.

Example of a method which is supposed to recieve parameters:

public static void someMethod(int x, String y)

And the call to it from another place:

someMethod(2, "hello") //correct because int and String sent

Example of a method which requires no parameters:

public static void anotherMethod( )

And the call to it from another place:

anotherMethod( )


Matching the Number, Order, and Type

The parameters have to be of a particular number, and type and order when sent, to match, exactly the parameter list which is part of the specific method "signature".

Method Signatures

The signature of a method is it's specific:

Here is a method signature of a public static method called method1, which takes in three parameters, an int, a boolean, and an array of Car objects, and which is void of a return value:

public static void method1(int x, boolean b, Car [ ] cArr)

And below is another different method. It is different because it has a different signature; namely the order of the parameters. Like the first example, this one is public and statc, it is also called method1, and it even takes in an int and a boolean and array of Car objects, just not in the same order as the method above:

public static void method1(boolean b, Car [ ] cArr, int x)
// Ok. This is a different signature than above.

With the different order of parameters we can say that the two signatures are different. And since they are different signatures, both of these methods could exist in the same class.

But the following method could not be in the same class as the first one above:

public static void method1(int num, boolean isRaining, Car [ ] carArray)
// Not Ok. Such a signature already exists.

This signature is already taken by the first method above. (Note that the identifier names of the parameters makes no difference, it's their number, type, and order that counts.)

Full Example

So, for example, a method might be:

public static void aMethod(String s, int n, boolean b){ if(b){ for(int i = 0; i < n; i++){ sout(s); } else{ sout("Sorry, you sent false as the boolean variable!"); } }

If using this method, we would have to send three, and exactly three, "arguments", to be received as "parameters", and send them in exactly the following order: String, int, boolean, such as:

aMethod("Hello", 3, true);

And the output of this would be:



Arguments vs. Parameters

It's actually not a bad idea to get nitpicky with this and note that the data that is sent to the method call is best refered to as "arguments", and the copy of that data received by the method is what is best refered to as "parameters". The reason this is good to note is that in memory, they really are two differnent things. Copies, yes, but two different things in memory. This has two major implications, in two situations.

Primitives sent (copied) as Arguments

A primitive value "sent" to a method, where the copy of that method is altered, is not itself changed back where it was "sent".

public static void main(String [ ] args){
    int x = 3;
    System.out.printnln(x); // we see 3
    System.out.printnln(x); // we still see 3. x was not changed, a copy of it was
public static void aMethod(int a){
    System.out.printnln(a); // we see 3
    a = a * 2;
    System.out.printnln(a); // we see 6

Objects (including arrays) sent (copied) as Arguments

When an object (including an array) is "sent" as an argument, the same thing happens: a copy of it is made in the receiving method. But that copy is a copy of the address at which the original array resides. So any changes made via the copied address (the received parameter) will be reflected in what both are pointing to, the array itself. So changes to the array in the called method will be changes to the array in the calling method.

public static void main(String [ ] args){
    int [ ] arr = {101, 103, 106, 109};
    System.out.printnln(arr[2]); // we see 106
    System.out.printnln(arr[2]); // we don't see 106. We see 206, 
                                        reflecting the changes made to the array in aMethod().
public static void aMethod(int [ ] a){ System.out.println(arr[2]); // we see 106 arr[2] = arr[2] + 100; System.out.printnln(arr[2]); // we see 206 }


Final Thoughts

And one final term point is that in some programming environments we talk of formal and actual parameters, but with Java and OOP, just calling the data sent and taken in "parameters" is fine.

Finally, do match all of this up with OOP constructors, set methods, and other methods which take in parameters. It would be good to look at a full OOP program with the above information on parameters in mind.