19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Let’s look at a method defined <strong>to</strong> find the larger between two integers. This method, named<br />

max, has two int parameters, num1 and num2, the larger of which is returned by the method.<br />

Figure 5.1 illustrates the <strong>com</strong>ponents of this method.<br />

5.2 Defining a Method 179<br />

modifier<br />

Define a method<br />

return value<br />

type<br />

method<br />

name<br />

formal<br />

parameters<br />

Invoke a method<br />

method<br />

header<br />

method<br />

body<br />

public static int max(int num1, int num2) {<br />

int result;<br />

if (num1 > num2)<br />

result = num1;<br />

else<br />

result = num2;<br />

parameter list<br />

method<br />

signature<br />

int z = max(x, y);<br />

actual parameters<br />

(arguments)<br />

}<br />

return result;<br />

return value<br />

FIGURE 5.1<br />

A method definition consists of a method header and a method body.<br />

The method header specifies the modifiers, return value type, method name, and<br />

parameters of the method. The static modifier is used for all the methods in this chapter.<br />

The reason for using it will be discussed in Chapter 8, Objects and Classes.<br />

A method may return a value. The returnValueType is the data type of the value the<br />

method returns. Some methods perform desired operations without returning a value. In this<br />

case, the returnValueType is the keyword void. For example, the returnValueType is<br />

void in the main method, as well as in System.exit, System.out.println, and<br />

JOptionPane.showMessageDialog. If a method returns a value, it is called a valuereturning<br />

method, otherwise it is called a void method.<br />

The variables defined in the method header are known as formal parameters or simply<br />

parameters. A parameter is like a placeholder: When a method is invoked, you pass a value <strong>to</strong><br />

the parameter. This value is referred <strong>to</strong> as an actual parameter or argument. The parameter<br />

list refers <strong>to</strong> the method’s type, order, and number of the parameters. The method name and<br />

the parameter list <strong>to</strong>gether constitute the method signature. Parameters are optional; that is, a<br />

method doesn’t have <strong>to</strong> contain any parameters. For example, the Math.random() method<br />

has no parameters.<br />

The method body contains a collection of statements that implement the method. The<br />

method body of the max method uses an if statement <strong>to</strong> determine which number is larger<br />

and return the value of that number. In order for a value-returning method <strong>to</strong> return a result, a<br />

return statement using the keyword return is required. The method terminates when a return<br />

statement is executed.<br />

method header<br />

modifier<br />

value-returning method<br />

void method<br />

formal parameter<br />

parameter<br />

actual parameter<br />

argument<br />

parameter list<br />

method signature<br />

Note<br />

Some programming languages refer <strong>to</strong> methods as procedures and functions. In those<br />

languages, a value-returning method is called a function and a void method is called a<br />

procedure.<br />

Caution<br />

In the method header, you need <strong>to</strong> declare each parameter separately. For instance,<br />

max(int num1, int num2) is correct, but max(int num1, num2) is wrong.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!