Java Chapter Fifth method definition and invocation

Source: Internet
Author: User

    

1, the definition of the method

What is a method

A method is a set of statements that complete a function, often written in a common function as a method

Definition of a method

[Access control character] [modifier] Returns a value type method name (parameter type form parameter, parameter type parameter,,...))
{
Method body
}

    modifiers : Public, static are called modifiers (they are explained in detail later);
    return value type : The type used to describe the result of the method operation. If other types are returned, compilation can be error-prone;
    Method Name : It is used as the identity of the reference method at the time of invocation;
    parameter list : The number of parameters for a method can be 0 to more, each argument is preceded by the data type of the argument, and each parameter is separated by commas. You can also have none of the parameters.
    method Body : It is a statement block that performs a specific function operation. For a method with a return value type, the last statement in the method body is the return keyword, which is the function of returning the result of the execution of the method to the outside of the method.
    return expression : here, for further analysis, the expression after return is the return value of the method. You need to be aware of the type of the expression, which must match the return type declared in the method header.

    formal parameters : used to accept externally passed-in variables when a method is invoked
    parameter Type : is the data type of the formal parameter
    return value : The method returns data to the program that called it after execution has completed
    return value type : Method The data type of the result to return

public static int square (int x)
{
int y=x*x;
return y; return value
}

Classification of methods

    depending on the number of parameters :
No parameter method
A method of participation

    depending on the return value type :
There are methods for returning values:
Basic data types
Reference data type
Method with no return value
void

For a method with no return value type, it does not return any value to the outside of this method. When defining such a method, the position of the declaring method return type cannot be omitted, instead of the keyword void, meaning "null".

2. Invocation of the method

    Invocation of the method

Method takes effect only after it has been called

Method is called:
Invocation of an argument-free method
Invocation of a parameter method

Invocation of an argument-free method

Method Name ()

   

attention issues in using methods

Parameter must indicate data type

Argument direct write, no type declaration required
Return can only be returned once
A return statement is encountered, the method finishes execution, and subsequent statements do not execute
The return value of the method must match the return value type in the method declaration
Method definition, cannot be written in main ()
methods are not nested.

3. Overloading of methods (overload)

The overload of a method is to allow more than one method of the same name in the same class

Rules for method overloading

Method name is the same

The parameters of the method must be different
The number of parameters is different or
Different parameter types
The return value type of the method can be the same or different

Java Chapter Fifth method definition and invocation

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.