You are on page 1of 4

Java Programming Tutorial - 43 - Methods - Introduction

Objectives
 Understanding Methods.

Introduction

What is a Method?
 A method is a collection of statements that performs a specific task.
 Tasks:
 Display Message (System.out.println())
 Convert Data Types (Integer.parseInt)
 Math.pow
 Main method (To start any Java Project/Program)
 Methods are commonly used to break a problem into small manageable
pieces.
 One long method that contains all of the statements.
 Several small methods that each solve a specific part of the problem
can be written (called divide and conquer). Divides a problem into
smaller problems, each of which are handled by separate method.
Figure:

 Methods is simplify programs (code reuse)


 Writing the code to perform a task once and then reusing it each time
you need to perform the task.

void Methods and Value-Returning Methods


There are two general categories of methods:
1. void methods
2. value returning methods
A void Methods

 void methods is one that simple performs a task and then terminates.
 Ex: System.out.println() is void method.

Example

1. Declares the num variable and initializes it with the value 8.


2. Calls the System.out.println() method.
a) Passing num as argument.
b) The method display a value on the screen, then terminate.
3. The code resume at line 3.

A Value returning Method

 A value returning method not only perform a task, but also sends a value
back to the code that called it.
 Ex: Integer.parseInt() method.

Example:

1. Declares the num variable.


2. Creates a String object with the value “1000” and assigns its address to the
“str” variable.
3. Assigns a value to the num variable.
a) Right side of the = operator is a call to the Integer.parseInt method.
b) Passing str as an argument.
c) The method executes and then return a value to this line of code.
d) The value that is returned from the method is assigned to the num
variable.
4. Display the value of num variable on the screen using System.out.println
method.

Value returning methods return a value to the calling statement.


The calling statement normally does something with this value, such as assign
it to a variable.
Example:

1. Declares the num variable.


2. Creates a String object with the value “1000” and assigns its address to the
“str” variable.
3. Multiplies the Integer.parseInt() method’s return value by 2 and assigns the
result to the num variable. After the code executes, num will be assigned
the value 2000.
4. Display the value of num variable on the screen using System.out.println
method.

Defining a void Method (Create a new Method)

 To create a method you must write its definition, which consists of two
general parts;
 a header
 a body
 The method header, which appears at the beginning of a method definition,
list several important things about the method, including the method’s
name.
 The method body is a collection of statements that are performed when the
method is executed. These statements are enclosed inside a set of curly
braces.

Fig: The header and body of a main method:

As you already know, every complete Java programs must have a main
method. Java program can have other methods as well.
Parts of the Method Header

 Method Modifiers - for this lesson, every method that we write will begin
with public static)

You might also like