1. Heavy load
- The concept of overloaded methods
Definition: more than one method with the same name is allowed in the same class, as long as their parameter number or parameter type are different.
Summary: "two are the same and different": the same class and the same method name
Different parameter lists: different parameter numbers and different parameter types - Examples of heavy loads:
Example 1: overloaded sort() / binarySearch() in Arrays class; println() in PrintStream
Example 2:
//The following four methods constitute overloads public void getSum(int i,int j){ System.out.println("1"); } public void getSum(double d1,double d2){ System.out.println("2"); } public void getSum(String s ,int i){ System.out.println("3"); } public void getSum(int i,String s){ System.out.println("4"); }
Examples that do not constitute overloads:
//The following three methods cannot be overloaded with the above four methods // public int getSum(int i,int j){ // return 0; // } // public void getSum(int m,int n){ // // } // private void getSum(int i,int j){ // // }
- How to determine whether it constitutes a method overload?
Judge strictly according to the definition: two are the same and different.
It has nothing to do with the permission modifier, return value type, formal parameter variable name and method body of the method! - How to determine the call of a method in a class:
Method name - > parameter list
Interview question: what is the difference between method overloading and rewriting?
throws\throw
String\StringBuffer\StringBuilder
Collection\Collections
final\finally\finalize
...
Abstract classes and interfaces
sleep() / wait()
2. Variable number formal parameters
1. Instructions:
- 1. New content in JDK 5.0
- 2. Specific use:
- 2.1 format of variable number formal parameters: data type... Variable name
- 2.2 when calling the method of variable number formal parameters, the number of parameters passed in can be: 0, 1, 2,...
- 2.3 methods with variable number formal parameters have the same name as those in this class, and methods with different formal parameters constitute overloads
- 2.4 arrays with the same method name and parameter type as those in this class do not constitute overloads. In other words, the two cannot coexist.
- 2.5 variable number formal parameters must be declared at the end in the formal parameters of the method
- 2.6 variable number formal parameters in the formal parameters of a method, at most one deformable parameter can be declared.
2. Examples:
public void show(int i){ } public void show(String s){ System.out.println("show(String)"); } public void show(String ... strs){ System.out.println("show(String ... strs)"); for(int i = 0;i < strs.length;i++){ System.out.println(strs[i]); } } //Cannot exist with the previous method // public void show(String[] strs){ // // } When called: test.show("hello"); test.show("hello","world"); test.show(); test.show(new String[]{"AA","BB","CC"});
3. Value transfer mechanism
1. Examples of assignment of variables in the method:
System.out.println("***********Basic data type:****************"); int m = 10; int n = m; System.out.println("m = " + m + ", n = " + n); n = 20; System.out.println("m = " + m + ", n = " + n); System.out.println("***********Reference data type:****************"); Order o1 = new Order(); o1.orderId = 1001; Order o2 = o1;//After assignment, the address values of o1 and o2 are the same, and both point to the same object entity in the heap space. System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " +o2.orderId); o2.orderId = 1002; System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " +o2.orderId);
Rules:
If the variable is a basic data type, the data value saved by the variable is assigned at this time.
If the variable is a reference data type, the value assigned at this time is the address value of the data saved by the variable.
-
For the parameter concept of the method
Formal parameters: parameters in parentheses declared when a method is defined
Argument: the data actually passed to the formal parameter when the method is called -
Parameter passing mechanism in Java: Value Passing
Rules:
- If the parameter is a basic data type, what the argument assigns to the formal parameter is the data value actually stored by the argument.
- If the parameter is a reference data type, the address value of the stored data of the parameter is assigned to the formal parameter.
extension:
If the variable is a basic data type, the data value saved by the variable is assigned at this time.
If the variable is a reference data type, the assigned value is the address value of the data saved by the variable.
- Typical examples and memory analysis:
[example 1]
[example 2]
4. Recursion
- definition:
Recursive method: a method body calls itself. - How to understand recursive methods?
Method recursion contains an implicit loop that repeats a piece of code, but this repetition does not need loop control.
Recursion must recurse in the known direction, otherwise this recursion will become infinite recursion, similar to an endless loop. - give an example:
// Example 1: calculate the sum of natural numbers between 1-n public int getSum(int n) {// 3 if (n == 1) { return 1; } else { return n + getSum(n - 1); } } // Example 2: calculate the product of natural numbers between 1-n: n! public int getSum1(int n) { if (n == 1) { return 1; } else { return n * getSum1(n - 1); } } //Example 3: we know a sequence: f(0) = 1,f(1) = 4,f(n+2)=2*f(n+1) + f(n), //Where n is an integer greater than 0, find the value of f(10). public int f(int n){ if(n == 0){ return 1; }else if(n == 1){ return 4; }else{ // return f(n + 2) - 2 * f(n + 1); return 2*f(n - 1) + f(n - 2); } } //Example 4: Fibonacci sequence //Example 5: Hanoi Tower problem //Example 6: fast exhaust
5. Practice
- What is method overloading?
"Two same and different": the same class and the same method name; The parameter list is different.
How to call the determined method: method name parameter list - Explain the concrete embodiment of the parameter passing mechanism in Java methods?
Basic data type: data value
Reference data type: address value (including data type of variable)
Person p1 = new Person(); eat();age User u1 = p1;//Compilation error (reverse thinking, counter evidence method) u1.eat() u1.age
- What are the differences between member variables and local variables in the declared location, whether they have default initialization values, whether they can be decorated with permission modifiers, and the location of memory allocation?
Fundamentals of Java (7) - Talk about the use of return keyword
① End method ② for methods with return value, return + return data - Provide memory parsing of the following code
Output:
15
0
20 - Memory structure: stack (local variable), heap (new structure: object (non static member variable), array)
- Variable: member variable vs local variable (in method, method parameter, constructor, constructor parameter, code block)