5.2. Creating a Method

 
[Page 135 ( continued )]

5.5. Passing Parameters by Values

The power of a method is its ability to work with parameters. You can use println to print any string and max to find the maximum between any two int values. When calling a method, you need to provide arguments, which must be given in the same order as their respective parameters in the method specification. This is known as parameter order association . For example, the following method prints a message n times:

   public static void   nPrintln(String message, int n) {   for   (   int   i =     ; i < n; i++)     System.out.println(message); } 

You can use nPrintln("Hello", 3) to print "Hello" three times. The nPrintln("Hello", 3) statement passes the actual string parameter, "Hello" , to the parameter, message ; passes 3 to n ; and prints "Hello" three times. However, the statement nPrintln(3, "Hello") would be wrong. The data type of 3 does not match the data type for the first parameter, message , nor does the second parameter, "Hello" , match the second parameter, n .

Caution

The arguments must match the parameters in order , number , and compatible type , as defined in the method signature. Compatible type means that you can pass an argument to a parameter without explicit casting, such as passing an int value argument to a double value parameter.


When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as pass- by-value . If the argument is a variable rather than a literal value, the value of the variable is passed to the parameter. The variable is not affected, regardless of the changes made to the parameter inside the method. We will examine an interesting scenario in the following example, in which the parameters are changed in the method but the arguments are not affected.

Listing 5.3 is a program that demonstrates the effect of passing by value. The program creates a method for swapping two variables . The swap method is invoked by passing two arguments. Interestingly, the values of the arguments are not changed after the method is invoked. The output of the program is shown in Figure 5.5.

Figure 5.5. The contents of the arguments are not swapped after the swap method is invoked.



[Page 136]
Listing 5.3. TestPassByValue.java
 1   public class   TestPassByValue {  2  /** Main method */  3   public static void   main(String[] args) {  4  // Declare and initialize variables  5   int   num1 =   1   ;  6   int   num2 =   2   ;  7  8      System.out.println(   "Before invoking the swap method, num1 is "   +  9        num1 +   " and num2 is "   + num2); 10 11  // Invoke the swap method to attempt to swap two variables  12  swap(num1, num2);  13 14      System.out.println(   "After invoking the swap method, num1 is "   + 15        num1 +   " and num2 is "   + num2); 16    } 17 18  /** Swap two variables */  19    public static void   swap(   int   n1, int n2)  { 20      System.out.println(   "\tInside the swap method"   ); 21      System.out.println(   "\t\tBefore swapping n1 is "   + n1 22        +   " n2 is "   + n2); 23 24  // Swap n1 with n2  25   int   temp = n1; 26      n1 = n2; 27      n2 = temp; 28 29      System.out.println(   "\t\tAfter swapping n1 is "   + n1 30        +   " n2 is "   + n2); 31    } 32  } 

Before the swap method is invoked (line 12), num1 is 1 and num2 is 2. After the swap method is invoked, num1 is still 1 and num2 is still 2. Their values are not swapped after the swap method is invoked. As shown in Figure 5.6, the values of the arguments num1 and num2 are passed to n1 and n2 , but n1 and n2 have their own memory locations independent of num1 and num2 . Therefore, changes in n1 and n2 do not affect the contents of num1 and num2 .

Figure 5.6. The values of the variables are passed to the parameters of the method.


[Page 137]

Another twist is to change the parameter name n1 in swap to num1 . What effect does this have? No change occurs because it makes no difference whether the parameter and the argument have the same name. The parameter is a variable in the method with its own memory space. The variable is allocated when the method is invoked, and it disappears when the method is returned to its caller.

Note

For simplicity, Java programmers often say passing an argument x to a parameter y , which actually means passing the value of x to y .


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net