Dabai became a Java software siege lion on the twelfth day (basic knowledge of methods, grammar and meaning, method call rules in Java)

method

There should be such a mechanism in the Java language:

  • A function code only needs to be written once
  • To use this function, you only need to pass specific data to this function
  • After this function is completed, it returns a final result.

In this way, the code can be reused, improving the reusability of the code. [This is the "method" ]

Using this method is called "invoke/invoke"

The essence of the method

A method is a piece of code, and this piece of code can complete a specific function and can be reused.
Method, the corresponding English word: Method, is called function in C language: Function,

The method is defined in the class body, and multiple methods can be defined in a class. There is no order in which methods are written, and they can be arbitrary.

No more methods can be defined in the method body! ! ! ! ! !

Basic syntax of the method

1. Grammatical structure

[修饰符列表] 返回值类型 方法名(形式参数列表){
    
    
	方法体;
}

2. Explain the above grammatical structure:

2.1 About the modifier list

  • Optional, not required
  • Currently written as public static
  • If there is "static keyword" in the method modifier list, how to call this method?
    Class name. Method name (actual parameter list);

2.2 Return value type

1. What is the return value?

  • A method can complete a specific function. After this function is over, most of them need to return the final execution result. The execution result may be a specific existing data. And this specific data is the return value.

2. What type of return value?

  • The return value is a specific data, and the data has a type. What needs to be specified here is the specific type of the return value.

3. What types can be specified for the return value type?

  • Any type of java can be used, including basic data types and all reference data types.

4. It is also possible that this method does not return any data after the execution ends. Java stipulates that when a method does not return any data after the execution ends, the return value type position must be written: void keyword .

5. The return value type can be:

  • byte,short,int,long,float,double,boolean,char,string,void…

6. If the return value type is not void, it means that a specific value must be returned after the method is executed. When the method execution ends, if no data is returned, the compiler reports an error. How to return the value? How to write the code? " Return value; " and the data type of "value" must be consistent with the "return value type of the method", otherwise the compiler will report an error.

7. When the return type is void, you cannot write statements like " return value; " in the method body . But note that you can write statements like " return; ".

8. As long as the statement with the keyword is executed, the method where the return statement is located ends. [Not the end of JVM, but the end of the method where return is located]

2.3 Method name

  • As long as it is a valid identifier
  • The method name is best known
  • The method name is preferably a verb
  • The first letter of the method name is required to be lowercase, and the first letter of each word after it is uppercase

2.4 Formal parameter list: short form parameter

  • The formal parameters are local variables: int a; double b;...
  • The number of formal parameters can be 0~N
  • Use "commas" to separate multiple formal parameters
  • The decisive role of the formal parameter is the data type of the formal parameter, and the name of the formal parameter is the name of the local variable.
  • When the method is called, the actual data actually passed to the method is called: actual parameter, referred to as actual parameter
  • The actual parameter list and the formal parameter list must meet:
    1 the same number
    2 the same type corresponds to the same

2.5 The method body must be enclosed by curly braces, and the code in the method body has an order

Examples:

//public表示公开的
//class表示定义类
//MethodTest是一个类名
public class MethodTest{
    
     //表示定义一个公开的类,起名MethodTest,由于是公开的类,所以源文件名必须为:MethodTest
	//类体
	//类体中不能出现直接编写java语句,除声明变量之外
	//方法出现在类体当中

	//方法
	//public表示公开的
	//static表示静态的
	//void表示方法执行结束后不返回任何数据
	//main是方法名:主方法
	//(String[] args):形式参数列表,其中String[]是一种引用数据类型,args是一个局部变量的变量名
	//所以以下只有args这个局部变量的变量名是随意的
	//主方法就需要这样固定编写,这是程序的入口。【SUN公司规定的,必须这样写】
	public static void main(String[] args){
    
    

	//这里的程序是一定会执行的
	//main方法是JVM负责调用的,是一个入口的位置
	//从这里作为起点开始执行程序
	//既然是这样,我们就可以在这里编写java语句来调用其他方法
	//调用MethodTest的sum方法,传递两个实参
	MethodTest.sum(10,20);//(10,20)实参列表

	//注意:方法体当中的代码是有顺序的,遵循自上而下的顺序以此执行
	//上一行代码的程序执行不结束,无法执行下一行代码。	
	
	//一个方法可以被重复使用,重复调用
	int a=100;
	MethodTest.sum(a,500);// (a,500)实参列表

	//再次调用方法
	int k=90;
	int f=10;
	MethodTest.sum(k,f);//(k,f)实参列表
	}

	//自定义方法,不是程序的入口
	//方法的作用:计算两个int类型数据的和,不要求返回结果,但是要求将结果直接输出到控制台
	//修饰符列表:public static
	//返回值类型:void
	//方法名:sum
	//形式参数列表:(int x,int y)
	//方法体:主要任务是求和之后输出计算结果
	public static void sum(int x, int y){
    
    
		System.out.println(x+'+'+ y +'='+ (x+y))
	}
}

3. How to call the method?

The method will not be executed if it is defined and not called, it will be executed only when it is called.
Grammar rules: "Static in the modifier list of a method"

  • Class name. Method name (list of actual parameters); <This is a java statement that means calling a certain method of a certain class and passing such actual parameters. >

Guess you like

Origin blog.csdn.net/qq2632246528/article/details/112714035