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

What is a method in Java language and how to use it

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

Share

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

This article mainly explains "what is the method in Java language and how to use it". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what the method is and how to use it in the Java language.

I. what is the method?

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

1. The method is an orderly combination of steps to solve a class of problems.

two。 Method is contained in a class or object

3. Method is created in the program and applied elsewhere

The principle of the design method: the original intention of the method is the function block, which is the combination of statement blocks that realize a function. When we design a method, it is best to keep the atomicity of the method, that is, a method completes only one function, which is beneficial to our later expansion.

Of course, read-only text can not be fully understood, the following code must be tapped one by one, carefully savor:

/ / Class public class Demo01 {/ / mian method, which can be understood as a method defined by the system (there must be a mian method in a class) public static void main (String [] args) {int sum=add (1Magne2); System.out.println (sum) } / / customizing a "addition" / / method in which static is written in order to facilitate calling it in other methods (to cooperate with return), which can be understood as a class variable public static int add (int a journal int b) {return aaddition / the defined addition is returned to the mian method that called it above}

The following is the case where the return is empty (viod) (comments are important):

Public class Demo02 {/ / mian method public static void main (String [] args) {test (); / / the following custom method calls will output 1} / / customize a method test () method public static void test () {/ / void is equivalent to a null return, then you do not need to use it with return, int astat1; System.out.println (a) / * because the variable defined this time is in the test () method, and when the mian method is called, the variable cannot be found in the mian method, then the return value is empty, using viod. This makes the mian method more concise, can directly call the test () method, the return value is empty, can not be returned with return, * /}} 2, the definition of the method

Java's method, similar to a function in other languages, is a code snippet used to perform a specific function. In general, defining a method contains the following syntax (a method contains a method header and a method body):

1. Modifier: this is optional and tells the compiler how to call the method. Defines the access type of the method.

two。 Return value type: the method may return a value. ReturnValueType is the data type of the value returned by the method. Some methods perform the required operation but do not return a value, that is, the return is empty. In this case, returnValueType is the keyword void.

3. Method name: is the actual name of the method. The method and the parameter table constitute the method signature.

4. Parameter type: the parameter is like a placeholder when the method value is given to the parameter. This value is called an argument or variable. The parameter list refers to the parameter type, order, and number of parameters of the method. Parameters are optional and the method does not contain any parameters.

Formal parameter: used to accept external input data when the method is called.

Argument: the calling method is the data actually passed to the method.

Method body: the method body contains specific statements that define the function of the method.

Combined with the above text, carefully analyze the following code:

Public class Demo03 {/ / main method public static void main (String [] args) {int max = max (20 public static int max 20); System.out.println (max);} / / Custom a ratio method public static int max (int num1,int num2) {int reason=0; if (num1==num2) {reason=num1; System.out.print ("both values are equal") Return reason;//return can not only return a value, but also means a termination method. The code is running and the statements after return will no longer execute} if (num1 > num2) {reason=num1;} else {reason=num2;} return reason;}}

Tip: return has the function of terminating the method, that is, if the return is executed successfully in the method, then the following code will not run.

3. Method invocation

Calling method: object name. Method name (argument list)

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

When a method returns a value, the method call is usually treated as a value. For example:

Int larger=max (30pr 40)

If the return value is void, the method call must be a statement. Such as:

System.out.println ("hello!")

IV. Overloading of methods

Overloading is a function that has the same function name but different parameters in a class.

Rules for method overloading:

1. The method name must be the same.

two。 The parameter list must be different (different number, or type, different order of parameters, etc.).

3. The return type of the method can be the same or different.

4. Just a different return type is not enough to overload a method.

Realization theory:

When the method name is the same, the compiler will match one by one according to the number and type of parameters of the called method to select the corresponding method. If the matching fails, the compiler will report an error.

Examples of overloading are as follows:

Public class Demo05 {public static void main (String [] args) {/ / call addition int sum=add of type int (2pime3); System.out.println (sum);} / define an addition public static double add of type double (double A1 and double a2) {double a1doua2; return a } / / define an addition of int type public static int add (int A1 minint a2) {int aqa1 addition a2; return a;} / * the addition of double type and int type defined by these two methods have different parameter types and the same method name. * /}

Variable parameter

In the method declaration, add an ellipsis (… ). Such as: public void test (int … i). Int... I can be understood as an array

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

Public class Demo07 {public static void main (String [] args) {Demo07 demo07=new Demo07 (); demo07.test (1 public void test 556 556 556 15 1212);} public void test (int aLint. I) {/ / int...i must be the last parameter of the method System.out.println (I [0]) / / I [0] extract the first number System.out.println (I [1]) in int...i (can be understood as an array); / / extract the second number System.out.println (I [2]); System.out.println (I [3]); System.out.println (I [4]);}} V, Recursive

Recursion is: a method calls A method! Is to call yourself.

Recursion can be used to solve some complex problems with simple programs. It usually transforms a large and complex problem layer by layer into a smaller problem similar to the original problem to solve, and the recursive strategy only needs a small number of programs to describe the repeated calculations needed in the process of solving the problem. the amount of code of the program is greatly reduced. The ability of recursion lies in defining an infinite set of objects with finite statements.

The recursive structure consists of two parts:

1. Recursive header: when not to call your own method. If there is no head, it will fall into an endless cycle.

two。 Recursive body: when you need to call your own method.

/ / Recursive public class Demo08 {public static void main (String [] args) {int baffle (4); System.out.println (b);} / / hierarchy (call yourself) public static int f (int n) {if (next1) {return 1;} else {return nqf (NMU1) / / n * (n Mel 1) * (n Mel 1-1) *. * 1}} so far, I believe you have a deeper understanding of "what is the method in Java language and how to use it". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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