SoFunction
Updated on 2025-03-06

Detailed explanation of the call relationship example of Java parsing method

1. Definition of method

The method is to encapsulate the code with repeated functions into an independent piece of code, and improve the reusability of the code (reduce code duplication) by calling methods (actually).

Each method can only complete one function.

2. Declaration format of method

[Modifier 1, Modifier 2] Return value type Method name (parameter type Formal parameter 1, parameter type Formal parameter 2,…) {

Execute statement block;
return return value;

}

example:

boolean isLeapYear (int year){  //Define the method    boolean isLeap = false;
        if(year%4==0 && year%100 != 0 || year%400 != 0){
            isLeap = true;
        }
    return isLeap;
}
public class MethodDemo01 {
    //Extract a method, calculate the sum of two numbers and return    public static int addNum(int a,int b){
        int sum = a +b;
        return sum;
    }
    //Extract a method, calculate the sum of three numbers and print it    public static void addNum2(int a,int b,int c){
        int sum = a +b +c;
        (sum);
    }
    public static void main(String[] args) {
        //Calculate method 1 and calculate the sum of 1+2        int sum1 = addNum(1,2);
        (sum1);

        //Calculate method 1 and calculate the sum of 3+4        (addNum(3,4));

        //Calculate the method, calculate the sum of 5+6+7 and print it        addNum2(5,6,7);
    }
}
  • Return value type: The type of the method output data (if the method does not return value, it is represented by the keyword void)
  • Method name: Similar to variable name, you can customize the method name (need to comply with Java identifier rules)
  • Formal parameters: variable (used to store data input from calling method)
  • Actual parameters: data entered by calling the method.
  • Return value: The data returned by the call method.

3. Calling of methods

int year= 2020;
boolean leapYear =isLeapYear(year); //Calling methodif(leapYear){
    (year +"It's a leap year!")
}
year = 2052;
leapYear =isLeapYear(year);//Calling methodif(leapYear){
    (year +"It's a leap year!")
}

4. Special method: program entry main method

The main method is a special method, which is the entry method of a Java program. When executing a Java program, the JVM will call the main method.

public static void main (String[]args){}

V. Local variables of the method

Local variables refer to variables declared in methods, and their scope of action is limited to methods.

Local variables must be initialized before use. (Variables defined in the main method are also local variables)

boolean isLeapYear (int year){
    boolean isLeap = false;//Declare a local variable and initialize it        if(year%4==0 && year%100 != 0 || year%400 != 0){
            isLeap = true;
        }
    return isLeap;
}
public classA{
    public static void main (String[]args){
    int i =6; //Integer (real argument, local variable)    int [] array = {1,2,3}; //Array (real arguments, local variables, address of the passed array)    test(i,array);
    (i+""+array[0]);
    }
    public static void test(int i,int []array){  //Formal parameters        i += 3;
        array[0] += 3;
    }
}
//Output result: 6 4

6. Static variables of the class

Static variables (also called class variables) are variables declared using static keywords that belong to the class itself, not any specific instance of the class (static variables do not need to be initialized before use.)

public class HellWorld{
    public static boolean isLeap;//Global variables (default automation)    
    public static void method01(){ //
        isLeap = true;//Share global variables    }
    public static void method02(){
        isLeap = true;//Share global variables    }
}

7. Static methods of classes

A static method is a method defined using the static keyword. A static method can access static variables and static methods of a class, but cannot access instance methods and instance variables of a class.

8. Transmission of methods

  • Real and formal parameters
    Real argument: variable or constant value input by calling method

Formal parameter: Data type variable defined in the method.

  • Value pass
    When the actual parameter passes data to the formal parameter, the change of the formal parameter does not affect the value of the original actual parameter, that is, the value transfer will only change the formal parameter and will not change the actual parameter. (Basic data type)

  • Reference pass
    Reference pass: refers to passing the address of the actual parameter to the function when calling a function, so that the modification of the parameters in the function will affect the actual parameters. (Reference data type)

The reference data is passed not the real data address, but the referenced address in the heap memory. No matter how the data passed by the reference changes, it is only the referenced address in the heap memory. The data address of the original data group has not been changed. Changing that part of the value is only the address copied to the actual parameter, and it does not change the original address.

  • Summarize
    In Java, parameter passing parameters essentially only have value passing.

When passing values, the change of formal parameters will not affect the actual parameters.

In a function, only by modifying the object content pointed to by the actual parameter, that is, referring to the address in the heap memory, will the actual parameter be affected.

public classA{
    public static void main (String[]args){
    int i =6; //Integer (real argument, local variable)    int [] array = {1,2,3}; //Array (real arguments, local variables, address of the passed array)    test(i,array);
    (i+""+array[0]);
    }
    public static void test(int i,int []array){//Formal parameters        i += 3;
        array[0] += 3;
    }
}
//Output result: 6 4

9. Method overloading

Method overloading refers to the fact that there is more than one method with the same name in the same class, but the number of parameters or parameter types of the method are different (irrelevant to the return value of the method)

Method overloading is for a class.

 public static int addNum(int a,int b){
        int sum = a +b;
        return sum;
    }
    //Extract a method, calculate the sum of three numbers and print it public static int addNum2(int a,int b,int c){
        int sum = a +b +c;
        return sum;
    }

For example, the following are all overloading methods

  • void show (int a,char b,double c);

  • void show (int a,double b,char c);

  • void show (int a,int b);

  • void show (int a);

  • double show(int a,char b,double c);

Summarize

This is the end of this article about the call relationship of Java parsing methods. For more related contents of Java method call relationships, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!