1. Functions
1. What is a function?
- A separate applet defined in a class that has a specific function.
- Functions are also called methods
2. Format of functions
- Modifier returns a value type function name (parameter type form parameter 1, parameter type parameter 2 ...) {
Execute statement (function body);
return value;
}
return value type: The data type of the output after the function finishes running.
parameter type: is the data type of the formal parameter.
formal parameter: is a variable that stores the actual arguments passed to the function when the function is called.
actual parameter: The actual data passed to the formal parameter.
return: ends the function, returning the data to the caller's keyword.
return value: The result after the function is run, returned to the caller.
Special case: The function does not have a specific return value.
The return is then terminated directly with a semicolon. (return can be omitted in the last line)
Use the Void keyword as the return value type.
3. Features of functions
- Define functions to encapsulate the function code
- Facilitates reuse of this feature
- function is executed only if it is called
- The appearance of functions improves the reusability of code
- For cases where the function does not have a specific return value, the return value type is represented by the keyword void, then the return statement in the function can be omitted if the last line is not written
- Attention:
- Functions can only be called, and functions cannot be defined in functions, that is, functions cannot be nested called
- When a function is defined, the result of the function should be returned to the caller and referred to the caller for processing
- Two clear: Results & Parameters
4. Overloading (overload)
Functions like functions, only the unknown content involved in the operation is not at the same time, you can use overloading to define multiple functions, using the same function name, so easy to read, when called, the virtual machine through the different parameter list to distinguish these functions.
In the same class, more than one function with the same name is allowed, as long as the number of arguments or the type of the parameter are different.
Regardless of the return value, see only the parameter list
Easy to read and optimized for programming
Public classutill{//returns the number of two integers and Public Static intSumintAintb) { returnA +b; } //returns two decimals and Public Static DoubleSumDoubleADoubleb) { returnA +b; } //returns the number of three integers and Public Static intSumintAintBintc) { returna+b+C; }}
This cannot be defined: Because the virtual machine is not sure which to call, it can only be distinguished by the type or number of argument lists.
Public classutil{//returns the number of two integers and Public Static intSumintAintb) { returnA +b; } //returns two decimals and Public Static DoubleSumintAintb) { returnA +b; }}
2. Function passing Value
- Java function base data type is a pass value, reference data type is a reference
- When a parameter is a reference, no reference to the argument object is changed, regardless of the action within the function body
- When a parameter is a reference, the contents of the argument object are changed only when the contents of the object are changed within the function body.
(i) Basic data type: Pass value, does not change the value of the argument
Public classdemo{ Public Static voidMain (string[] args) {intA = 3,b = 4; Fun (A, b); System.out.println (A+","+b); } //attempt to exchange values of a and B Public Static voidFunintAintb) { inttemp =A; A=b; b=temp; } }
Operation result is 3, 4
(b) object type parameter: reference, the function body changes the parameter reference, does not change the argument reference, but can change the content of the argument.
public class fundemo{ public static void main (string[] args) {person P1 = new person (10 = new person (20 "before,p1.age:" +p1.age); System.out.println ( "before,p2.age:" +p2.age); SS (P1,P2); System.out.println ( "after,p1.age:" +p1.age); System.out.println ( "after,p1.age:" +p2.age); }
Attempt to exchange P1 and P2 references, and change the Age property value of one of the public staticvoid SS (Person P1, Person p2) { = p1; = P2; = p; = +; }}
Operation Result:
Before,p1.age:10
Before,p2.age:20
After,p1.age:10
After,p2.age:30
Analysis: When running to person p1 = new Person (10), a new space is opened in the stack memory to store the variable P1, creating an instance of person (10) in the heap memory, p2 the same. When the SS method is called, it also opens up new space in the stack memory to store the form variables P1 and P2, which point to the person instance that corresponds to the actual argument in the heap memory, and within the SS method, the values of the P1 and P2 of the formal parameters are exchanged, that is, they are swapped for points. At this point, the direction of the argument does not change. Then, change the content of the instance that the parameter P1 points to, that is, change the content of the instance that the argument P2 points to.
[Java Learning note]java the definition and use of functions in the basic overview of Language basics & function Transfer problems