How to Use Methods that Take Parameters in Java - dummies

How to Use Methods that Take Parameters in Java

By Doug Lowe

A parameter is a value that you can pass to a method in Java. Then the method can use the parameter as though it were a local variable initialized with the value of the variable passed to it by the calling method.

The guessing-game application has a method named getRandomNumber that returns a random number between 1 and 10:

public static int getRandomNumber()
{
 return (int)(Math.random() * 10) + 1;
}

This method is useful, but it would be even more useful if you could tell it the range of numbers you want the random number to fall in. It would be nice to call the method like this to get a random number between 1 and 10:

int number = getRandomNumber(1, 10);

Then, if your program needs to roll dice, you could call the same method:

int number = getRandomNumber(1, 6);

Or, to pick a random card from a deck of 52 cards, you could call it like this:

int number = getRandomNumber(1, 52);

You wouldn’t have to start with 1, either. To get a random number between 50 and 100, you’d call the method like this:

int number = getRandomNumber(50, 100);

Declaring parameters

A method that accepts parameters must list the parameters in the method declaration. The parameters are placed in a parameter list inside the parentheses that follow the method name. For each parameter used by the method, you list the parameter type followed by the parameter name. If you need more than one parameter, you separate the parameters with commas.

Here’s a version of the getRandomNumber method that accepts parameters:

public static int getRandomNumber(int min, int max)
{
 return (int)(Math.random()
  * (max – min + 1)) + min;
}

Here the method uses two parameters, both of type int, named min and max. Then, within the body of the method, these parameters can be used as though they were local variables.

The names you use for parameters can be the same as the names you use for the variables you pass to the method when you call it, but they don’t have to be. You could call the getRandomNumber method like this:

int min = 1;
int max = 10;
int number = getRandomNumber(min, max);

Or you could call it like this:

int low = 1;
int high = 10;
int number = getRandomNumber(low, high);

Or you could dispense with the variables altogether and just pass literal values to the method:

int number = getRandomNumber(1, 10);

You can also specify expressions as the parameter values:

int min = 1;
int max = 10;
int number = getRandomNumber(min * 10, max * 10);

Here number is assigned a value between 10 and 100.

Scoping out parameters

The scope of a parameter is the method for which the parameter is declared. As a result, a parameter can have the same name as local variables used in other methods without causing any conflict. Consider this program:

public class ParameterScope
{
 public static void main(String[] args)
 {
  int min = 1;
  int max = 10;
  int number = getRandomNumber(min, max);
  System.out.println(number);
 }
 public static int getRandomNumber(int min, int max)
 {
  return (int)(Math.random()
   * (max – min + 1)) + min;
 }
}

Here the main method declares variables named min and max, and the getRandomNumber method uses min and max for its parameter names. This doesn’t cause any conflict, because in each case the scope is limited to a single method.

Understanding pass-by-value

When Java passes a variable to a method via a parameter, the method itself receives a copy of the variable’s value, not the variable itself. This copy is called a pass-by-value, and it has an important consequence: If a method changes the value it receives as a parameter, that change is not reflected in the original variable that was passed to the method. This program can help clear this up:

public class ChangeParameters
{
 public static void main(String[] args)
 {
  int number = 1;
  tryToChangeNumber(number);
  System.out.println(number);
 }
 public static void tryToChangeNumber(int i)
 {
  i = 2;
 }
}

Here a variable named number is set to 1 and then passed to the method named tryToChangeNumber. This method receives the variable as a parameter named i and then sets the value of i to 2. Meanwhile, back in the main method, println is used to print the value of number after the tryToChangeNumber method returns.

Because tryToChangeNumber gets only a copy of number, not the number variable itself, this program displays the following on the console: 1.

The key point is this: Even though the tryToChangeNumber method changes the value of its parameter, that change has no effect on the original variable that was passed to the method.