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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.