method

advertisement
Islamic University of Gaza (IUG)
Faculty of Engineering
Computer Engineering Department
Java Programing (1) Lab.
ECOM 2114
Eng. Asma Obeid
Lab 8
Methods
Prepared by
Eng. Asma M. Obeid
12-11-2012
Objectives
 To create methods, invoke methods, and pass arguments to a method.
 To use method overloading and understand ambiguous overloading.
Introduction
A method is a collection of statements that are grouped together to perform an
operation. When you call the System.out.println method, for example, the system
actually executes several statements in order to display a message on the console .
Creating a Method
In general, a method has the following syntax:
modifier returnValueType methodName(list of parameters) {
// Method body;
}
A method declaration consists of a method header and a method body.
Keep
in
Mind
That...
 S
ome
methods perform their desired operation without returning a value. In this case, the
returnValueType is the keyword void.
For example, when you call the methods System.exit, System.out.println, and
JOptionPane.showMessageDialog , you don't assign it to any variable, such
methods are called void methods. The method that returns a value is called a
nonvoid method.
[Pick the date] Page 2
 The variables defined in the method header are known as formal parameters or
simply parameters.
 When a method is invoked, you pass a value to the parameter. This value is
referred to as actual parameter or argument.
 Parameters are optional; that is, a method may contain no parameters.
 You need to declare a separate data type for each parameter. For instance, int
num1, num2 should be replaced by int num1, int num2
Calling a Method
To use a method, you have to call or invoke it. There are two ways to call a method; the
choice is based on whether the method returns a value or not.
If the method returns a value (int type here) , a call to the method is usually treated as a
value. For example,
int larger = max(3, 4);
calls max(3, 4) and assigns the result of the method to the variable larger. Another
example of a call that is treated as a value is
System.out.println(max(3, 4));
which prints the return value of the method call max(3, 4).
If the method returns void, a call to the method must be a statement. For example, the
method println returns void. The following call is a statement:
System.out.println("Welcome to Java!");
Note
A method with a nonvoid return value type can also be invoked as a statement in Java. In
this case, the caller simply ignores the return value. This is rare, but permissible if the
caller is not interested in the return value.
[Pick the date] Page 3
Keep in Mind That...
When a program calls a method, program control is transferred to the called method. A
called method returns control to the caller when its return statement is executed or when
its method-ending closing brace is reached.
Exercise 1: MaxTest.java
Note:
The main method is just like any other method except that it is invoked by the JVM.
[Pick the date] Page 4
When the max method is invoked (line 6), variable i's value 5 is passed to num1,
and variable j's value 2 is passed to num2 in the max method. The flow of
control transfers to the max method. The max method is executed. When the
return statement in the max method is executed, the max method returns the
control to its caller (in this case the caller is the main method).
Keep in Mind That...
 A nonvoid method must have a return statement
void Method Example
The preceding exercise gives an example of a nonvoid method. This one shows how to
declare and invoke a void method.
Exercise 2: TestVoidMethod.java
[Pick the date] Page 5
Note:
A return statement is not needed for a void method, but it can be used for terminating
the method and returning to the method's caller. The syntax is simply
return;
Keep in Mind That...
 The arguments must match the parameters in order, number, and compatible type,
as defined in the method signature.
Passing Parameters by Values
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.
Exercise 3 : TestPassByValue
[Pick the date] Page 6
Overloading Methods
The max method that was used earlier works only with the int data type. But what if you
need to find which of two floating-point numbers has the maximum value? The solution
is to create another method with the same name but different parameters, as shown in the
following exercise:
Exercise 4 : TestMethodOverLoading.java
[Pick the date] Page 7
Homework:
[Pick the date] Page 8
[Pick the date] Page 9
Download