In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to define and call Java with parameters and methods with return values". In daily operation, I believe that many people have doubts about how to define and call methods with parameters and return values in Java. Xiaobian consulted all kinds of materials and sorted out simple and useful methods of operation. I hope it will be helpful to answer the doubts of "how to define and call methods with parameters and methods with return values in Java". Next, please follow the editor to study!
Definition and invocation of formal parameters and arguments of methods with parameters
Formal parameters: parameters in method definition
Equivalent to variable definition format, such as int number
Arguments: parameters in method calls
Equivalent to a variable or constant, such as 10, number
Exercise with parameter method
Requirements: design a method to print the maximum of two numbers, and the data comes from the method parameters
Train of thought:
1. Define a method to print the maximum number in two books, such as getMax ()
Public static void getMax () {}
two。 Define two parameters for the method to receive data
Public static void getMax (int a _ line int b) {}
3. Use branch statements to deal with the size relationship of numbers in two cases
If (a > b) {system.out.println (a);} else {system.out.printf (b)
4. Call the defined method in the main method (using constants)
Public static void main (String [] args) {/ / pass the constant getMax directly (10Magne20);}
5. Call the defined method in the main method (using variables)
Public static void main (String [] args) {/ / define variables, passing int adept 10; int baud 20; getMax (aforme b);}
Code example:
Public static void main (String [] args) {/ / TODO Auto-generated method stub getMax (10,20); / / use the constant int axiom 10; int bread20; getMax (a, b) / / use the variable} public static void getMax (int areint b) {if (a > b) {System.out.println (a);} else {System.out.println (b);}} the definition of a method with a return value and call a method definition with a return value
Format:
Public static data type method name (parameter) {
Return data
}
Example:
Public static boolean isEvenNumber (int number) {
Return true
}
Example 2:
Public static int getMax (int a _ line int b) {
Return 100
}
Note: the return value after the return in the method definition matches the data type on the method definition, otherwise the program reports an error
Method call with return value
Format:
Method name (parameter)
Example:
IsEvenNumber (5)
Format 2:
Data type variable name = method name (parameter)
Example:
Boolean Number = isEvennumber (5)
Note:
The return value of the method is usually received using a variable, otherwise the return value will be meaningless
Sample code: define a method that takes a parameter, determines whether the data is even, and returns true or false
Public static void main (String [] args) {/ / data type variable name = method name (parameter) boolean flag= isEvenNumber (10); System.out.println (flag);} public static boolean isEvenNumber (int number) {if (number%2==0) {return true } else {return false;}} at this point, the study on "how to define and call Java with parameters and methods with return values" is over. I hope to be able 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.
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.