methods

Java methods are collections of statements that together perform a function.

  • A method is an ordered combination of steps to solve a class of problems
  • Methods are contained in classes or objects
  • Methods are created in the program and referenced elsewhere

Principle of method design: the original intention of a method is a function block, which is a collection of statement blocks to achieve a function. When we design the method, it is better to keep the atomicity of the method, that is, a method only completes 1 function, which is conducive to our later expansion.

Advantages of the method

  • Make procedures shorter and clearer.
  • Good for program maintenance.
  • Can improve the efficiency of program development.
  • Improved code reuse.

Method definition

Return value type method name (parameter type parameter name){... Method body... Return Return value; }Copy the code
  • Modifiers: Modifiers, optional, tell the compiler how to call the method. Defines the access type for the method.
  • Return value types: Methods may return values. ReturnValueType is the data type of the value returned by the method. Some methods perform the desired action but return no value. In this case, returnValueType is the keyword void.
  • Method name: is the actual name of the method. The method name and parameter list together form the method signature.
  • Parameter types: The parameter is like a placeholder. When a method is called, the value is passed to the parameter. This value is called an argument or variable. The parameter list refers to the type, order, and number of parameters of a method. Arguments are optional, and methods can contain no arguments.

Formal parameters: Data used to receive external input when a method is called. Argument: The data actually passed to the method when it is called.

  • Method body: The method body contains concrete statements that define the functionality of the method.

The method call

Java supports two ways to call a method, depending on whether the method returns a value.

When a program calls a method, control of the program is given to the called method. Returns control to the program when the return statement of the called method executes or when the method body closes parentheses.

A method call is usually treated as a value when a method returns a value.

In the Java language, methods are called using the following form: object name. Method name (argument list)

public static void main(String[] args) { int i = 5; int j = 2; int k = max(i, j); System.out.println(I + "and" + j + ", Max: "+ k"); } public static int Max (int num1, int num2) {int result; if (num1 > num2) result = num1; elseresult = num2; return result; }Copy the code

Method overloading

Two methods of a class have the same name, but have different argument lists.

The Java compiler determines which method should be invoked based on the method signature.

Method overloading can make programs clearer and easier to read. Methods that perform closely related tasks should use the same name.

Overloaded methods must have different argument lists. Methods cannot be overloaded based solely on modifiers or return types.

Variable parameter

Starting with JDK 1.5, Java supports passing mutable arguments of the same type to a method.

The variable arguments to the method are declared as follows:

typeName... parameterName 
Copy the code

In the method declaration, add an ellipsis (…) after the parameter type. .

Only one variable parameter can be specified in a method, and it must be the last parameter of the method. Any ordinary arguments must be declared before it.

Public static void main(String args[]) {// Call printMax(34, 3, 3, 2, 56.5); printMax(new double[]{1, 2, 3}); }public static void printMax( double... numbers) { if (numbers.length == 0) { System.out.println("No argument passed"); return; }double result = numbers[0]; / / sort! for (int i = 1; i < numbers.length; i++){ if (numbers[i] > result) { result = numbers[i]; } }System.out.println("The max value is " + result); }Copy the code

recursive

Recursion is a common way to solve a problem, which is to simplify the problem gradually. The basic idea of recursion is “call itself”, a method using recursive techniques will call itself directly or indirectly.

Using recursion can use simple procedures to solve some complex problems. It usually transforms a large complex problem layer by layer into a smaller problem similar to the original problem to solve. The recursive strategy only needs a few programs to describe the repeated calculation required by the process of solving the problem, greatly reducing the amount of code of the program. Recursion is the ability to define an infinite collection of objects in finite statements.

The recursive structure consists of two parts:

  • Recursive head. When not to call its own method. If you don’t have a head, you’re stuck in an endless loop.

  • The recursive body. When you need to call your own method.

    //54321 public static void main(String[] args) { System.out.println(f(5)); }public static int f(int n) { if (1 == n) return 1; elsereturn n*f(n-1); }

(1) Boundary conditions: factorial, multiplied to the last number, that is, 1, return 1, the program is executed to the end;

(2) Recursive forward section: when the current parameter is not equal to 1, continue to call itself;

If the current parameter is 5, then it is 5. 4, i.e. 5 (5-1), i.e. N * (n-1)

Recursion is actually convenient for programmers to machine, recursion can be easily converted into programs through mathematical formulas. Its advantage is that it is easy to understand and easy to program. However, recursion is implemented by the stack mechanism, each deep layer, will occupy a piece of stack data area, for some algorithms with deep nested layers, recursion will be inadequate, the space will end up in memory crash, and recursion also brings a lot of function calls, which also has a lot of extra time overhead. So at great depths, it’s not spacetime very good. (Will take up a lot of memory space)

Although iteration is efficient, the running time increases only because the number of cycles increases, there is no extra overhead, and there is no increase in space, but the disadvantage is that it is not easy to understand, difficult to write complex problems.