Basics of Java (8): Object-oriented: overloading, variable number of formal parameters, value transfer mechanism, recursion
Article directory 1. Overloading 2. Variable number of formal parameters 3. Value passing mechanism 4. Recursion 5. Exercise 1. Overloading Method The concept of overloadingDefinition: In the same class, more than one method with the same name is allowed, as long as they have different number of parameters or parameter types. Summary: “Two are the same but different”: same class, same method name Different parameter lists: different number of parameters, different parameter types Examples of overloading: /> Example 1: Overloaded sort() / binarySearch() in the Arrays class; println() in PrintStream Example 2: //The following four methods constitute overloadingpublic void getSum(int i,int j){System.out.println(“1”);}public void getSum(double d1,double d2){System.out.println(“2” );}public void getSum(String s ,int i){System.out.println(“3”);}public void getSum(int i,String s){System.out.println(“4”); } Examples that do not constitute overloading: //The following three methods cannot be overloaded with the above four methods// public int getSum(int i,int j){// return 0;// }// public void getSum(int m,int n){// // }// private void getSum(int i,int j){// // } How to determine whether it constitutes an overload of a method? Judge strictly according to the definition: two are the same but different. It has nothing to do with the method’s permission modifier, return value type, formal parameter variable name, or method body!…
Java determines the return value of the previous method_java basics 5 (method, return value, overloading, recursion)
Method: Define a way to solve a problem. A block of code with a specific function Features: 1. Methods will not be executed if they are not called 2. Methods cannot be nested outside of methods in a class Methods with return value types Definition format: Modifier return value type method name ([parameter list]) { Method body statement; return return value; } ①Modifier: access permission modifier public static ②Return value type: After the method is executed, the data type of the result Basic data type | Reference data type ③Method name: The identifier matches the identifier Naming rules and specifications, method calls should be based on the method name ④()–>Parameter list: parameters can be present, absent, or multiple parameters In the method definition When, unknown and uncertain values are defined in the parameter list Data type parameter name 1, data type parameter 2,… Parameters are equivalent to the declaration of local variables ⑤{}->Code blocks with specific functions ⑥return: 1) End the method early 2) Bring out the return value Method call: Method name (parameter); –>Execution method Code in Call of method with return value type: 1. Directly call the method name (parameter); 2. Assignment call data type variable name =…
Examples of commonly used algorithms for Javascript iteration, recursion, exhaustion, and recursion
Add and accumulate Accumulate: Add a series of data to a variable. Finally, the cumulative result is obtained For example: Find the cumulative sum of numbers from 1 to 100 The ball falls from a height and returns to half of its original position each time. Find the distance traveled by the ball when it hits the ground for the tenth time Accumulation: Multiply a series of data into a variable to obtain the cumulative result. The most common one is the factorial of n var n=100; var result= 1; for(var i=1;i<=n;i++){ result*=i; } General form: Accumulation: V +=e; Accumulation: v*=e; V stands for accumulation and accumulation, e stands for accumulation/accumulation term Key points of the algorithm: (1) Initialization Initialize v and e Accumulation: v = 0; Accumulation: v = 1; The initialization of e, if the accumulation/product term is relatively complex, may be decomposed into several sub-terms and initialized separately. For example, in the problem of calculating pi, the accumulation term is decomposed into three parts: symbol, numerator and denominator. (2) Loop control conditions One is a fixed number of times, such as the problem of calculating the jumping distance or the problem of calculating the sum of the…
Java (command line parameter passing, variable parameters, recursion
1. Command line parameter passing Passing parameters through the command line, main can also pass parameters public class Hello { public static void main(String[] args) { for (int i = 0; i System.out.println(“args[“+i+”]”+args[i]); } }} 2. Variable parameters The essence is an array Starting from JDK1.5, Java supports passing variable parameters of the same type to a method. In the method declaration, add an ellipsis (…) after the specified parameter type A A method can only specify one variable parameter, which must be the last parameter of the method, and any ordinary parameters must be declared before it. public class Demo04 { public static void main(String[] args) { // Call the variable parameter method printMax(34,3,25,88); printMax( new double[]{857,66,88}); } public static void printMax(double… numbers){ if(numbers.length==0){ System. out.println(“No argument passed”); return; } double result = numbers[0]; // Sorting for (int i = 0; i <numbers .length; i++) { if(numbers[i] > result){ result = numbers[i]; } } System.out.println(“The maximum value is “+result); }} 3. Recursion Method A calls method B, it is easy for us to understand! Recursion means that method A calls method A! Just adjust yourself Use simple programs to solve some complex problems. Large and complex problems are transformed…
Basics of Java (8): Object-oriented: overloading, variable number of formal parameters, value transfer mechanism, recursion
Article directory 1. Overloading 2. Variable number of formal parameters 3. Value passing mechanism 4. Recursion 5. Exercise 1. Overloading Method The concept of overloadingDefinition: In the same class, more than one method with the same name is allowed, as long as they have different number of parameters or parameter types. Summary: “Two are the same but different”: same class, same method name Different parameter lists: different number of parameters, different parameter types Examples of overloading: /> Example 1: Overloaded sort() / binarySearch() in the Arrays class; println() in PrintStream Example 2: //The following four methods constitute overloadingpublic void getSum(int i,int j){System.out.println(“1”);}public void getSum(double d1,double d2){System.out.println(“2” );}public void getSum(String s ,int i){System.out.println(“3”);}public void getSum(int i,String s){System.out.println(“4”); } Examples that do not constitute overloading: //The following three methods cannot be overloaded with the above four methods// public int getSum(int i,int j){// return 0;// }// public void getSum(int m,int n){// // }// private void getSum(int i,int j){// // } How to determine whether it constitutes an overload of a method? Judge strictly according to the definition: two are the same but different. It has nothing to do with the method’s permission modifier, return value type, formal parameter variable name, or method body!…
Java determines the return value of the previous method_java basics 5 (method, return value, overloading, recursion)
Method: Define a way to solve a problem. A block of code with a specific function Features: 1. Methods will not be executed if they are not called 2. Methods cannot be nested outside of methods in a class Methods with return value types Definition format: Modifier return value type method name ([parameter list]) { Method body statement; return return value; } ①Modifier: access permission modifier public static ②Return value type: After the method is executed, the data type of the result Basic data type | Reference data type ③Method name: The identifier matches the identifier Naming rules and specifications, method calls should be based on the method name ④()–>Parameter list: parameters can be present, absent, or multiple parameters In the method definition When, unknown and uncertain values are defined in the parameter list Data type parameter name 1, data type parameter 2,… Parameters are equivalent to the declaration of local variables ⑤{}->Code blocks with specific functions ⑥return: 1) End the method early 2) Bring out the return value Method call: Method name (parameter); –>Execution method Code in Call of method with return value type: 1. Directly call the method name (parameter); 2. Assignment call data type variable name =…
Java recursive implementation of exhaustive list of Chinese character words_Javascript iteration, recursion, exhaustive, and recursive common algorithm examples to explain…
Accumulate and accumulate Accumulate:Add a series of data to a variable. Finally, the cumulative result is obtained For example, :calculate the cumulative sum of the numbers from 1 to 100 The ball falls from a height, each time it returns to half of the original value ,Find the distance traveled by the ball when it hits the ground for the tenth time var h=100; var s=0; for(var i=0;i<10;i++){ h=h/2; s&# 43;=h; } s=s*2+100; Accumulate: a series of Multiply the data into a variable to get the cumulative result. The common one is the factorial of n var n=100; var result= 1; for( var i=1;i<=n;i++){ result*=i; } General form: Accumulate:V +=e; Accumulate:v*&# 61; e ) Initialization Initialize v and e Accumulate :v = 0; Accumulate :v = ; 1; e initialization,If the accumulation/product term is complex, it may be decomposed into several sub-terms and initialized separately, such as the problem of calculating pi. xff0c;The cumulative term is decomposed into three parts: symbol, numerator and denominator. (2) Loop control conditions One is a fixed number of times – such as the problem of calculating the bounce distance – the problem of calculating the sum of the first 20 items of the sequence…
LeByte Java programming methods, calls, overloading, recursion
1. Overview Methods refer to the methods, means and solutions that people adopt in practice to achieve certain goals and effects. The so-called method is an orderly combination of codes that solves a type of problem. It is a functional module. A method in a programming language is a collection of statements grouped together to perform an operation. For example, the System.out.println method, the system actually executes multiple statements for messages printed on the console. The method can be understood as a “CD player” that puts in different discs to produce different songs; or a “juicer” that puts in different fruits to squeeze out different juices. The method is to give a named function block to the multiple lines of code that can solve the problem, so that we can use it multiple times. The role of the method: 1), the main method is too bloated 2) Name the repeatedly executed code blocks to facilitate reuse 3), facilitate top-down decomposition of problems 4), easy to maintain code Now we will learn how to define our own methods with or without a return value, use methods that are called with or without parameters, and overload methods with the same name. 2.…
Java determines the return value of the previous method_java basics 5 (method, return value, overloading, recursion)
Method: Define a way to solve a problem. A block of code with a specific function Features: 1. Methods will not be executed if they are not called 2. Methods cannot be nested outside of methods in a class Methods with return value types Definition format: Modifier return value type method name ([parameter list]) { Method body statement; return return value; } ①Modifier: access permission modifier public static ②Return value type: After the method is executed, the data type of the result Basic data type | Reference data type ③Method name: The identifier matches the identifier Naming rules and specifications, method calls should be based on the method name ④()–>Parameter list: parameters can be present, absent, or multiple parameters In the method definition When, unknown and uncertain values are defined in the parameter list Data type parameter name 1, data type parameter 2,… Parameters are equivalent to the declaration of local variables ⑤{}->Code blocks with specific functions ⑥return: 1) End the method early 2) Bring out the return value Method call: Method name (parameter); –>Execution method Code in Call of method with return value type: 1. Directly call the method name (parameter); 2. Assignment call data type variable name =…
LeByte Java programming methods, calls, overloading, recursion
1. Overview Methods refer to the methods, means and solutions that people adopt in practice to achieve certain goals and effects. The so-called method is an orderly combination of codes that solves a type of problem. It is a functional module. A method in a programming language is a collection of statements grouped together to perform an operation. For example, the System.out.println method, the system actually executes multiple statements for messages printed on the console. The method can be understood as a “CD player” that puts in different discs and different songs appear; or a “juicer” that puts in different fruits to squeeze out different juices. The method is to give a named function block to the multiple lines of code that can solve the problem, so that we can use it multiple times. The role of the method: 1), the main method is too bloated 2) Name the repeatedly executed code blocks to facilitate reuse 3), facilitate top-down decomposition of problems 4), easy to maintain code Now we will learn how to define our own methods with or without a return value, use methods that are called with or without parameters, and overload methods with the same name. 2.…