1. There are parameters and return values
Definition of methods with parameters and return values:
Syntax: access modifier return value type method name (formal parameter list) {
Method body;
return expression;
}
Calls to methods with parameters and return values:
Syntax: data type variable name = method name (actual parameter list);
The return value of a method is also called the method value, which is implemented by the return statement in the called method.
Syntax: return; Or return expression;
The return statement is a jump statement. Its main functions are as follows:
- Ends the execution of the called method and returns to the calling method.
- Returns the value of an expression that will be used as the return value of the method during the method call.
The code in the method body after the return statement will no longer be executed
- Timing of return value transfer: when the called method returns to the calling method after execution.
- The process of return value transfer: the variable receiving the return value = the value of the expression after the return statement.
- Meaning of return value transfer: a way to transfer data from the called method to the calling method.
be careful:
- Methods can have no return value or have a return value, but there can only be one return value at most.
- Methods can be variables, constants, and expressions.
- In the method without return value, there may be no return statement in the method body. When the called method is executed, it will automatically return to the calling method. You can also use "return;" Statement to end the execution of the called method and return to the calling method
- In a method with a return value, the method body must contain a "return expression;" sentence.
- The data type of the return value in the method body must match the return value type specified when defined in the method.
- When using conditional structures, you must ensure that each branch contains a return value.
2. Data type of parameter
Java data types are divided into value types and reference types, which are different when used as parameters.
2.1 parameters of value type
When the parameter of a method is a value type, because the value stored in the value type variable is the value of the variable, the formal parameters and arguments will copy the variable value. When the code in the method completes the change of the formal parameter value, the argument will not change synchronously
Example: modify the example, define the method to realize the salary increase function, and the salary increase of each employee is 20%.
//Define the method to realize the salary increase function of employees public void changeValue(int val) { System.out.println("Before method change" + num); val++; System.out.println("After changes within the method" + num); }
MethodParameterDemo mpd = new MethodParameterDemo(); nt num = 100; System.out.println("Before calling method" + num); mpd.changeValue(num); System.out.println("After calling the method" + num);
For parameters of value type, parameter changes will not affect the arguments.
Use the return keyword to return the changed formal parameters.
2.2} parameters of reference type
- The reference type variable in Java stores the memory address of the object. The properties and methods of the object can be accessed through the memory address.
- When the reference type variables are assigned to each other, the object address is copied, not the object attribute.
Example:
public class Person { int id; String name; public Person() { } public Person(int id, String name) { this.id = id; this.name = name; } }
public static void main(String[] args) { Person p1 = new Person(1, "zhangsan"); Person p2 = p1; System.out.printf("Before change p1 Your name is%s,p2 Your name is%s\n", p1.name, p2.name); p2.name = "lisi"; System.out.printf("After change p1 Your name is%s,p2 Your name is%s\n", p1.name, p2.name); }
The Person type is a reference type. The memory address is stored in the variable p1. During assignment, the addresses in the two variables are the same and point to the same object.
When we use the reference type as a method parameter, because the data type of the parameter is the reference type and the memory address of the object is stored in the variable, the formal parameter and the argument are actually the same object. Changing the value of the argument will also change the formal parameter.
Example:
public void changeName(Person p){ if(p != null){ p.name = " Miyagi Poetry "; } }
Person p1 = new Person(1, " Cheng Xuyuan "); System.out.println(" Before calling method p1 Your name is "+p1.name); MethodParameterDemo2 mpd2 = new MethodParameterDemo2(); mpd2.changeName(p1); System.out.println(" After calling the method p1 Your name is "+p1.name);
The calling of parametric methods is actually the process of assigning values to formal parameters by arguments. Whether the values of formal parameters and arguments are synchronized depends on the data type of the parameter. When the parameter is a value type, the values of formal parameters and arguments are not synchronized. When the parameter is a reference type, the values of formal parameters and arguments are synchronized.