Java Programming Basics-Methods

Source: Internet
Author: User

1. Summary of methods (functions)(1), meaning: a method (function) is a piece of independent applet defined in a class that has a specific function. (2), method-defined syntax format:

Modifier returns a value type method name (parameter type argument name 1, argument type parameter name 2,.. ){

EXECUTE statement

......

return value;

}

Format Description:

Modifier: The method has more modifiers, has the ability to qualify access, static modifier statics, and final modifier final.

Return value type: The data type used to qualify the method return value.

Parameter type: Used to qualify the data type of the parameter passed in when the method is called.

Parameter name: is a variable that is used to receive incoming data when the method is called.

return keyword: Used to end the method and return the value of the specified type of method.

Return value: The value returned by the return statement that returns a value of the type specified by the method.

(3), special attention:

A) in the method "parameter type argument name 1, parameter type argument Name 2" is called the parameter list, it is used to describe the method when the call needs to accept parameters, if the method does not need to receive any parameters, the parameter list is empty, that is, no content is written inside.

b) The return value of the method must be the return value type of the method declaration, and if there is no return value in the method, the return value type is declared void, at which point the return statement in the method can be omitted.

c) The method executes only if it is called. Functions can only be called functions, and functions cannot be defined in functions. the smallest functional unit of the J Ava is the function.

(4), the definition of two clear methods:

First, what is the final result of a clearly defined function? The explicit return value type.

Second, is it clear that there is an unknown content involved in the process of defining the function? clear parameter list.

The simple code sample demonstrates:

[Java] view plain copy
  1. Public class Demo {
  2. public static void Main (string[] args) {
  3. int add = Add (5, N); Call the Add () method to receive the result with an int type variable add
  4. System.out.println (add); //Print results
  5. Printinfo (); //Call Printinfo () method
  6. }
  7. public static int Add (int i, int j) {//with two int type parameter, return value int for two integer method
  8. int sum = i + j;
  9. return sum;
  10. }
  11. public static void Printinfo () {//method with no return value type
  12. System.out.println ("Hello world!");
  13. //return; can omit
  14. }
  15. }

2. Method overloading

(1), meaning: Java allows you to define multiple methods with the same name in a program, but with different parameter lists, this is the overloaded method.

The overloads of a method are independent of the return value type, which has only two conditions, one is the same method name, and the other is a different argument list. (Note the difference from overriding)

Different parameter lists include different number of parameters, different parameter types, and different order of parameters. The overload is easy to read and optimizes the program design.

(2), when to use overloading?

When the definition function is the same, but the participation operation unknown content is different. Then, define a function name representation function, easy to read, and through the different parameter list to distinguish multiple functions with the same name.

(3), overloaded code example:

[Java] view plain copy
  1. Public class Demo {
  2. public static void Main (string[] args) {
  3. System.out.println ("First:" + Add (5, 2));
  4. System.out.println ("Second:" + Add (5, 2, 12));
  5. System.out.println ("Third:" + Add (5.8, 2.9));
  6. }
  7. public static int Add (int x, int y) {//for two integers and
  8. return x + y;
  9. }
  10. public static int Add (int x, int y, int z) {//for three integers and
  11. return x + y + z;
  12. }
  13. public static double Add (double x, double y) {//For two values of type double and
  14. return x + y;
  15. }
  16. //Error Example
  17. //public static int Add (int a,int b) {///and the first method is the same, an error.
  18. //return a+b;
  19. // }  
  20. }

3. Method recursion(1), meaning:

Recursive method refers to the process of invoking itself within a method, recursion must have an end condition, otherwise it will fall into the state of infinite recursion, can never end the call.

(2), export : When doing recursive algorithm, must take the exit, that is, to do recursive algorithm must have a definite recursive end condition. This is very important. In fact, this export is very well understood, is a condition, when we meet the conditions of the time we will no longer be recursive.

The code sample demonstrates:

[Java] view plain copy
  1. Public class Demo {
  2. public static void Main (string[] args) {
  3. System.out.println (Recursion (5));
  4. }
  5. public Static long recursion (int n) {//a recursive invocation of the factorial of a number
  6. if (n = = 1) {//export
  7. return 1;
  8. }
  9. return recursion (n- 1) * n;
  10. }
  11. }
Simple analysis: The use of recursive method to achieve a number of factorial, very important point, a clear recursive end of the condition, that is, when the number of 1 is no longer recursive, return the final recursive results. namely 5*4*3*2*1=120

Java Programming Basics-Methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.