Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of method use, overloading and Recursion in Java

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the use of methods in Java, overloading and recursive example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

one。 The basic use of the method 1. What is the method?

Method is a code snippet, similar to a function in C language

two。 Basic syntax of method

Basic grammar

/ method definition public static method return value method name (parameter type parameter) {method body code; return return value;} public static void main (String [] args) {/ / method call return value variable = method name (argument);}

Seeing that this may be a little abstract, let's write a concrete method: add two integers.

Public class TestDemo {/ / method definition public static int Add (int xreint y) {int sum = x + y; return sum;} public static void main (String [] args) {/ / method call Add (10pm 20); System.out.println (Add (10m 20));}}

Matters needing attention

1. When a method is defined, there can be no parameters. Specify a type for each parameter

two。 When the method is defined, the return value can also be absent. If there is no return value, the return value type should be written as vo.

3. The parameters when the method is defined are called formal parameters (formal parameters), and the parameters when the method is called are called actual parameters (actual parameters).

4. The definition of the method must be in the class, and the code can be written above or below the calling location.

5. Entry of all programs: main function

The relationship between shape participation and actual parameters

First of all, let's write a method to exchange two numbers and run it.

Public class Test1 {public static void swap (int apenint b) {int temp = a; a = b; b = temp;} public static void main (String [] args) {int a = 10; int b = 20; System.out.println ("before exchanging arguments:" + a + "" + b); swap (amemb) System.out.println ("after exchanging arguments:" + a + "" + b);}}

Why hasn't anything changed?

Because we exchange formal parameters, not exchanged actual parameters. If we want to exchange arguments, we should get the addresses of an and b, but! An and b in the main function, the variables in the function are local variables and are stored on the stack. But in Java, you can't get the address on the stack, so the actual values of an and b have not changed. If you want to exchange the values of an and b, you can only put the values of an and b on the heap (all the objects on the heap! )

two。 Method overload 1. The role of overloading

We mentioned earlier that the method requires parameter types, but what if we need to use one function to be compatible with multiple parameters at the same time? Method overloading can be used here

After we write an add method, if we want to use this method to calculate double types, there will be a problem of type incompatibility, so how should we solve it? Maybe we can do something like this.

Public class Test2 {public static int addInt (int a, int b) {int sum = aheadb; return sum;} public static double addDouble (double ahelidoub) {double sum = astatb; return sum;} public static void main (String [] args) {double a = 10.5; double b = 20.5; addDouble (arecom b) System.out.println (addDouble (afort b));}}

There is no problem with this way of writing, for example, it is used in the go language, but Java thinks that the name addInt is not very good, so it is better to call it add directly, so it is written as follows.

Public class Test2 {public static int add (int a, int b) {int sum = aheadb; return sum;} public static double add (double ahelidoub) {double sum = astatb; return sum;} public static void main (String [] args) {double a = 10.5; double b = 20.5; add (arecom b) System.out.println (add (afort b));}}

There is no problem with running, this is the function of overloading! It can solve the problem of parameter type mismatch. For example, add method, you can also add more versions of the use, such as the addition of multiple numbers.

The same method name, which provides different versions of the implementation, is called method overloading.

two。 Rules for using overloaded

For the same class:

1. Method name is the same

two。 The parameters of the method are different (number of parameters or parameter type)

3. The return value type of the method does not affect the overload

4. When the two methods have the same name and the same parameters, but the return values are different, it does not constitute an overload. For example, int add (int a pencil int b) and double add (int a pencil int b)

three。 Recursion of method 1. The concept of recursion

Recursion is the process by which a method calls itself. To achieve recursion, it is necessary to derive the recursive formula.

The premise of recursion: there is a condition close to termination, call yourself.

The key to recursion is to find the recursive formula! Understand the process of "delivery" and "return"

two。 Recursive execution process analysis public class Test3 {public static int fac (int n) {if (nonglut1) {return 1;} int temp = n*fac (nMuth1); return temp;} public static void main (String [] args) {System.out.println (fac (3));}}

Specific process analysis:

3. Recursive exercise

I believe that seeing here, you also have a certain understanding of recursion, you can properly practice some questions to increase proficiency. The author's answer is attached. If there is any mistake, please correct it!

1. Print each digit of a number in order

Print each digit of a number sequentially (for example, 1234 prints out 1234)

Public class Demo1 {public static void print (int n) {if (n > 9) {print (nmax 10);} System.out.print (n + ");;} public static void main (String [] args) {int n = 1234; print (n);}}

two。 Recursively find 1 + 2 + 3 +. + 10

Recursively find 1 + 2 + 3 +. + 10

Public class Demo2 {public static int func (int n) {if (func (10)) {n + = func (func 1);} return n;} public static void main (String [] args) {System.out.println (func (10));}}

3. Returns the sum of the numbers that make up it

Write a recursive method, enter a non-negative integer and return the sum of the numbers that make up it. For example, if you enter 1729, you should return 1 "7" 2 "9, whose sum is 19.

Public class Demo3 {public static int func (int n) {if (n)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report