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)06/01 Report--
This article mainly introduces "what is the definition and call of Java method". In daily operation, I believe that many people have doubts about the definition and call of Java method. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the definition and call of Java method". Next, please follow the editor to study!
1. Overview of methods
What is the method?
A method (method) is a code set that organizes blocks of code with independent functions into a whole to make them have book functions.
Note:
Method must be created before it can be used, a process called method definition
Method is not run directly after it is created, but needs to be used manually before it is executed. This process is called method call.
two。 Method definition and invocation 2.1 method definition
Format:
Public static void method name () {
/ / method name
}
Example:
Public static void isEvenNumber () {/ / method name} 2.2 method call
Format:
Method name ()
Example:
IsEvenNumber ()
Note:
Method must be defined and then called, otherwise the program will report an error
2.3 procedure for calling method public class MethodDemo {public static void main (String [] args) {/ / calling method isEvenNumber () } / / requirements define a method in which a variable is defined to determine whether the data is an even number public static void isEvenNumber () {/ / definition variable int number = 10 / / determine whether the changed data is even if (number%2==0) {System.out.println ("the data is even");} else {System.out.println ("the data is odd") } 2.4 method exercise
Requirements, design a method to print the maximum of two numbers
Code example:
Public class text072 {public static void main (String [] args) {isMaxNumber ();} public static void isMaxNumber () {int astat9; int baked 55; if (a > b) {System.out.println ("max is" + a) } else {System.out.println ("max is" + b);}} 3. Definition and invocation of methods with parameters 3.1 definition of methods with parameters
Format:
Public static void method name (parameter) {...}
Format (single parameter):
Public static void method name (data type variable name) {.}
Example (single parameter):
Public static void isEvenNumber (int number) {....}
Format (multiple parameters):
Public static void method name (data type variable name, data type variable name 2) {...}
Example (single parameter):
Public static void isEvenNumber (int number1, int number2,.) {...}
Note:
1. When defining a method, there must be no less data type and variable name in the parameter, and an error report is missing from any program.
two。 When defining a method, multiple parameters are separated by commas
3.2 method calls with parameters
Format:
Method name (parameter)
Format (single parameter):
Method name (variable name / constant value)
Example:
IsNumber (5)
Format (multiple parameters)
Method name (variable name 1 / constant value 1, variable name 2 / constant value 2)
Example:
GetMax (5pr 9)
Note: the number and type of parameters must match the settings in the method definition, otherwise the program reports an error
Sample code: define a method that receives a parameter to determine whether the data is even
Public class text073 {public static void main (String [] args) {/ / constant value call isEvenNumber (5); / / variable call int number = 10; isEvenNumber (number) } public static void isEvenNumber (int number) {if (number%2==0) {System.out.println ("true");} else {System.out.println ("false") At this point, the study of "what is the definition and invocation of Java methods" is over. I hope to be able to solve your 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.
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.