Java Reference
In-Depth Information
In Chapter 2, you learned that a Java application program is a collection of classes, and
that a class is a collection of methods and data members. One such method is main . The
programs in Chapters 2 through 5 use only the method main ; all the programming
instructions are packed into one method. This technique, however, is appropriate only
for short programs. For large programs, it is not practical (although it is possible) to put
the entire programming instructions into one method, as you will soon discover. You
must learn to break the problem into manageable pieces. This chapter first discusses
previously defined methods and then user-defined methods.
Let's imagine an automobile factory. When an automobile is manufactured, it is not made
from basic raw materials; it is put together from previously manufactured parts. Some
parts are made by the company itself, others are manufactured by different companies at
different locations.
Methods in Java are like automobile parts; they are building blocks. Methods are used to
divide complicated programs into manageable pieces. There are both predefined
methods, methods that are already written and provided by Java, and user-defined
methods, methods that you create.
Using methods has several advantages:
￿ While working on one method, you can focus on just that part of the
program and construct it, debug it, and perfect it.
￿ Different people can work on different methods simultaneously.
￿ If a method is needed in more than one place in a program, or in
different programs, you can write it once and use it many times.
￿ Using methods greatly enhances the program's readability because it
reduces the complexity of the method main .
Methods are often called modules. They are like miniature programs; you can put them
together to form a larger program. When user-defined methods are discussed, you will
see that this is the case. This ability is less apparent with predefined methods because their
programming code is not available to us. However, because predefined methods are
already written, you will learn these first so that you can use them when needed. To
include a predefined method in your program(s), you only need to know how to use it.
Predefined Methods
Before formally discussing Java's predefined methods, let's review a concept from college
algebra. In algebra, a function can be considered a rule or correspondence between
values—called the function's arguments—and the unique value of the function associated
with the arguments. Thus, if f(x) = 2x + 5 , then f(1) = 7 , f(2) = 9 , and f(3) = 11 ,
where 1 , 2 , and 3 are arguments of f , and 7 , 9 , and 11 are the corresponding values of
the function f .
 
Search WWH ::




Custom Search