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

How to use the Java method

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use Java method". In daily operation, I believe many people have doubts about how to use Java method. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Java method"! Next, please follow the editor to study!

Detailed explanation of Java method

A Java method is a collection of statements that perform a function together.

Steps to solve a class of problems

Included in classes and objects

Create it in a program and use it elsewhere

Principle: a method is a function block, a collection that implements a function, and it is best to maintain atomicity (taking advantage of atomic extension functions).

Naming method: small hump is named

Package com.zhang.func;public class Demo01 {/ / main method public static void main (String [] args) {int sum = add (1Power3); System.out.println (sum);} / / static keyword modifies add_func into a class method, so you can use / / int in the class to indicate the definition of the return type int public static int add (int a, int b) {return astatb;}} 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:

Modifier: optional, which tells the compiler how to call the method, which actually defines the access type

Return value type: the method may have a return value or be empty (void)

Method name: the actual name of the method, the method name and parameters together constitute the method signature

Parameter type: the parameter is like a placeholder. The parameter is passed in when the method is called. The transmitted value is called the argument, and the value that receives the value is called the formal parameter.

Method body: the method body is composed of the code to be implemented by the method

The modifier returns the value type method name (parameter type parameter name) {. The method body. Return return value;}

In addition to the function of returning values, return also has the function of terminating methods.

Method call:

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

When a method returns a value, this method is usually treated as a value.

Int larger = max (40,30)

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

System.out.println ("Hello, world!")

Java is all value passing, no reference passing.

Overload:

Overloading is a class that has the same function name but different parameters. The name is the same and the parameter list is different.

Rules:

The method name must be the same

Parameter list must be different (number | Type | different order, etc.)

The return value can be the same or different

Just a different return value is not enough to constitute a method overload

Theoretical realization:

When the method name is the same, the compiler will match one by one according to the number and type of parameters of the calling method to select the corresponding method, otherwise an error will be reported.

Variable parameters:

Starting with JDK1.5, Java supports passing variable parameters of the same type to a method

In the method, specify the parameter type followed by a class apostrophe (.)

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

Package com.zhang.func;public class Demo04 {public static void main (String [] args) {/ / variable parameter System.out.println (max);} public static int max (int...) Nums) {if (nums.length = = 0) {System.out.println ("array length is zero"); return 0;} int max = nums [0]; for (int I = 1; I < nums.length; iTunes +) {if (max < nums [I]) max = nums [I];} return max;}} recursion:

The method calls itself.

Recursion can be used to implement simple programs to solve complex problems, such as factorial, which usually converts a large complex problem into a smaller problem similar to the original problem. The recursive strategy needs only a small number of programs to describe the repeated calculations needed in the problem-solving process, which greatly reduces the amount of code.

Recursion consists of two parts:

Recursive boundary: the exit condition of a recursive program. If not, there will be an endless loop.

Recursive body: when do you need to call your own method

Package com.zhang.func;public class demo05 {/ / Recursive n! Public static void main (String [] args) {System.out.println (reN (5));} public static int reN (int n) {if (n = = 1) {return 1;} return n * reN (n-1);} at this point, the study on "how to use the Java method" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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