The methods in Java

Source: Internet
Author: User

Method:

I. What is a method?

  is a code snippet with a name. Applies to a function. The definition of a method only guarantees that the function is available only if the function is implemented.

two. What is the purpose of the method?

For the reuse of code. (same type of operation, no duplicated write code)

Three. Composition of the method. (also declaration of methods)

  Method-decorated list characters (writable) method returns a value type method name (parameter 1, parameter 2, Parameter 3 ...) {

Execute the statement;

}

   eg: pbulic static void M1 (int a, int b) {int c = a + b;}

1. The method modifier list is optional: Permissions controlled by: public protected private/static

2. Return value type: any of 11 data types.

3. No return value: void

4. Method name as long as it is a valid identifier

5. Method parameter list, multiple comma separated, formal parameter also to indicate data type;

Formal parameter: Refers to the argument at the method; Data type variable name, data type variable name,...

Argument: Refers to the parameter passed in at the method call.

6. Within the method body, if there is a return value (type), use the return statement, and return has the function of the end.

Four. Classification of methods:

1. member Method 2. Static method 3. Construction method

Five. Invocation of the method:

1. Question of the order in which the program statements are executed:

In addition to the traditional from the top down, from left to right

Because of the way the function appears, you can write the code in any order, without having to consider performing a subordinate relationship.

2. When the method is defined, that is, where the code is written, the place of execution is where the function is called, the function name, the parentheses, the arguments, and then the return value assigned to a variable.

3. Invocation of the method:

It can only be called in Main.

When calling, use only one name and pass different parameters on it. (Direct output)

       eg:System.out.println (5);

        SYSTEM.OUT.PRINTLN (6);

System.out.println (7);

The class name. static method name, parentheses, parameters, return value to a variable;

     eg:  M1 (100,200);

The new object assigns a value to a variable: the name of the object variable. The method name, the parentheses parameter, assigns the return value to a variable;

      eg: md.m2 (10,11);

The callback function, the parameter of the function is also a function, the parameter of the method is a method.

      eg: System.out.println (Colculate.sum (10,20));

Six. Process of method execution

   Method Execution Principle:(method call only executes, not call not executed)

Method divides the method in memory when it is called.

If the method does not call, space is no longer partitioned in memory

Method call, from the memory data structure is "stack" (open space in the stack);

The end of the method, from the memory data structure is the "bomb stack";

Seven. Overloading of methods:

In the same class, the method name is the same, and the parameter class table is called the method overload;

Parameter list is different, can be different number of parameters, can be different parameter data type;

Overloads and return values of the method, regardless of the modifier list if the method name is the same and the argument list is the same, the overload of the method is an error.

 Public classmethod{ Public Static voidMain (string[] args) {intJ; System.out.println (5 ); System.out.println (6 ); System.out.println (7 ); System.out.println (Colculate.sum (10,20) ); System.out.println ("Helloword" ); M1 (100,200); Method_01_ MD=Newmethod_01_ (); MD.M2 (10,11); }     Public Static voidM1 (intAintb) {System.out.println (a+ "+" + B + "=" + (A +b)); }}classcolculate{ Public Static intSumintAintb) {        returnA +b; }     Public Static intSumintAintBintc) {        returna+b+C; }}classmethod_01_{ Public voidM2 (intAintb) {a= b++; b= 10;    System.out.println (a); }                }

The methods in Java

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.