Basic knowledge of Java functions

Source: Internet
Author: User

http://blog.csdn.net/cxwen78/article/details/7322891 mainly from the definition of Java functions, functions, functions of the application, the function of overloading four aspects to explain the Java function.

Definition of a function
A function is a separate program defined in a class that has a specific function, also known as a method .

Note: A function is defined in a class and cannot be defined in a function.


Second, the format of the function


modifier Returns a value type function name ( parameter type form parameter 1, parameter type parameter 2, ... ){
Execute the statement;
return value ;

Example code:

[Java] view Plaincopy
    1. public static int Getsum (int x, int y) {
    2. return x+y;
    3. }

Format Description:

    • modifier: The addition of the function to modify, so that the function has more meaning;
    • return value type: The data type of the returned result after the function is run;
    • function Name: It can be defined by itself, as long as it does not conflict with system keywords. It is suggested that a meaningful name, the writing specification for the verb combination, if it is a multi-word combination, the first letter lowercase, followed by the first letter of the word capital, such as getsum, which means to get the sum of the value.
    • parameter type: refers to the data type of the formal parameter;
    • formal parameter : is a variable that is used to store the actual arguments passed to the function when the function is called;
    • actual parameters: The specific values passed to the formal parameters;
    • return: used to end the function;
    • return value: The result of processing as a function is returned to the caller, whose data type must be the same as the return value type;

Iii. features of functions
    1. The function code can be encapsulated by defining functions;
    2. Easy to reuse the function code;
    3. Functions are executed only if they are called;
    4. The appearance of function improves the reusability of code;
    5. For a function operation, when there is no specific return value, the return value type is represented by the keyword void, and if the return statement in the function is in the last row, it can be omitted.
Attention:
--functions can only be called functions, and functions cannot be defined inside a function. That is to say, the function is a peer, there is no relationship, only call action;
--When a function is defined, the result of the function should be returned to the caller and referred to the caller for processing


iv. Application of Functions
function is a separate function, so before you define a function, there is a " two Clear”:
1, clear the operation result of this function. The purpose is to define the return value type of the parameter;
2, clearly in the process of defining the function is not unknown to participate in the operation of the content. That is, whether the specific contents of the function can be implemented completely independently, or by relying on some of the values that the caller gives us in order to be specific, the purpose of which is to clarify the parameter list of the function (the type of the parameter and the number of arguments).

Principle:
How to define a good function: try to get the function to implement only one function, which can improve the function reuse.


Example code:

[Java]View Plaincopy
    1. /* demand: output a rectangular lattice  
    2.  *  ideas:  
    3.    1, function result is output a square lattice , the return value type is void; ,
    4.    2, there are unknown content involved in the operation, there are two, respectively, representing the length and width of the square, both unknown content types are int type  
    5. */   
    6. Public static void printrectangularlattice (int length, int width) {  
    7.     for (int x=0; x<length;x++) {  
    8.          for (int y=0; y<width;y++) {  
    9.              system.out.print ("*");   
    10.          }  
    11.          System.out.println ();   
    12.     }  
    13.      return ;  
    14. }  



V. Overloading of functions (overload)

1. The concept of overloading: in the same class, more than one function with the same name is allowed, as long as the number of arguments or parameter types of the functions with the same name are different.

2, overloading features: independent of the return value type, only related to the parameter list. That is, the JVM distinguishes the function by its argument list. While the parameter list is the same, a function of the same name with a different return value type cannot exist with one class at a time.

3, the advantages of overloading: Fang easy to read, optimize the program design;

4. Overloading Example:(in the same class)

[Java]View Plaincopy
    1. Returns the number of two integers and
    2. public static int Add (int x, int y) {return x+y};
    3. Returns the number of three integers and
    4. public static int Add (int x, int y, int z) {return x+y+z};
    5. Returns two decimals and
    6. public static double Add (double x, double y) {return x+y};

Basic knowledge of Java functions

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.