In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "case analysis of the use of Java methods", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java method use case analysis" article.
I. the concept of method and its use 1.1. What is method?
The method is a code snippet. Similar to the "function" in the C language. The functions are as follows:
1. Is the organization code that can be modularized (when the scale of the contemporary code is more complex).
two。 To make sure that the code is reused, one code can be used in multiple locations.
3. Make the code easier to understand and simpler.
4. Directly call the existing method development, do not have to repeat the wheel.
1.2. Definition of method
Define the format:
Example: write a function to realize the sum of the factorial of how many terms
Public class TeseDemo220424 {/ / calculate the factorial of a number public static int func (int n) {int ret = 1; for (int I = 1) find the address of the method-> the body of the method that executes the called method-> return to the end of the called method-> go back to the main tone method and continue to execute
Image icon:
Note:
1. When a method is defined, the code of the method is not executed. It is executed only when it is called.
2, a method can be called multiple times.
1.4. The relationship between actual parameters and formal parameters (important)
For the relationship between shape participation in the actual parameter, we must be no stranger, that is, the formal parameter is a temporary copy of the actual parameter, that is to say, the change of the parameter will not affect the actual parameter.
The specific reasons are illustrated: (because the specific problem related to the frame of the function stack is more complicated, here is a brief explanation.)
Therefore, as you can see from the figure, if the value is called, the value of the parameter participating in the shape is not stored in the same space at all, so it will not interfere with each other. In C language, if you want the change of the parameter to affect the argument, it must be called by passing address, but there is no such saying in Java, so the solution can only be to use reference (more on this later).
2. Method overloading 2.1.Why do you need a method to overload public class TestDemo220426 {public static int addInt (int x point int y) {return x + y;} public static double addDouble (double x Magi double y) {return x + y;} public static void main (String [] args) {int a = 10; int b = 10; int ret = addInt (Azov b) Double c = 11.1; double d = 12.1; double ret1 = addDouble (cMagne d);}}
Look at this code, addint,adddouble these two methods, the purpose is to find the sum of two numbers, but you have to define two different functions, the function name is different, but sometimes the name is a headache, so since the essential function of the method is the same, but the parameters may be different, can we all use the same function name?
The answer is yes, and this is called method overloading.
2.2. Definition of method overloading
In Java, if multiple methods have the same name and different parameter lists, they are said to be overloaded. It's a name, but it has a different meaning.
Public class TestDemo220426 {public static int add (int x pencil int y) {return x + y;} public static double add (double x Magi double y) {return x + y;} public static void main (String [] args) {int a = 10; int b = 10; int ret = add (a Magi b); System.out.println (ret); double c = 11.1 Double d = 12.1; double ret1 = add (CPH d); System.out.println (ret1);}}
Screenshot of running the program:
As can be seen from the above program, it all implements the function of addition, but defines two different function implementations, but the function name is the same, which is function overloading. The compiler will call the corresponding function according to the parameters you pass in.
Note:
1. The method name must be the same.
two。 The parameter list must be different (different number of parameters, different types of parameters, different order of types) (need to be distinguished by parameters)
3. It has nothing to do with whether the return value type is the same.
4. You cannot define two functions that are distinguished only by the return value, which cannot constitute an overload
5. When compiling the code, the compiler deduces the argument type and determines which method to call according to the result of the deduction.
2.3. Method signature
The method signature is the final name of the method after being compiled and modified by the compiler. Specific method: method full path name + parameter list + return value type, constitute the complete name of the method. Functions used to distinguish overloads
How can I see our method signature:
1. First compile the project to generate a .class bytecode file
two。 In the console, go to the directory where the .class you want to view is located
3. Enter: javap-v bytecode file name
3. Recursion 3.1, the concept of recursion
Definition: a method that calls itself during execution is called recursion.
Necessary conditions for recursion:
1. A recursive formula.
two。 Recursive termination condition.
3.2.Recursive process analysis public static int func (int num) {if (num = = 1) {return 1;} return num*func (num-1);} public static void main (String [] args) {/ / recursive factorial Scanner scan = new Scanner (System.in); System.out.println ("Please enter a number:") Int num = scan.nextInt (); int ret = func (num); System.out.println ("factorial:" + ret);}}
Here we take the code for the factorial of a number as an example to analyze a recursive process:
In fact, the process of recursion is not complicated, as two parts, one is to hand it out, but to return, the blue arrow above is the process of passing, and the red arrow is the process of returning.
3.3, Recursive exercise
Print each digit of a number sequentially (for example, 1234 prints out 1234)
Public class TestDemo220427 {public static void myprint (int num) {if (num < 10) {System.out.println (num); return;} myprint (num/10); System.out.println (num); return } public static void main (String [] args) {/ / Recursive implementation of printing digits sequentially Scanner scan = new Scanner (System.in); System.out.println ("Please enter a number:"); int num = scan.nextInt (); myprint (num);}}
Find the N item of the Fibonacci series
Public class TestDemo220428 {public static int fib1 (int n) {int f1 = 1; int f2 = 1; int f3 = 1; for (int I = 3bizi)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.