Use of Java methods 1

1, Concept and application of method  

(1) What is the method

Many programming languages (like C and C + +) use the term function to describe named subroutines, but in Java, the term method is often used to mean "the way to do something". In fact, it doesn't hurt to continue to think of it as a function. Although this is only a difference in terms, we still use the idiomatic usage of Java, that is, the term "method"

(2) I. significance of the method  

1. It is a modular organization code (when the code scale is complex)

2. The code can be reused, and a copy of the code can be used in multiple locations

3. Make the code easier to understand

4. Call the existing method directly without repeating

(3) Definition of method

1. Basic grammar

  2. Precautions

(1) At this stage, the modification of the method adopts ----- > public static fixed collocation

(2) , [] internal representations are optional

(3) , methods must be defined in the class

(4),   When defining a method, there can be no parameters. If necessary, specify the type of each parameter

(5) . when defining a method, the return value can also be absent, such as the main method. If there is no return value, the return value type must be written as void

(6) . method definitions cannot be nested

(7) . parameters during method definition are called "formal parameters", and parameters during method call are called "arguments"

(8) . the method must be defined in the class, and the position of the method definition can be above or below the calling position

(9) There is no such concept as "function declaration" in Java

3. Code example

(1) . implement a function to detect whether a year is a leap year

 public static boolean isLeapYear(int year) {
       if ((0 == year % 4 && 0 != year % 100) || year % 400 == 0) {
           return true;
           return false;

    public static void main(String[] args) {
        int year=2001;
        boolean isLeap= isLeapYear(year);

(2),   Implement a method of adding two integers

 public static void main(String[] args) {
        int a=10;
        int b=20;
        int num=add(a,b);
    public static int add(int x,int y){
        return x+y;

(4) . execution process of method call

1. Method call procedure

(1) . process

Call the method -- > pass the parameter -- > find the method address -- > execute the method body of the called method -- > return after the called method ends -- > return to the calling method and continue to execute

(2) , illustration

  (3) Code example 1 (calculate the addition of two integers)  

public static void main(String[] args) {
        int a =10;
        int b=20;
        System.out.println("Before the method is called for the first time");
        int ret=add(a,b);
        System.out.println("After the first method call");

        System.out.println("Before the second method call");
        System.out.println("After the second method call");
    public static int add(int x,int y){
        System.out.println("In calling method x = " + x +  " y = " + y);
        return x+y;

(4) Code example 2(   1! + 2! + 3! + 4! + 5!)

public static void main(String[] args) {
        int sum=0;
       for (int i=1;i<=5;i++){
           sum +=fac(i);
    public static int fac(int n){
        System.out.println("calculation n Factorial of n! = " + n);
        int result=1;
        for (int i=1;i<=n;i++){
            result *=i;
        return result;

(5) Relationship between, arguments and formal parameters

1. Formal parameter

The formal parameter of the method in Java is equivalent to the argument n in the sum function, which is used to receive the value passed by the sum function when calling. The name of a formal parameter can be chosen at will and has no impact on the method. A formal parameter is just a variable that the method needs to use when defining, which is used to save the value passed by the method when calling.

2. Argument

Actual parameters.

Note: in Java, the value of the argument is always copied to the formal parameter. The formal parameter and the argument are essentially two entities

3. Stack frame

(1) Each method must have its own running environment when running. The professional term of the running environment is stack frame.

(2) Stack frame is actually a data structure that organizes some information related to the method needed by the method runtime, such as formal parameters, local variables, etc.

(3) . the stack frame is created with the execution of the method and destroyed with the end of the method. The lifetime of the stack frame is the same as that of the method.

4. Illustration

5. Code example

 public static void swap(int a,int b){
        System.out.println("swap Before exchange:");
        int temp =a;
        System.out.println("swap After exchange:");

    public static void main(String[] args) {
        int left=10;
        int right=20;
        System.out.println("main Before exchange");
        System.out.println("main After exchange:");

  6. Operation results

  7. There is a problem (why the number does not change after main exchange)

(1) Dynamic diagram (stack frame 1 is before exchange and stack frame 2 is after exchange)

  (2) Static diagram

  (6) Methods without return value

The return value of the method is optional. Sometimes it can not be. If not, the return value type must be written as void

Tags: Java

Posted on Sat, 25 Sep 2021 14:26:07 -0400 by keiron77