Module 6: Methods
Module 6: Methods
Introduction
• Methods can be used to define reusable code
and organize and simplify coding.
• A method is a collection of statements
grouped together to perform an operation. In
earlier chapters you have used predefined
methods such as System.out.println,
System.exit, Math.pow, and Math.random.
These methods are defined in the Java library.
Defining a Method
• A method definition consists of its method
name, parameters, return value type, and
body.
• The syntax for defining a method is as follows:
Defining a Method (cont.)
• Let’s look at a method defined to find the
larger between two integers. This method,
named max, has two int parameters, num1
and num2, the larger of which is returned by
the method.
• Figure 6.1 illustrates the components of this
method.
Defining a Method (cont.)