Chapter 4. Void Methods
So far we’ve only written short programs that have a single class and a single method (main). In this chapter, we’ll show you how to organize longer programs into multiple methods and classes. We’ll also present the Math class, which provides methods for common mathematical operations.
Math Methods
In mathematics, you have probably seen functions like
and
, and you have learned to evaluate expressions like
and
. First, you evaluate the expression in parentheses, which is called the argument of the function. Then you can evaluate the function itself, maybe by punching it into a calculator.
This process can be applied repeatedly to evaluate more complex expressions like
. First we evaluate the argument of the innermost function, then evaluate the function itself, and so on.
The Java library includes a Math class that provides common mathematical operations. Math is in the java.lang package, so you don’t have to import it. You can use, or invoke, Math methods like this: ...