Day 04 - Methods
Day 04 - Methods
Proposition
❑ public: This keyword indicates that the method’s existence should be publicized to the world,
and that any Java program that knows about your program (or, more accurately, the class
defined for your Java program) should be able to use your method. That’s not very meaningful
for the types of programs you’re dealing with at this point in the course, but it will become
more meaningful later on.
Introduction to Method – Method Structure 10
❖ As can be seen from the figure, the
method header specifies the modifiers,
return value type, method name, and
parameters of the method. The following
paragraphs describe method declarations
piece-by-piece:
Proposition
❑ static: The static modifier might be used for all the methods in this course. The reason for
using it will be discussed in Programming 02 (PR2).
Introduction to Method – Method Structure 11
❖ As can be seen from the figure, the
method header specifies the modifiers,
return value type, method name, and
parameters of the method. The following
paragraphs describe method declarations
piece-by-piece:
Proposition
❑ return-value-type: After the word static comes the return value type, which indicates whether
the method returns a value when it is called and, if so, what type the value is. If the method
doesn’t return a value, specify void.
Introduction to Method – Method Structure 12
❖ As can be seen from the figure, the
method header specifies the modifiers,
return value type, method name, and
parameters of the method. The following
paragraphs describe method declarations
piece-by-piece:
Proposition
❑ method-name: The name of the method. This name should follow the naming conventions
mentioned a few week ago.
Fun fact: We say “define a method” and “declare a variable.” We are making a subtle distinction here. A definition defines what the defined item is, but a declaration usually
involves allocating memory to store data for the declared item.
Introduction to Method – Method Structure 13
❖ As can be seen from the figure, the
method header specifies the modifiers,
return value type, method name, and
parameters of the method. The following
paragraphs describe method declarations
piece-by-piece:
Proposition
❑ parameter-list: You can pass one or more values to a method by listing the values in
parentheses following the method name. The parameter list in the method declaration lets
Java know what types of parameters a method should expect to receive and provides names
so that the statements in the method’s body can access the parameters as local variables. If
the method doesn’t accept parameters, you must still code the parentheses that surround the
parameter list. You just leave the parentheses empty.
Introduction to Method – Method Structure 14
❖ As can be seen from the figure, the
method header specifies the modifiers,
return value type, method name, and
parameters of the method. The following
paragraphs describe method declarations
piece-by-piece:
Proposition
❑ method-body: The method body consists of one or more Java statements enclosed in a set of
braces. Unlike Java statements such as if, while, and for, you still have to use the braces even
if the body of your method consists of only one statement.
Facts
❑ Some programming languages refer to methods as procedures and functions. In those languages, a value-returning
method is called a function and a void method is called a procedure.
Need to know how to use this?
It's time to see how it works.
15
Introduction to Method – Calling a Method 16
❖ In a method definition, you define what the method is to do. To execute the method, you
have to call or invoke it. There are two ways to call a method, depending on whether the
method returns a value or not.
❖ If a method returns a value, a call to the method is usually treated as a value. For
example:
int larger = max(3, 4); - calls max(3, 4) and assigns the result of the method to the variable larger.
System.out.println(max(3, 4)); - which prints the return value of the method call max(3, 4).
❖ If a method returns void, a call to the method must be a statement. For example, the
method println returns void. The following call is a statement:
System.out.println("Welcome to Java!");
Introduction to Method – Calling a Method 17
❖ When a program calls a method, program control is transferred to the called method. A
called method returns control to the caller when its return statement is executed or when
its method-ending closing brace is reached.
Figure A. When the max method is invoked, the flow of control transfers to it. Once the max method is
finished, it returns control back to the caller.
Introduction to Method – Calling a Method 18
❖ Each time a method is invoked, the system creates an activation record (also called an activation frame)
that stores parameters and variables for the method and places the activation record in an area of memory
known as a call stack. A call stack is also known as an execution stack, runtime stack, or machine stack,
and it is often shortened to just “the stack.” When a method calls another method, the caller’s activation
record is kept intact, and a new activation record is created for the new method called. When a method
finishes its work and returns to its caller, its activation record is removed from the call stack.
19
I was just thinking... I understand how
methods are structured and executed,
but are there different types of methods
in Java? How can I create one?
Good question! Yes, Java has different types of methods, but the
one we'll focus on today is the static method. There’s also another
distinction between void methods and returning methods,
methods with and without parameters. Each serves a different
purpose. Let me explain what makes them distinct. Then you will
see how to define a method based on your demand.
20
❖ Introduction to Method
❖ Types of Methods
❑ Void Methods
❑ Methods Returning Values
❖ Method Features
❖ Final Touches
21
❖ And to call this method, we need to invoke it from another method, such as the main
method in this case, by using a statement.
Proposition
❑ The return type of a method can be any of Java’s primitive return types, or the return type can
be a reference type, including a class defined by the API such as String or a class you create
yourself.
Types of Method – Value-Returning Methods 26
❖ As can be seen, to create a method that returns a value, you simply indicate the type of
the value returned by the method on the declaration in place of the void keyword. For
example, here’s a method declaration that creates a method that returns a String value:
Proposition
❑ When you specify a return type other than void in a method declaration, the body of the
method must include a return statement that specifies the value to be returned. The return
statement has this form:
return literals or expression;
❑ The expression must evaluate to a value that’s the same type as the type listed in the method
declaration. In other words, if the method returns a String, the expression in the return
statement must evaluate to a String.
Now that we know how to
define methods in Java, have
you ever wondered how we can
provide these methods with the
information they need to
perform their tasks?
Types of Method – Passing Arguments by Values 28
❖ You know, oftens, programmers write methods as if they were black boxes — the
programmer knows what is going on inside the box, but all of those implementation
details are hidden from the user. The user should be aware of the input of the black box,
and they should be able to predict an output given the input, but the details of how that
input gets transformed into the output can be hidden from the users.
Example
❑ Consider a television remote — we can give the remote some input, like pressing the “Volume Up” button, and we
can expect some output, like the volume increasing. But beyond that, all of the inner workings of the process is
hidden from us. We, as the television remote user, don’t really know how the remote communicates with the TV or
how the TV interfaces with its speakers. All we know is the format of the input and the expected output. Then we let
the black box do the rest.
Types of Method – Passing Arguments by Values 29
❖ It is the fact that the power of a method is its ability to work with those input (or formal
parameters). For example, you can use println to print any string and max to find the
maximum of any two int values. Remember that when calling a method, you need to
provide arguments (or actual parameters), which must be given in the same order as their
respective parameters in the method signature. This is known as parameter order
association.
Notice
❑ The arguments must match the parameters in order, number, and compatible type, as defined in the
method signature. Compatible type means that you can pass an argument to a parameter without
explicit casting, such as passing an int value argument to a double value parameter.
Types of Method – Passing Arguments by Values 30
❖ A parameter is a value that you can pass to a method. The method can then use the
parameter as if it were a local variable initialized with the value of the variable passed to
it by the calling method.
Formal Parameters
Scope of i1 and i2
Returned value
Actual Parameters
And please keep in
(Arguments)
mind that both types of
methods may require
parameters and use
them in a similar way.
Proposition
❑ When Java passes a variable to a method via a parameter, the method itself receives a copy of the variable’s value, not
the variable itself. This copy is called a pass-by-value, and it has an important consequence: If a method changes the
value it receives as a parameter, that change is not reflected in the original variable that was passed to the method.
31
Proposition
❑ As can be seen, we divided a large problem into two subproblems: determining whether a
number is a prime and printing the prime numbers. As a result, the new program is easier to
read and easier to debug. Moreover, the methods printPrimeNumbers and isPrime can be reused
by other programs.
Methods Features – Overloading Methods 37
❖ Modularization involves breaking down functionality into distinct methods. But what
happens when we need to use the same method name to handle different inputs? For
example, the max method that was used earlier works only with the int data type. But
what if you need to determine which of two floating-point numbers has the maximum
value?
We’ll cover recursion in detail later in this course. For now, just
keep in mind that it’s a method calling itself to solve problems
more efficiently. We’ll explore how it works and see some
practical examples soon.
Delivered
That’s all about Methods,
do you have any question?
If not? Let’s summarize how to define and use methods…
41
Summary 42
❖ Methods in Java can be defined based on how they interact with the program's data and
the tasks they perform. We typically define methods in the following ways:
public static return-datatype methodName() {
public static void methodName() { // Method body
// Method body return result; // value that’s the same
} datatype as the return-datatype
}
System.out.println(max(3, 4)); - which prints the return value of the method call max(3, 4).
❖ If a method returns void, a call to the method must be a statement. For example, the
method println returns void. The following call is a statement:
System.out.println("Welcome to Java!");
45
Thanks!
Any questions?
For an in-depth understanding of Java, I highly recommend
referring to the textbooks. This slide provides a brief overview
and may not cover all the details you're eager to explore!