methods

What is the method, what is the use?

(You can first look at a program if there is no method, what will happen?)

A method (English word: method) is a piece of code that does a particular function and can be reused.

The advent of methods makes code reusable.

  • In C, methods are called “functions.”

  • In Java, it’s not called a function, it’s called a method.

You define/extract a method that does not perform a function,

So this method that you’re extracting is meaningless. One common approach is a “functional unit.”

Assuming that a function can be extracted independently in future development, it is recommended to define it as a method so that whenever the function is needed in the future, the method can be called directly instead of having to write the business logic code repeatedly.

2. The most difficult method to implement is:

Method extraction depends on how the business is doing.

What is the return value type defined for a method?

What is the name of the method?

What is the formal argument list defined for a method?

.

One way is a stand-alone function.

3. Definition of methods

Return value type method name (formal argument list){method body; }Copy the code

4, the method of learning every detail

1, modifiers: optional, currently written as: public static

2. How to interpret the return value? The return value is the result of a method after execution.

3. What types can be specified for return value types?

4. Relationship between return value and “return statement”.

5, as long as the method name is a valid identifier, the first letter is lowercase, and the first letter of each following word is uppercase. A name knows a meaning.

6. Formal parameter list

7. Method body: The code in the method body is executed line by line in a top-down order.

8. How to call a method? “The name of the class. When can it be omitted?

The list of actual arguments, called arguments for short. (The actual data passed when the method is called.)

The relationship between arguments and parameters is one to one.

5, the MEMORY structure of the JVM three more important memory space.

Methods area:

Store the code snippet, store the XXx. class bytecode file, this is the first space to have data, the class loader first loaded the code into here.

Heap memory:

(Object-oriented)

Stack memory:

What is stored in a stack?

The amount of memory (local variables) required for each method to execute.

6. Stack data structure in data structure

Principle:

  • Last in, first out

  • After the advanced

Terms related to stack data structures:

  • Stack frame: Always point to the element at the top of the stack (the element at the top of the stack has active weight).

  • The stack elements

  • The bottom of the stack elements

  • Push, push, push, push

  • Pop the stack, pop the stack, pop

We talked a little bit yesterday: What are data structures? What is an algorithm?

There is a book: Data Structures and Algorithms.

The choice of data structure and algorithm is very important, and the execution efficiency of the program is greatly improved.

Can be very good to optimize the program.

7. Analyze the memory changes during the program running

Methods that are defined but not called are not executed.

When a method is called: push the stack (allocate space for the method in the stack)

At the end of method execution: the stack is loaded (freeing the method’s space and local variables’ memory).

Overload method

1. When should we consider using method overloading?

When functionality is similar, it is recommended to define method names that are consistent,

This code is beautiful and easy to program.

Note: Be sure to make the method names inconsistent if the functionality is not similar.

2. What conditions do code meet to constitute a method overload?

  • Condition 1: In the same class

  • Condition 2: Same method names

  • Condition 3: Different list of formal parameters (type, number, order)

Note:

Method overloading is independent of the return value type and is independent of the list of modifiers.

3. The advantages of method overloading?

  • The code is beautiful

  • Easy to code

Methods the recursive

1. Need to understand what method recursion is?

Method calls itself.

2, when using recursion, must add the end condition, no end condition, will occur stack memory overflow error.

StackOverflowError

Cause: the stack has been pressed, the stack is not loaded, and the stack memory is not enough.

3. Will draw the memory structure diagram of recursive methods.

You can draw it as you go through the recursion.

4, can use the loop instead of recursion try to use the loop, the execution of the loop is less memory, recursion is relatively more memory, in addition to recursion is easy to use memory overflow, the JVM stops working.

Of course, there are rare cases where you can only use recursion and no other code can solve the problem.

5, when recursion has a closing condition, and the closing condition is legal, must not be out of memory?

Not necessarily. Maybe the recursion is too deep.

What should I do if I encounter a stack memory overflow error caused by recursion in actual development?

Step 1: Check that the end conditions are correct.

Step 2: If correct, adjust the STACK memory size of the JVM. (java -X)

Do we just keep reducing variables? Code reduction? Is that good?

public class Test{ public static void main(String[] args){ /* int i = 100; System.out.println(i); */ System.out.println(100); boolean flag = test(); if(flag){ ... } if(test()){.... } } public static boolean test(){ return true; }}Copy the code

What happens if you count the number of variables too much? (Operation efficiency will not be low)

Consequence 1: Poor code readability.

Consequence 2: Poor readability can also have implications for code development efficiency.

In fact, computer memory is not bad this…

Note: There are some variable names that must be defined during coding.

You’ll need to access this data later in the code. Access the data repeatedly.

The last

Recommended to you a more detailed Java zero basic tutorial, the following is I have seen feel pretty good, worth watching the collection.

To share it with you, click here

www.bilibili.com/video/BV1Rx…

If it helped you, thank you for your support