Learn Java - day04 - Process Control Statements from scratch (2) & Methods
1. Process control statements
1. Dead cycle
Dead Loop: A loop that cannot be terminated by self-control in a program
Classification:
Dead loop of for statement
for ( ; ; ) { Loop body statement; }
Dead loop of while statement
while (true) { Loop body statement; }
(Supplementary)
- The dead-loop execution efficiency of the for statement is better than that of the while statement. In writing algorithms, design patterns, frames, computer programming languages, etc., the dead-loop table of the for statement is recommended.
- In the actual business requirements development process, select the dead loop of while statement, which makes the dead loop code of while statement more readable and easier to understand.
2. Nested loops
If a loop statement is placed in the loop body of another loop statement, then a nested loop can be formed, which can be either a nesting of for loop statements or a nesting of while loop statements and do... But a regular nested loop generally refers to the nesting of "for loop statements".
Nested Loop Format for Statement
for (Initialization statement for outer loop;Loop condition statement for outer loop;Iterative statements for outer loops) { for (Initialization statement for inner loop;Loop condition statement for inner loop;Iterative statements for inner loops) { Loop body statements for inner loops; } }
2. Methods
1. Concepts
Method is the abstraction of the behavior function of a class or object in the object-oriented part, and is the most important component of a class or object. But functionally, the method is to encapsulate a block of code for a particular function. We can extract the functional code from the main method that repeats many times and encapsulate it into the method so that it can be reused. It is important to note that methods in Java cannot exist independently and that all methods must be defined in classes or interfaces.
Advantages of the method
- Improve code reuse, reduce code writing, and improve development efficiency.
- Decrease code coupling by isolating each functional code in the program.
2. Format for defining methods
Modifier Return Value Type Method Name (Formal parameter type 1 Formal parameter name 1 ,Formal parameter type 2 Formal parameter name 2 , ...... ,parameter types n Formal parameter name n) { Method Body; return Return value; } -Modifier: There are no learning modifiers, temporarily use public static Fixed Writing instead; -Return value type: The data type of the result (return value) of the method run, and the result data is returned to the caller method after the method executes. -Method name: Name the method; -():List of parameters, when a method needs to use data from the caller's method during execution, it cannot be used directly because of scope, and needs to Pass data through parameter transfer; -No parameters:()Nothing in the parameter list -Method body: special function code extracted from the original program;
3. Two definitions of the method
Return value type: Data type of explicit method return value
Parameter list: Explicit parameter list requires several data from the caller method; Define what the data type is for each data
4. Method Calls
Features of the method:
No call, no execution
Method Call Format
-
If a method belonging to a class is called (not learned yet)
Class name.Method Name(Arguments);
-
If a method belonging to an object is called (not learned yet)
Object Name.Method Name(Arguments);
-
If a method in the same class is called
Direct call in the following format:Method Name(Arguments);
-
Output call in the following format:
System.out.println(Method Name(Arguments));
-
Assignment call in the following format:
Data type variable name = Method Name(Arguments);
Formal and actual parameters
Formal parameters: Variables that follow the method name () when defining a method are called formal parameters (for short, formal parameters).
Actual parameters: The parameters in () after the method name when calling a method are called actual parameters (for short, arguments).
The code is as follows (example):
// Requirement: Define a method for calculating the sum of two integers public class Demo { public static void main(String[] args) { // Direct Call // getSum(3,4); // Output Call // System.out.println(getSum(3,4)); // Assignment Call int sum = getSum(3,4);//Find getSum(int a, intb) in the same class through getSum(3,4), jump directly from line 10 to line 20, and bring 3 and 4 together System.out.println("sum = " + sum); } /* Define a method for calculating the sum of two integers Two definitions: Return value type: int List of formal parameters: int a,int b */ public static int getSum(int a, int b) { // return a + b; } }
5.void keywords
- void keyword: method does not return a value
- Notes for void keywords:
1. When the method does not have an appropriate return value, the position of the return value type when defining the method cannot be written at all, and the void keyword is needed to occupy the place;
2. When the return value type of a method is void, it can only be called directly
-Direct calls are only available when method return value type is void
-Assignment calls are recommended when method return value type is not void
3. When the return value type of the method is void, the return keyword after the method can be omitted from not writing
6. Method overload
- Method overload: In the same class (or child-parent inheritance relationship), the same method name and different parameter lists occur
- Characteristics of overload
1. Must be in the same class (or child-parent inheritance relationship)
2. Method names must be the same
3. The list of parameters must be different (at least satisfy the following points)
(1) Different data types in the list of formal parameters
(2) Different number of formal parameters in the list of formal parameters
(3) Different order of data types in parameter list
The code is as follows (example):
public class Demo { public static void main (String[] args) { } public static void getSum (int a , int b) {} public static void getSum (int a , double b) {} public static int getSum (double a , int b) { return 0; } public static void getSum (double a , double b) {} public static void getSum (int a , int b , int c) {} }
6. Recursion of methods
- Recursion: The phenomenon of calling yourself within the current method.
- Recursive classification:
Recursion is divided into two types, direct and indirect.
Direct recursion is called the method itself calling itself.
Indirect recursion allows method A to call method B, method B to call method C, and method C to call method A.
"Sum 1 to a specified number"
The code is as follows:
Xiao Bai asks for attention!!! Xiao Bai asks for attention!!! Xiao Bai asks for attention!!! (Say the important thing three times)// Requirements: sum 1 to a specified number public class MethodDemo05 { public static void main(String[] args) { //Calculate the sum of 1~num using recursion int num = 5; // Call summation method int sum = getSum(num); // Output Results System.out.println(sum); } /* By recursive algorithm. Parameter list: int Return value type: int */ public static int getSum(int num) { /* num When 1, the method returns 1, Equivalent to being the outlet of a method, num always has the case of 1 */ if(num == 1){ return 1; } /* num When not 1, the method returns the sum of num +(num-1) Recursive call to getSum method */ return num + getSum(num-1); } }