The article directories
- The basic usage of the method
- 1. What is method?
- 2. Method definition syntax
-
- (1) Basic syntax of methods
- (2) Method invocation
- (3)Java memory structure
- 3. Method call execution process
- 4. The relationship between arguments and parameters (important)
- Method overloading
- 1. Reload the problem to be solved
- 2. Use overloading
- 3. Rules for overloading
-
- The rules
- Third, method recursion
- 1. Concept of recursion
- 2. Analysis of recursive execution process
- 3. Method recursive practice
- Finished!
Outline of this content
\
\
Following up on Java basic syntax (4) – program logic control \
The basic usage of the method
\
1. What is method?
\
The way to do that is to encapsulate something that is repeated, encapsulate it as a function.
Functions similar to C, but not identical.
\
The meaning of method existence (do not recite, focus on experience):
- Is the ability to organize code modularize (when the code size is complex).
- Make sure that code is reused, and that a copy of code can be used in multiple places.
- Make the code easier to understand and simpler.
- Call the existing method directly to develop, do not have to duplicate the wheel.
\
2. Method definition syntax
\
(1) Basic syntax of methods
(2) Method invocation
\
Code examples:
Take the sum between 1 and 100
Compile result:
\
We see that the contents of the method are successfully called and the return value of the method is received.
In this code exercise, we learned the basic syntax of a method and its invocation.
\
(3)Java memory structure
\
\
The way we call a method is stack dependent
Each time a method is called, memory is created for that method on the stack. Call this memory: stack frame.
The above code is used as an example to describe how to store methods in memory.
\
\
The first method to be called is main, and a stack frame of Main will be opened in memory. At this time, a ret variable will be created in main, and the ret space will be stored in main memory.
\
Call add and create a stack frame with a parameter n to store n. Go down and create a “sum” variable. In the “sum” variable, enter and create an “I” variable.
\
The storage condition of stack space at this time:
\
\
The add method returns to end the statement, and when the call is complete, the stack is removed.
\
The main statement is removed from the stack.
\
\
This is a complete method call.
\
3. Method call execution process
\
The basic rule
\
1. Method code is not executed when a method is defined. It is executed only when called.
2. When a method is called, the argument is assigned to the parameter.
3. After the parameters are passed, the method body code is executed.
4. When the method is finished executing (a return statement is encountered), go back to the method call and continue executing.
5. A method can be called more than once.
4. The relationship between arguments and parameters (important)
\
What arguments? What is a parameter?
Let’s look at the following code example
\
\
We learned the use of passing parameters through an exercise.
Use method to exchange two integers.
Let’s see if the following code is correct.
\
\
It looks like the process is correct, so let’s look at the compilation result.
\
There was no exchange, so why?
\
We know that we are passing the value of the argument, but the parameter is just a copy of the argument, so in swap, we are just exchanging the values of x and y. After swap, the values of the two parameters are automatically destroyed, so the value of the argument remains the same.
\
In C, if we want to exchange the values of two integers with a function, we need to perform the address operation
\
But we make it clear:
\
1.Java does not have the ampersand symbol
2. In Java, the memory address on the stack cannot be retrieved
3. In Java, method parameters can only be passed by value, not by address as in C language.
\
So how to implement the address transfer operation in Java?
Class, interface, abstract class, enumeration, String, array, etc.
We’ll talk more about this in a future blog post.
\
Note the following when passing parameters:
As shown above:
1. The number, type, and sequence of parameters must be matched.
2. The return value type of a method should also correspond to the accepted type.
\
Method overloading
\
Method overloading can be used when we need to use a function with multiple arguments.
\
1. Reload the problem to be solved
\
\
We want to add floating-point numbers by passing two double variables to the add method.
But the result of compiling:
\
We can only change the code to
\
\
Both methods do the same thing: add two pieces of data. If it’s too much trouble to create different types of functions for different types of data, we have the concept of method overloading in Java: using a function that supports multiple arguments at the same time.
\
2. Use overloading
\
Again, modify the above code:
\
\
The names of these methods are all called add. But some add computes the addition of ints and some double; Some even add two numbers, some even add three.
\
Providing different versions of the same method name is called method overloading
\
3. Rules for overloading
\
The rules
\
Take a look at the following two codes:
From these two code examples, we can see that the two methods of sum are still two identical methods in the eyes of the Java editor because their argument lists are the same. So an error is reported, so the return value has no effect on method overloading.
\
Third, method recursion
\
1. Concept of recursion
\
A method that calls itself during execution is called recursion.
Recursion is the mathematical equivalent of “mathematical induction”, with an initial condition and then a recursive formula.
Notes for recursion:
1. The program calls itself
2. The recursive program must have a condition that approaches abort.
3. Core: When writing recursive programs, I need to derive a recursive formula by myself.
\
Ex. :
We pray for N!
Starting condition: N = 1, N! Is 1. This starting condition is equivalent to the end condition of the recursion.
Recursive formula: find N! , it’s not easy to solve directly, so we can convert the problem to N! => N * (N-1)!
Code implementation:
\
import java.util.Scanner;
public static int fac(int n){
if(n==1) {return 1;
}
return n*fac(n-1);
}
public static void main5(String[] args){
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int ret = fac(N);
System.out.println(ret);
}
Copy the code
\
2. Analysis of recursive execution process
\
Recursive program execution process is not easy to understand, to understand clearly recursion, we must first understand the “method execution process”, especially “method execution after the end of the call to continue to execute”.
\
3. Method recursive practice
\
See the blog – Java Method recursive Use and Exercise
\
Well, this time Java basic syntax – method use of knowledge to share here ended, I hope you can practice, familiar with the knowledge, improve their own. Finally, thank you for your appreciation and attention!!
\
Thank you!! \