Instructional Material: Passing Parameters

Printer-friendly version

Parameters (also called arguments) are the way in which we can send data (variables or constants) to a method for it to act on. While there are cases were methods may not need parameters, parameters make methods especially useful. Parameters allow a method to produce a different result each time it is called depending on the input values supplied. Parameters are said to be "passed" to the method.

A method parameter-list consists on one or more parameters (separated by commas) specified between the method definition parenthesis. A parameter consists of a data type and a name. The data type can be any Java data type (primitive or reference) and the name any name you wish. The name is used to access the parameter's value inside the method. Here is a simple example:

Here two integer parameters will be passed to the method computeArea() when it is called. The method uses these parameters to compute the result which is returned to the calling code. Calling this method might look like this:

The variable area will contain 100. The computeArea() method can be reused to compute any area, any number of times for any height and width values passed on the call. Using a method to compute the area is also handy if the way you compute the area changes. You only have to change the method, not all the places it is called, which is what you would have to do if you had coded the area computation everywhere you needed it.

Parameter lists can contain variables or literal values. Note that variable names used on the method call do not have to match the names on the parameter list but the data types must match. In the example above, the method uses the parameter names height and width, but the calling program passed a literal value and a variable called x. At the same time, the method cannot use the variable x, it must use the variable width. In other words, I will call computeArea() with the variable x, but in the computeArea() method that value will be known as width.

In Java, primitive data type parameters are passed to methods as a copy of the data from the calling code. This means that changing a parameter inside the method does not change that variable in the calling code. In our example, if you changed the width variable in the computeArea() method, that would not change the variable x in the calling code. This is called passing parameters by value. The actual data value in x is copied into the variable width inside the method. Passing reference variables (objects) as parameters follows this same rule but has some differences to be aware of. We will discuss this in it's own lesson in Unit 8.

Here is the example in CodingGround. Add code to the computeArea() method in the example to change the value of the width variable before the computation. Add code to the main() method to show that the variable x is unaffected by your change to width.

Using your knowledge of the switch statement, add a new method that takes an integer parameter from 1-7 that represents the day of the week and return the name of that day. Call that new method from the main() method and print out the day name.

Remember the main method from the Hello World! example? Here is the main() method definition again:

Now we can see that the main() method has a parameter list, which consists of one parameter, an array of String values. We will discuss arrays later, but for now the args parameter is a list of options (also called arguments) that are passed to the main method by the JVM when the program is started.

 

Material Type: 
Lecture/Presentation
Education Level: 
HW Platform: 
SW Platform: 
Interactivity Style: 
Audience: