List of articles

  • One . The basic usage of method
  • 1. What is the method (method)
  • 2. Method definition syntax
    • (1) The basic syntax of methods
    • (2) Method call
    • (3)Java Memory structure
  • 3. The execution of a method call
  • 4. The relationship between actual parameters and formal parameters ( important )
  • Two . Method overloading
  • 1. The problem to be solved
  • 2. Use overload
  • 3. Overloaded rules
    • The rules
  • 3、 … and 、 Method recursion
  • 1. The concept of recursion
  • 2. Recursive execution process analysis
  • 3. Method recursion exercise
  • End !

Content outline

 Insert picture description here

Next chapter Java Basic grammar ( Four )—— Program logic control

One . The basic usage of method

1. What is the method (method)

Method In fact, it is to encapsulate a repetitive thing , Encapsulate it as a function .

Be similar to C Functions of language , But it’s not exactly the same .

The significance of the existence of methods ( Don’t recite , It’s about experience ):

  1. It’s organizational code that can be modularized ( When the code size is complex ).
  1. Make sure the code is reused , One piece of code can be used in multiple locations .
  1. Make the code easier to understand .
  1. Call existing methods directly to develop , You don’t have to make wheels again .

2. Method definition syntax

(1) The basic syntax of methods

 Insert picture description here

(2) Method call

 Insert picture description here

Code example :

seek 1-100 The sum between

 Insert picture description here

Compilation result :

 Insert picture description here

We see , Successfully called the content of the method , It also receives the return value of the method .

In this code exercise , We know the basic syntax of the method and its call .

(3)Java Memory structure

 Insert picture description here

Our calls to methods , It’s stack dependent

Call one method at a time , Will open up a memory on the stack for this method . Call this memory : Stack frame .

Take the code above as an example , Introduce how to store the method in memory .

 Insert picture description here

The first method to call is main Method , At this time, a block will be opened up in the memory main The stack frame , At this time in main Create a ret Variable , stay main It is stored in the memory of ret Space .

Call again at this point add Method , Open up a stack frame again , The formal parameter is n, Deposit n Space , Go down Created a sum The variable of , In the storage of a sum Space , Entering the loop creates a i The variable of .

In this case, the storage of stack space :

 Insert picture description here

add Method return The end of the statement , After calling , Out of the stack .

 Insert picture description here

main Method statement execution completed , Out of the stack .

 Insert picture description here

This is a complete method calling process .

3. The execution of a method call

The basic rule

1. When defining methods , Code that does not execute methods . Only when called .

2. When a method is called , Will assign an argument to a formal parameter .

3. After passing the parameters , It will execute to the method body code .

4. When the method is finished ( encounter return sentence ), Go back to the method call and continue .

5. A method can be called many times .

4. The relationship between actual parameters and formal parameters ( important )

What arguments ? What is formal parameter ?

Let’s take a look at the following code example

 Insert picture description here

We know how to use the parameters through an exercise .

Subject requirements : Exchange two shapes in a way .

Let’s see if the following code is correct .

 Insert picture description here

It seems that the process is right , Let’s take a look at the compilation results .

 Insert picture description here
There was no exchange , Why is that ?

We know , What we pass is the value of the argument , But a formal parameter is just a copy of an actual parameter , So in swap In the method , We just exchanged x,y The values of these two parameters ,swap After the method is called, the values of the two parameters are destroyed automatically , So the value of the argument doesn’t change .

stay C In language , We want to exchange two integer values through a function , Need to carry out & Its address operation

But we make it clear that :

1.Java in No, & Take the address symbol

2.Java among The memory address on the stack is unreachable

3.Java In this method, only values can be passed , Can not be like C The language carries on the address transmission reference .

So in Java How to realize the operation of address transfer in ?

The answer is to quote , As long as it’s a reference type , class 、 Interface 、 abstract class 、 enumeration 、String、 Array etc. …

We will continue to mention the specific use in the updated blog .

Notes on real participation in formal parameters when passing parameters

 Insert picture description here
As shown in the figure above :

1. Number of parameters 、 type 、 All in one order A match .

2. The return value type of a method should be the same as the accept type A corresponding .

Two . Method overloading

Sometimes we need to use a function to be compatible with multiple parameters at the same time , We can use method overloading .

1. The problem to be solved

 Insert picture description here

We hope that in the future add Method passes in two double Variable of type , Add floating point numbers .

But the compilation results :

 Insert picture description here

We can only change the code to

 Insert picture description here

Both methods implement the same function , Add two data . We have to create different types of functions for different data , Is this too much trouble , So in Java There is the concept of method overloading in : Compatibility of multiple parameters with one function .

2. Use overload

Or modify the above code :

 Insert picture description here

The name of the method is add. But there are add It’s calculation int Add up , There are plenty of them double Add up ; Some even add two numbers together , Some can also calculate the sum of three numbers .

The same method name , Provide different versions of the implementation , be called Method overloading

3. Overloaded rules

The rules

 Insert picture description here

Look at the following two codes :

 Insert picture description here

Through these two code examples , We can know in Java The editor looks like ,sum The two methods are still the same , Because its parameter list is the same . So it will report an error , So the return value has no effect on method overloading .

3、 … and 、 Method recursion

1. The concept of recursion

A method calls itself in the execution process , It’s called “ recursive ”.

Recursion is the mathematical equivalent of “ Mathematical induction ”, There is a starting condition , And then there’s a recursive formula .

Note on recursion :

 Insert picture description here

1. The program calls itself

2. This recursive program must have a near abort condition .

3. The core : When writing recursive programs , You need to deduce a recurrence formula yourself .

example :

We ask N!

Starting conditions : N = 1 When , N! by 1. This starting condition is equivalent to the end condition of recursion .

Recursive formula : seek N! , It’s not easy to ask directly , You can turn the problem into 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);}

2. Recursive execution process analysis

The execution of recursive programs is not easy to understand , Think about recursion , You have to understand it first “ Method execution process ”, In especial “ After method execution , Go back to the call position and continue on ”.

3. Method recursion exercise

See blog ——Java Method Recursive use and practice

Okay , This time, Java Basic grammar —— That’s the end of sharing knowledge about the use of methods , I hope you can practice more , Be familiar with knowledge , Improve yourself . Finally, thank you for your appreciation and attention !!!

Thank you for enjoying !!!

End !