Java Basics
Methods 2 - Actual arguments (parameters)
- Left-to-right argument evaluation
- Term: actual argument = value which is passed in a method call.
- Term: void method = method that doesn't return a value.
- Term: value-returning method = method that does return a value.
Terms: actual argument, argument, actual parameter, parameter
The values that are passed to a method are called actual arguments in the Java specification. However, it is very common for them to be called just arguments, actual parameters, or just plain parameters. These terms are so used interchangeably so often that even the Java specification isn't entirely consistent. For a value which is passed in a call I'll try to stick to actual argument or just argument, which are generally regarded as the "best" terms.
Identifying method arguments
When you call a method, you can pass information for it to use. These actual arguments are inside parentheses following the method name. Use commas to separate arguments if there is more than one. The previous program is shown below, but this time the arguments in the calls are highlighted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// File : methods/KmToMilesArgs.java
// Purpose: Convert kilometers to miles. Use JOptionPane for input / output.
// Author : Fred Swartz
// Date : 22 Apr 2006
import javax.swing.*;
public class KmToMilesArgs {
//============================================================ constants
private static final double MILES_PER_KILOMETER = 0.621;
//================================================================= main
public static void main(String[] args) {
//... Local variables
String kmStr; // String km before conversion to double.
double km; // Number of kilometers.
double mi; // Number of miles.
//... Input
kmStr = JOptionPane.showInputDialog(null, "Enter kilometers.");
km = Double.parseDouble(kmStr);
//... Computation
mi = km * MILES_PER_KILOMETER;
//... Output
JOptionPane.showMessageDialog(null, km + " kilometers is "
+ mi + " miles.");
}
}
|
Argument evaluation
Before a method is called, the arguments are evaluated left-to-right.
In the example above most arguments are simple values, except the
second argument in the call to showMessageDialog.
Before the call can be made, this argument expression must be evaluated
by performing the conversions to string and the concatenations.
Void and value-returning methods
A method may return a value. In the example above,
showInputDialog returns a String
and parseDouble returns a double value.
These method calls can be used anywhere in an expression
where a String or double value is required. Here they simply
provide the value for the right side of an assignment.
void.
If a method has a "side effect", but doesn't produce a value,
it is called a void method.
The showMessageDialog method shows something to the
user, but doesn't return a value, and is a void method.
When a method is defined, you need to specify the keyword void
if it doesn't return a value. You can see this on line 13 where the
main method definition starts.
Review Questions
[TODO]