Java basic grammar learning day07---beginners must read detailed explanation

1. Method

1.1 Methods (functions, procedures)

Various languages ​​have the concept of methods (some languages ​​call them functions or procedures).
Methods are used to encapsulate a specific logical function
such as: perform calculations or operations;
methods can be called repeatedly in the program;
methods can reduce code duplication, Ease of program maintenance.

1.2 Definition of method

1.2.1 Define the functionality of methods (functions, procedures)

Methods are used to encapsulate a specific functionality.
Five elements that define a method: modifier, return value type, method name, parameter list, method body;
insert image description here

1.2.2 Define parameters and return values

  • The parameters of the method refer to: the data that is passed to the method when it is called and needs to be processed by the method;
  • The method can have parameters or no parameters, and having parameters can make the method processing more flexible;
  • When a method is defined, it is necessary to declare the parameter variables required by the method.
  • When the method is called, the actual parameter value is passed to the parameter variable of the method. It must be ensured that the type and number of passed parameters conform to the declaration of the method.
    void say() {
    
       }
    void say( String name ) {
    
       }
    int   sum ( int   num1 , int   num2 ) {
    
       }
方法调用结束后可以返回一个数据,称之为返回值。
方法在声明时必须指定返回值的类型。
若方法不需要返回数据,将返回值类型声明为void。
若方法需要返回数据,将返回值类型声明为特定数据类型。

1.3 Method call

insert image description here
method call

package com.tedu.method;
//测试方法
public class Test4_Method {
    
    
    public static void main(String[] args) {
    
    
       System.out.println(1);
       method1();//方法调用 1 3 2
       System.out.println(2);
    }
    //创建method1方法,这个方法不调用是不会执行的
    public static void method1() {
    
    
       System.out.println(3);
    }
}

method parameters

package com.tedu.method;
//测试方法
public class Test4_Method {
    
    

    public static void main(String[] args) {
    
    

       System.out.println(1);
       method2(10);//方法的参数 1  100 2
       System.out.println(2);

    }

    //创建method2(),并且把参数传递给a

    //在创建方法时需要指定参数类型和参数名

    public static void method2(int a) {
    
    

       System.out.println(a*a);

    }
}

Exercise 3: Methods with return values

package com.tedu.method;

//测试方法

public class Test4_Method {
    
    
public static void main(String[] args) {
    
    
       System.out.println(1);
       int result = method3(100);//有返回值的方法
       System.out.println(result);
       System.out.println(2);
    }
    //创建method3()

    //方法的返回值要和调用位置保持一致

    public static int method3(int num) {
    
    

//通过return关键字,把结果返回给调用位置的result保存

       return num*num;

    }

}

1.3.1 return statement

It can be returned through the return statement. The function of the return statement is to end the method and return the data to the caller.

return  num1 +num2 ;
return语句返回该表达式的值。
return ;
对于返回值为void的方法也可以使用return语句,此时该语句的作用仅仅在于结束方法调用。

1.3.2 Parameter passing when calling a method

定义方法:
public  static  int  sum ( int  num1 , int num2 ){
    
      }
main方法中调用:int result = sum(56); 
                            int  a = 50, b = 60;
                            int  result = sum ( a , b );
定义方法:
public  static  void  sayHi(String name) {
    
      }
main方法中调用:sayHi(“wkj”);
                            sayHi(”zhangsan ”);

public static int max(int a, int b) {
    
     … … … }
int a = 5; int b=6;
int myMax = max(a,b);

1) 为main方法中的变量a、b、myMax
    分配空间并赋值。
2) 调用方法max,为max方法的参数
    变量a,b分配空间。
3) 将调用值传递到参数变量中。
4) max方法运行完返回,参数变量空间
    释放。
5) main方法中的myMax变量得到返回值。

Guess you like

Origin blog.csdn.net/weixin_43639180/article/details/117526944
Recommended