[Java method learning] method and its definition and call

method

A Java method is a collection of statements, which together perform a function.

The method is an ordered combination of steps to solve a class of problems

Method is contained in class or object

The method is created in the program and is referenced elsewhere

Principle of design method : The original intention of a method is a function block, which is a collection of sentence blocks that realize a certain function. When we design a method, it is best to maintain the atomicity of the method , that is, a method only completes one function, which is conducive to our later expansion.

Naming rules : first letter lowercase plus camel case principle

//mian 方法
public static void main(String[] args) {
    
    
    //这里的add为实际参数
    int sum = add(1,2);
    System.out.println(sum);
}
//加法
//这里的add为形式参数
public static int add (int a,int b){
    
    
    return a+b;
 /* 输出结果:3  */

Method definition

Java methods are similar to functions in other languages ​​(such as C language), which is a piece of code used to complete a specific function. In general, defining a method includes the following syntax:

The method contains a method header and a method body. Here are all the parts of a method:

1. Modifier: Modifier, which is optional, tells the compiler how to call the method. Defines the access type of the method.

2. Return value type: The method may return a value. renturnValueType is the data type of the return value of the method. Some methods perform the required operation, but do not return a value. In this case, returnValueType is the keyword void.

3. Method name: is the actual name of the method. The method name and the parameter list together form the method signature.

4. Parameter type: The parameter is like a placeholder. When the method is called, the value is passed to the parameter. This value is called an actual parameter or variable. The parameter list refers to the parameter type, order, and number of parameters of the method. The parameters are optional, and the method does not need to contain any parameters.

  • Formal parameters: used to receive data input from the outside world when the method is called. The above example

  • Argument: The data actually passed to the method when the method is called. The above example

5. Method body: The method body contains specific grammar, which defines the function of the method

修饰符 返回值类型 方法名(参数类型 参数名){
     
     
    ···
    方法体
    ···
    return 返回值;
}

6.return can return a value or end a method ----- generally use return 0 to end the method

Method call

Calling method: object name. method name (list of actual parameters)

Java supports two methods of invoking methods, depending on whether the method returns a value or not.

  • When a method returns a value, the method call is usually treated as a value. E.g:
int larger = max(10,20)
  • If the method return value is void, the method call must be a statement. E.g:
System.out.println("Hello World!");
/* println方法的语句(隐藏在java中)
public void println(int x) {
        synchronized (this) {
            print(x);
            newLine();
 */
public class Demo02 {
    
    
    public static void main(String[] args) {
    
    
        int t=max(501,501);
        System.out.println(t);
    }
    //比大小
    public static int max(int num1,int num2){
    
    
        //修饰符 修饰符 返回值类型 名字
        int result=0;
        if(num1==num2){
    
    
            System.out.println("num1=num2");
            return 0;
            /*return还可以用来结束方法,如果相等会输出两行:
            num1=num2
            0
             */
        }
        if(num1>num2){
    
    
             result = num1;
        }else {
    
    
             result =num2;
        }

        return result;//return一般写在方法最下面
    }


}

Expand understanding: value transfer (Java is all value transfer) and reference transfer

Guess you like

Origin blog.csdn.net/weixin_44302662/article/details/114648010