[02 Java Object Oriented Programming: 2. Member method]

Posted by John_S on Tue, 01 Mar 2022 07:06:01 +0100

2. Membership method

2.1 format

 class Class name {
	Return value type member method name(parameter list ) {
	Member method;
	}
 }
//Class name: capitalize each word
//Method name: capitalize the first letter of each word from the second word.

2.2 return value type

Concept: return value mainly refers to the data content returned from the method body to the method body.

  • Return value type mainly refers to the data type of return value, which can be basic data type or reference data type.
  • When the returned data content is 66, the return value type can be written as int
  • Use the return keyword in the method body to return specific data content and end the current method. When the returned data content is 66, write return 66 in the method body
  • When the method does not need to return any data content, the return value type can be written as void.

2.3 formal parameter list

Concept: formal parameters are mainly used to bring the data content outside the method body into the method body.

**Format: * * syntax format is as follows:
(data type parameter variable name 1, data type parameter variable name 2,...)

When the data content brought in is "hello", write String s in the formal parameter list

When the data content brought in is 66 and "hello", the formal parameter list can be written as int i, String s

If this method does not need to bring in any data content, nothing can be written in the position of the formal parameter list.

2.4 method body

Return value type member method name(parameter list ) {
	Member method;
	}
//Custom member method to print all member variables
void show() {
    //Both member methods and member variables belong to members within the class, so you can directly access member variables without reference Prefix of
    System.out.println("I am"+name+",I this year"+age);
}

2.5 method call

Reference variable name Member method name (argument list);

The actual parameter list is mainly used to initialize the formal parameter list, so the number, type and order of parameters should be completely consistent.

Actual parameters can pass direct quantities, variables, expressions, method calls, etc.

public class Point {
    int x;	//Abscissa 											// x. Y is the member variable
    int y;	//Ordinate
    
    //3. Define a member method to print member variables
    void show() {
        System.out.println("The abscissa is:"+x+",The ordinate is:"+y);
    }
    
    //The user-defined member method modifies the name to the value specified by the parameter
    void setName(String s) {
        name=s;
    }
    //The user-defined member method modifies the age to the value specified by the parameter
    void setAge(int i) {
        age=i;
    }
    //The user-defined member method can modify the name and age to the value specified by the parameter at the same time
    void setNameAge(String s,int i) {							//The in () is a formal parameter
        name=s;													//s. I is a local variable
        age=i;
    }
    
    public static void main(String[] args) {
        
        //1. Declare that the reference of Point type points to the object of Point
        Point P=new Point();
        //Print the value of the member variable
        //System.out.println("abscissa:" + P.x + ", ordinate:" + p.y ");
        P.show();
        
        System.out.println("------------------------------")
        //2. After modifying the value
        P.x=3;
        P.y=5;
        //System.out.println("abscissa:" + P.x + ", ordinate:" + p.y ");
        
        //The essence of calling a method is to jump to execute the method body according to the method name, and then jump back
        P.show;
        
        //The modification of member variables is realized through the call of member methods
        P.setName("Ummmm ");
        P.setAge(25);
        P.show();	//Ummmm  	 twenty-five
        
         //The modification of member variables is realized through the call of member methods
         //P.setNameAge("ah Qi", 25); 						// () arguments
        int ia=25;
        P.setNameAge("Ah"+"seven",ia);
        P.show();	//Ummmm  	 twenty-five
    }    
}

2.6 variable length parameters

Return value type method name (parameter type... Parameter name)

The number of parameters of the specified type in the method parameter part can be changed, that is, 0~n

At most one variable length parameter can be declared in the parameter list of a method, and it needs to be placed at the end of the parameter list.

//The user-defined member method realizes the use of variable length parameters, 	 It can be regarded as a one-dimensional array
void showArgument(int num,String... args) {
    System.out.println("num="+num);
    for(int i=0;i<args.length;i++) {
        System.out.println("The first"+(i+1)+"The first parameter is:"+arg[i]);
    }
   
    //The printing of variable length parameters is realized through the call of member methods
    p.showArgument(12);		//12
    p.showArgument(15,"Parameter 1");
    p.showArgument(15,"Parameter 1","Parameter 2");
}
public class Point {
    //Member variable
    int x;
    int y;
    //The user-defined member method realizes the use of variable length parameters
    void showArgument(int... args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println("Subscript" + i + "The element of is" + args[i]);
        }
    }
    public static void main(String[] args) {
        //Declaring a Point type reference points to an object of type Point
        Point p=new Point();
        p.showArgument(1,2,3);
    }
}

Note: methods and methods are parallel

7.2 member variables and local variables

  1. Defined location:

    The local variable is inside the method body or ();

    The member variable is outside the method and written directly in the class

  2. Scope of action:

    Local variables can only be used inside methods;

    Member variables can be used by the whole class;

  3. Default:

    Local variables have no default value. If you want to use them, you need to assign them manually

    If the member variable is not assigned, it has a default value. The rule is the same as that of the array

  4. Memory location:

    Local variables are located in stack memory; Member variables are in heap memory

  5. Life cycle:

    Local variables are born with the method entering the stack and disappear with the method leaving the stack

    Member variables are born with the creation of objects and disappear with the garbage collection of objects

2.8 transmission of method parameters

2.8.1 process

int max(int ia, int ib) {..........} / / member method

int a = 5; int b=6; int res = m.max(a,b); //main method

  1. Allocate space and initialize variables a, b and res in the main method.
  2. Call the max method to allocate space for the formal parameter variables ia and ib of the max method.
  3. Assign the value of the argument variable to the memory space of the formal parameter variable.
  4. The max method returns after running, and the formal parameter variable space is released.
  5. The res variable in the main method gets the return value of the max method.
  6. Release the memory space of related variables after the main method.

2.8.2 precautions

  1. When a variable of basic data type is passed as a parameter of a method, the change of the value of the formal parameter variable usually does not affect the value of the actual parameter variable, because the two variables have their own independent memory space;
  2. When a variable of reference data type is passed as a parameter of a method, the change of the pointing content of the formal parameter variable will affect the value of the pointing content of the actual parameter variable, because the two variables point to the same memory space
  3. When a variable referring to a data type is passed as a parameter of a method, if the formal parameter variable changes its direction and then changes the specified content, the change of the pointing content of the argument variable will not be affected, because the two variables point to different memory spaces.
public class ArgumentTest {
    //The custom member method prints the integer data passed in by the parameter
    //ia=ib=10
    void show1(int ia) {
        ia=200;
        System.out.println("ia Value of=" + ia);   //10    200
    }
    //The custom member method prints the array data passed in by the parameter
    //Reference data type. The address is stored in memory
    void show2(int[] arr1) {
        arr1=new int[2];
        arr1[0]=200;
        System.out.println("show Method: arr1[0]The value of is"+arr1[0]);  //200
    }

    public static void main(String[] args) {
        //1. Declare that the reference of Argument type points to the object of this type
        ArgumentTest at = new ArgumentTest();
        //2. Use reference variables to call member methods
        int ib = 10;
        at.show1(ib);
        System.out.println("ib Value of="+ib);     //10
        System.out.println("------------------------------------");
        //3. Call the show method to test
        int[] arr2=new int[]{10,20};
        at.show2(arr2);
        System.out.println("show Method: arr2[0]The value of is"+arr2[0]);  //200   10
    }
}

Stack area of memory structure

  • Stack is used to store all local variables during program operation. A running Java program will have multiple method calls from the beginning to the end.
  • The JVM will allocate a corresponding space in the stack for each method call, which is called the stack frame of the method. A stack frame corresponds to a method being called. The stack frame stores the parameters, local variables and other data of the method.
  • When a method call is completed, its corresponding stack frame will be cleared.

Related concepts of parameter transmission

  • Parameters are divided into formal parameters and arguments. The parameters when defining a method are called formal parameters, and the parameters passed when calling a method are called arguments.
  • When calling a method, the actual parameter is passed to the formal parameter by value passing. In fact, the formal parameter is used inside the method.
  • The so-called value transfer is to transfer the value of the parameter when the parameter is a basic type. For example, when i=10 is passed, 10 is assigned to the formal parameter. When the parameter is an object, the value of the object is passed, that is, the address of the object is assigned to the formal parameter

Topics: Java