Chapter6
6.1 Introduction
Methods can be used to define reusable code and organize and simplify coding.
public static int sum(int i1, int i2) {
int result = 0;
for (int i = i1; i <= i2; i++)
result += i;
return result;
}
public static void main(String[] args) {
System.out.println("Sum from 1 to 10 is " + sum(1, 10));
System.out.println("Sum from 20 to 37 is " + sum(20, 37));
System.out.println("Sum from 35 to 49 is " + sum(35, 49));
}
Note
Some programming languages refer to methods as procedures and functions. In those languages, a value-returning method is called a function and a void method is called a procedure.
6.2
define vs. declare
We say “define a method” and “declare a variable.” We are making a subtle distinction here. A definition defines what the defined item is, but a declaration usually involves allocating memory to store data for the declared item.
6.3 Calling a Method
Calling a method executes the code in the method.
There are two ways to call a method, depending on whether the method returns a value or not. If a method returns a value, 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 a 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!");
Caution
A return statement is required for a value-returning method. The method shown
below in (a) is logically correct, but it has a compile error because the Java compiler
thinks that this method might not return a value.
public static int sign(int n) {
if (n > 0)
return 1;
else if (n == 0)
return 0;
else if (n < 0)
return –1;
}
should be
public static int sign(int n) {
if (n > 0)
return 1;
else if (n == 0)
return 0;
else
return –1;
}
reusing method
Methods enable code sharing and reuse. The max method can be invoked from any class, not just TestMax. If you create a new class, you can invoke the max method using ClassName.methodName (i.e.,
TestMax.max).??
activiation record && call stack
Each time a method is invoked, the system creates an activation record (also called an activation frame) that stores parameters and variables for the method and places the activation record in an area of memory known as a call stack. A call stack is also known as an execution stack, runtime stack, or machine stack, and it is often shortened to just “the stack.” When a method calls another method, the caller’s activation record is kept intact, and a new activation record is created for the new method called. When a method finishes its work and returns to its caller, its activation record is removed from the call stack.
6.6 Modularizing Code
Modularizing makes the code easy to maintain and debug and enables the code to be reused.
6.8 Overloading Methods
Overloading methods enables you to define the methods with the same name as long
as their signatures are different.
method overloading
This is referred to as method overloading; that is, two methods have the same name but different parameter lists within one class. The Java compiler determines which method to use based on the method signature.
6.11 Method Abstraction and Stepwise Refinement
The key to developing software is to apply the concept of abstraction.