Input Parameters


Methods are often not meant to be self-contained entities. They are intended to act upon arguments that are passed to the method when it is called. The number and type of arguments a method requires are declared in a parameter list that is part of the method declaration. Each element in the list consists of the type of the parameter and a parameter name that will be used by the method to access the parameter. Commas separate each type- name pair in the list. Parameters can be primitive or reference types. A method that has no inputs will have an empty pair of parentheses as its parameter list.

The parameter names only exist within the scope of the method. A parameter name can be the same as the name of one of the class members . Inside the method, such a parameter will temporarily shadow, or hide, the class member variable. You cannot pass a method into a Java method (unlike C which supports the concept of a function pointer). However, you can pass an object to a method and call the object's methods. This topic becomes important when we discuss "Generic Class Libraries" in Chapter 23.

Passing Arguments to Methods

Before we explore how arguments are passed to methods, let's discuss an issue of semantics. Parameters are declared in the method declaration. When a method is called, arguments are sent to the method. When you call a method, the number and type of arguments you pass to the method must match its parameter list. The arguments can be the names of variables , primitive or String literals, or any expression that is compatible with the parameter type.

Arguments to Java methods are passed by value. When an argument is sent to a method, the value of the argument is sent to the method rather than the argument itself. For arguments that are primitive types, a copy of the value of the argument is sent to the method. For example, if you wanted to call a method named blah() that takes an int type as an argument, the syntax

 int j=4; blah(j); 

is functionally equivalent to the syntax

 blah(4); 

When an argument value reaches the method, it is assigned to the corresponding variable in the method's parameter list. The value and the original argument are disconnected. Any changes made to the value inside the method are not made to the value of the argument.

Reference type arguments are also passed by value to methods. The value that is passed is a copy of the reference to the object or array referenced by the argument. When the reference is assigned to the corresponding variable from the method's parameter list, the variable points to the same object as the argument. This means that any changes made through a reference type parameter inside a method will also change the original object. This is handy if you want a method to modify more than one object. A method can only return one value, but it can modify any number of objects if references to them are passed to the method as arguments.

Example: Primitive and Reference Type Input Arguments

This example demonstrates the difference between primitive and reference type variables when they are passed to methods. The doubleThem() method takes two arguments ”an integer variable and an integer array. The method doubles the value of the input arguments. In the main() method of the InputDemo class, an integer variable and an integer array are created, initialized , and passed to the doubleThem() method.

When the method returns, the values of the variables are printed. The elements of the integer array have doubled in value but the integer variable has not. This is because the integer variable is a primitive type. Its value was passed to the method rather than the variable itself. When the value of the parameter variable was doubled inside the method, the original variable was unaffected. An array is a reference type and a copy of the reference to the array was passed to the method. Any changes made through the reference copy in the method are also made to the original object.

 public class InputDemo {   //  This method doubles the value of its input   //  parameters   public void doubleThem(int j, int array[]) {     j*=2;     for(int i=0; i<array.length; ++i) {       array[i]*=2;     }   }   public static void main(String args[]) {     //  create and initialize a primitive and     //  reference type variable.     int j = 4;     int array[] = { 1,2,3 };     InputDemo demo = new InputDemo();     //  Call the doubleThem() method     demo.doubleThem(j,array);     //  Print the new values of the variables     System.out.println("j is "+j);     for(int i=0; i<array.length; ++i) {       System.out.println("array[" + i +                          "] is " + array[i]);     }   } } 

Output ”

 j is 4 array[0] = 2 array[1] = 4 array[2] = 6 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

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