Home >Java >JavaBase >what is java method

what is java method

angryTom
angryTomOriginal
2019-11-13 11:15:303397browse

what is java method

What is a java method

In our daily life, a method can be understood as doing something , and the solution adopted.

In java, a method is a method used to solve something or implement a certain function.

In the process of method implementation, there will be many statements used to complete some meaningful functions - usually processing text, controlling input or calculating values.

We can execute (or call) the method in the program by referencing the method name and required parameters in the program code. Methods generally have a return value, which is used as the result of the transaction.

The syntax format of the method

修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2,......){ 
执行语句
……… 
return 返回值;
}

The specific description of the above syntax format is as follows:

1) Modifier: Modification of the method There are many modifiers, including those that limit access rights, static modifiers, and final modifiers, final, etc. These modifiers will be gradually introduced in the subsequent learning process

2) Return value type: Used to limit the data type of the method return value

3) Parameter type: Used to limit the data type of the parameters passed in when calling the method

4) Parameter name: It is a variable used to receive The data passed in when calling the method

5) return keyword: used to end the method and return the value of the specified type of the method

6) Return value: the value returned by the return statement, this value Will be returned to the caller

It should be noted that the "parameter type parameter name 1, parameter type parameter name 2" in the method is called the parameter list, which is used to describe the method when it is called. Parameters that need to be received. If the method does not need to receive any parameters, the parameter list is empty, that is, nothing is written in (). The return value of the method must be the return value type declared by the method. If there is no return value in the method, the return value type must be declared as void. At this time, the return statement in the method can be omitted.

Next, a case is used to demonstrate the definition and use of the method, as follows.

public class Method {
public static void main(String[] args) {
int area = getArea(3, 5); // 调用 getArea方法
System.out.println(" The area is " + area);
}
 
// 下面定义了一个求矩形面积的方法,接收两个参数,其中x为高,y为宽
public static int getArea(int x, int y) {
int temp = x * y; // 使用变量temp记住运算结果
return temp; // 将变量temp的值返回
}
}

In the above code, a getArea() method is defined to find the area of ​​the rectangle. The parameters x and y are used to receive the height and width passed in when calling the method, and the return statement is used to return the calculation. The resulting area. In the main() method, get the area of ​​the rectangle by calling the getArea() method, and print the result.

Method calling diagram

Next, an illustration is used to demonstrate the entire calling process of the getArea() method, as shown in the figure below.

what is java method

As can be seen from the above figure, during the running of the program, the parameters x and y are equivalent to two variables defined in memory. When the getArea() method is called, the incoming parameters 3 and 5 are assigned to variables x and y respectively, and the result of x*y is returned through the return statement. The entire method calling process ends and the variables x and y are released.

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of what is java method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn