Location>code7788 >text

Java method

Popularity:647 ℃/2024-08-11 15:50:47

I. Definition of methodology

A method is a way to encapsulate duplicative code into a single piece of code by calling the method(Role) Improve code reusability (reduce code duplication)

Each method can accomplish only one function.

II. Method declaration format

[modifier 1, modifier 2] return value type method name (parameter type formal parameter 1, parameter type formal parameter 2, ...) {

    Execute the statement block.
    return Return value.

}

Example:

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

        //call method 1, calculate the sum of 3+4
        (addNum(3,4)); //Call method 1, calculate the sum of 3+4.

        //call the method, calculate the sum of 5+6+7 and print it
        addNum2(5,6,7); //Call method, calculate sum of 5+6+7 and print.
    }
}
  • Return value type: the type of data output by the method (if the method does not have a return value, it is indicated by the keyword void)
  • Method name: similar to variable name, can be customized method name (need to comply with Java identifier rules)
  • Formal parameters: variables (used to store data entered by the calling method)
  • Actual parameter: the data entered by the calling method.
  • Return value: the data returned by the called method.

III. Invocation of methods

int year= 2020;
boolean leapYear = isLeapYear(year); // call method
if(leapYear){
    (year + "It's a leap year!")
}
year = 2052; //Call method if(leapYear){ (year + "It's a leap year!")
leapYear = isLeapYear(year); // call method
if(leapYear){
    (year + "It's a leap year!")
}

IV. Special methods: program entry main method

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

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

V. Local Variables for Methods

A local variable is a variable declared in a method whose scope is limited to the method

Local variables before useMust be initialized. (Variables defined in the main method are also localized)

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; //Declares a local variable and initializes it.
        }
    return isLeap; }
}
public classA{
    public static void main (String[]args){
    int i = 6; //integer (real parameter, local variable)
    int [] array = {1,2,3}; //array (real parameter, local variable, pass address of array)
    test(i,array).
    (i+""+array[0]);
    }
    public static void test(int i,int []array){ //formal parameter
        array[0] += 3; } public static void test(i,int []array){ //formal parameter
        array[0] += 3; }
    }
}
// Output: 6 4

VI. Static variables of classes

Static variables (also known as class variables) are variables declared using the static keyword that belong to the class itself and not to any particular instance of the class (static variables do not need to be initialized before they can be used.)

public class HellWorld{
    public static boolean isLeap;//global variable(Default Automation)
    
    public static void method01(){ //
        isLeap = true;//共享global variable
    }
    public static void method02(){
        isLeap = true;//共享global variable
    }
}

VII. Static methods of classes

A static method is a method defined using the static keyword. A static method has access to the class's static variables and static methods, but not to the class's instance methods and instance variables.

VIII. Passing references to methods

1. Real and formal parameters

Real parameter: the value of the variable or constant entered by the calling method

Formal parameter: a variable of the data type defined in the method.

2. Value transfer

When a real parameter passes data to a formal parameter, the change in the formal parameter does not affect the value of the original real parameter, i.e., value passing only changes the formal parameter, not the real parameter. (Basic data types)

3. Reference transfer

Pass by reference: It means that the address of the actual parameter is passed to the function when it is called, so that any modification of the parameter in the function will affect the actual parameter. (referencing data types)

The data passed by reference is not the real data address, it is the address in the heap memory of the reference, no matter how the data passed by reference is changed, it is only the address in the heap memory of the reference, and the data address of the original data set has not been changed, and the change of that part of the value is only the address copied by the formal parameter to be used by the real parameter, and it is not the change of the original address.

4. Summary

  1. In Java, parameter passing is essentially only value passing.

  2. When passing values, a change in a formal parameter does not affect a real parameter.

In a function, only modifying the contents of the object pointed to by a real reference, that is, referencing an address in heap memory, affects the real reference

public classA{
    public static void main (String[]args){
    int i = 6; //integer (real parameter, local variable)
    int [] array = {1,2,3}; //array (real parameter, local variable, pass address of array)
    test(i,array).
    (i+""+array[0]);
    }
    public static void test(int i,int []array){// Formal parameter
        i += 3;
        array[0] += 3; }
    }
}
// Output result: 6 4

IX. Method overloading

Method overloading means that there is more than one method of the same name in the same class, but the method'sThe number or type of parameters varies(independent of 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
 public static int addNum2(int a,int b,int c){
        int sum = a +b +c;
        return sum;
    }

For example, the following are all method overloads

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);

Ten, Java memory division