What is Actually Passed to a
Method?
The data that is passed to a method is used inside the method to accomplish tasks.
Definition clarification: What is passed
"to" a method is referred to as an "argument". The "type"
of data that a method can receive is referred to as a "parameter". (You may see "arguments" referred to as "actual
parameters" and "parameters" referred to as "formal parameters".)
|
First, notice that if the arguments are variable names, the formal parameters need not be
those same names (but must be the same data type).
The "order" of the arguments in the method call must correspond to their respective parameters in the method definition.
number1 will correspond to int x
number2 will correspond to int y |
In Java, arguments that are primitive data types sent to methods are passed-by-value, which means that a copy of the actual data stored in the argument is passed to the method
(as opposed to the argument's memory location being passed to the method).
Pass-by-value means that when a method is called, a copy of the value of each argument is passed to the
method. This copy can be changed
inside the method, but such a change will have NO effect on the original argument.
Let's look at some examples:
Ex. 1:
Will the values from main be changed after being sent to this
method?? |
int num = 10;
double decimal = 5.4;
NumberManeuvers(num, decimal);
System.out.println("num = " + num + "and
decimal = " + decimal);
---------------------------------------
public static void NumberManeuvers(int i, double j)
{
if (i == 10)
j = 6.2;
i = 12;
}
// ANSWER: In this case, NO.
The output will be:
num = 10 and decimal = 5.4
|
Here's what's happening!
Copies of the actual parameter values from main are sent
to the method, where they become the values for the formal
parameters. When the method is finished, the copies are
discarded. The actual parameter values remain unchanged.
(Notice that nothing was "returned" from this method.) |
Ex. 2:
What will happen to the values in main after being sent to this
method?? |
int a =
2, b = 3, c = 4, answer = 2;
answer = discriminant(a, b, c);
System.out.println("The discriminant is " + answer);
------------------------------------------------------------
public static int discriminant(int x, int y, int z)
{
int disc;
disc = y*y - 4*x*z;
return disc;
} |
Here's what's happening!
It is possible for a method to change a value in main by
returning and saving the value after the method is complete.
The limitation to this concept is that only ONE value can
be returned from a method.
In this example, answer was initially 2, but after execution of
the method and new assignment to answer, it now has the value
-23. |
Ex. 3:
What will happen to the String dog after dog is sent to this
method?? |
String
dog = "My dog has fleas."
dog = SpeciesChange(dog);
System.out.println("dog);
public static String SpeciesChange(String pet)
{
pet = "My cat has fleas.";
return (pet);
} |
Here's what's happening!
As in the examples above, since the method returns a value that
is then stored in dog, the value of dog is changed. The output is:
My cat has fleas.
If no value had been
returned, there would have been no change to dog. |
Scanner problems with methods!
When we first spoke of the Scanner class (used to receive input from a user), we stated the following:
"If you have multiple inputs from the user of data types involving numerics (int, double) and strings (String), be sure to close the scanner at the END of the program. If you close after your first use of the first data type, the underlying stream (System.in) will get closed and your future attempts to receive input of other data types will fail (actually, you will get an error)."
Okay. This made sense. Close your uses of the Scanner class at the END of the program.
But what happens when your program consists of main and a series of methods which also need to use the Scanner class? When do we close the Scanner class?
If you place the code Scanner reply = new Scanner(System.in); in a method (which is perfectly legal) and then include reply.close(); in the same method, you will "turn off" System.in permanently for your program. Any future attempt to obtain user input will fail. If you code a second Method which also needs user input, and you place Scanner reply2 = new Scanner(System.in); in this new method, it will not be recognized since System.in is now "closed" for this program.
Yuck!!! So how do we deal with this problem.
We know that methods do not have access to information from main (unless it is sent to them). So we must tell the method the information it needs regarding the Scanner class, such as Scanner reply = new Scanner(System.in); Then proceed as normal using the Scanner. BUT, DO NOT CLOSE the Scanner in the method if the Scanner is to be used again in the program.
Now, Eclipse will show a yellow "Warning" that the Scanner has not been closed in the method (Resource leak: 'reply' is never closed). Don't worry! It is just a warning, not an error.