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-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "how to use the Java method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

When we learn operators, we need to create a new class and main method for each operator separately, and we will find that writing code in this way is very cumbersome and repetitive.

Whether you can avoid these duplicate codes, you need to use methods to implement them.

01

Definition of method

The method is to extract a function and define the code in a single curly braces to form a separate function.

When we need this function, we can call it. This not only realizes the reusability of the code, but also solves the phenomenon of code redundancy.

Define the format:

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

For example:

Public int getNum (int A1) {int num=a1*2; return num;}

02

Method invocation

After the method is defined, the method will not run on its own, but must be called before it can be executed. We can call our own defined method in the main method main. In the main method, you can simply write the name of the method to be called.

Public static void main (String [] args) {/ / calls the defined method ();} / / defines the method, which is called by the main method public static void method () {System.out.println ("self-defined method that needs to be called and run by main");}

Note:

Methods must be defined in one class. Methods outside methods cannot be defined in another method. "how to use the Java method" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report