Lesson: Passing Parameters

Overview: 
Explore how the code that calls a method can pass variables (data) to the method for the method to process.
Objectives: 

Understand passing parameters to methods from the code that calls the method and how this makes methods much more general and useful.

Content: 

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 used to compute any area, any number of times for any height and width values passed on the call. This 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 Java, 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. 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 shortly.

Here is the example in CodingGround.

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 args is a list of options (also called arguments) that are passed to the main method by the JVM when the program is started.

 

Lesson navigation: