คืนค่าการตั้งค่าทั้งหมด
คุณแน่ใจว่าต้องการคืนค่าการตั้งค่าทั้งหมด ?
ลำดับตอนที่ #8 : Java III - Method
Using
Methods
A method is a section of code to which we assign a
specific task. From here, we will be splitting up the main method into smaller methods. When Java
files are executed, the main method is automatically
executed. When the program is divided up, the main method instructs each of the
others to execute their individual tasks.
Code
with Multiple Methods
The code on the left is disorganized
and hard to understand because everything is written in the main method. On the
other hand, the code on the right is organized and easy to understand since
it's split into smaller methods. By splitting our program into smaller methods,
we can give each of them separate and specific roles.
Calling
Methods
You can think of a method as a collection of statements that
are grouped together to perform a specific operation. In the left
figure, we defined a method called hello,
and called the hello method from the main method. It’s ok if you don’t quite
understand this process yet. Check the flow on the right and try using methods
in the next exercise.
Method
Definitions
First, let’s take a look at method
definitions. For now, remember public static void as
a fixed phrase. Later, we will learn about the ()
at the end of each method name. We write the code for each method inside the
block, the part between { and }. We should give each method a name that makes it
easy to tell what it does.
We define methods inside a class. As shown below, the hello method is defined in the block ({}) of the Main class.
If you define a method outside of a class, there will be an error, so be
careful.
Calling
Methods
To call a method, just write methodName(). Don't forget to put the () when you call the method. We'll learn more
about this later.
Using
Arguments
Arguments
An argument is like an additional piece of data
that you give to the method. When you call a method, you can pass an argument
so that you can use that value inside the method.
Receiving
Arguments
In Java, you have to specify in the
definition of a method that it can handle arguments. First, we need to specify a variable (parameter) for handling an
argument. You can set a parameter inside the ()
at the end of public static void methodName().
Remember to include the data type for each parameter just like when declaring a
variable.
Passing
Arguments
To pass an argument to a method, call the method like this: methodName(argument). An argument passed to a method is assigned to the variable specified in the definition as its parameter, and that variable can be used within the method.
Multiple
Arguments
Methods
with Multiple Arguments
Methods can receive multiple
arguments. In the definition, to set multiple parameters, separate them with
commas ( , ) . Parameters can be in any
order and of any data type. When calling a method, arguments should be in order
from left to right as follows: firstArgument,
secondArgument, …
Passing
Multiple Arguments
Let's see how to pass multiple
arguments to a method. Take a look at the example below.
The
Order of Passing Arguments
Though the order of the parameters can
be arbitrary, when calling a method, you have to pass the arguments in the same
order as the parameters. Like in the example below, if you try to use an
integer argument where a String type parameter is defined, it causes an error
(the order of passing arguments is reversed).
Method
Return Values
Return
Values
A return value allows us to use the result of a
method where it was called. Think of a method like a factory. The factory gets
materials (arguments) from the client (main method), does the predetermined
manufacturing (operations), and returns the finished product to the client.
It's the return value that corresponds to the finished product.
Method
Return Values
Methods can send a return value back
to the caller with return. When there is a
return value, you need to set its return type (the data type of the return value). To do this,
change the void part of public static void. As shown below, the return
value of the add method is an integer, so we
set the data type like public static int.
Return
Value Example
After a method is executed, the return
value (if it exists) will replace the caller of the method. This way, we can
assign the caller of the method to a variable (see the example below). This can
be very useful.
Note: "Caller" refers to the operation
that called the method.
Void
So what is void
from the fixed phrase public static void? The
"void" means no
return value. As shown in the example below, the hello method doesn't
have a return value. We use
void to specify that a method has no return value.
Method
Overloading
Identically
Named Methods
Defining more than one method with the
same name is not allowed in Java. This is because if the computer recognizes
multiple methods with the same name, it cannot determine which method to call.
Overloading
However, if the arguments of two
identically named methods differ in number or data
type, they can have the same name. Even if there are multiple methods
with the same name, the computer can tell which one to call when argument lists
are unique. Defining multiple methods with the same name is called overloading.
Combining
Methods
Calling
a Method from Another
We can call a method not only from the
main method but from other methods as well. As components of the larger
program, methods help each other to make up one whole program.
Combining
Methods
Let's call one method from another. To
calculate (a + b) / 2, we can use the add
method inside the average method, which
returns the average of two numbers. Note that (double)
is a data type conversion we learned in the Java I lesson.
Boolean
Return Values
Finally, let's create a method that
returns a boolean value. As shown in the example below, we first define the
method as a boolean data type. The isEven method checks if the value of the argument
is an even number and returns true if it is even, and false otherwise.
Example:
Method Returns Boolean Value
Let's try using the isEven method, which returns a boolean value. Take
a look at the example below. Especially the output!
ความคิดเห็น