Java basic Syntax (2)

Source: Internet
Author: User

1. Method 1.1 Method Overview

In our daily life, the method can be understood as the solution to do something and take it.

such as: Xiao Ming students on the roadside ready to study in the school bus. This is faced with one thing (ride to school this matter) need to solve, the solution? Can take the bus or take a taxi way to school, then, this solution to a certain thing, we call the method.

In Java, a method is a way to accomplish something or to implement a function.

Method implementation, there are a number of statements that are used to accomplish some meaningful function-usually working with text, controlling input, or calculating values.

We can implement (or call) the method in the program code by referencing the method name and the required parameters. Methods, which generally have a return value, are used as the result of the processing of the event.

1.2 Syntax format for methods

In Java, the specific syntax format for declaring a method is as follows:

modifier Returns a value type method name (parameter type argument name 1, argument type parameter Name 2, ...). {     Execute     statement ..... return return value;}

For the syntax format above, specify the following:

    • Modifiers: Methods have more modifiers, restrict access, have static modifier statics, and final modifier final, which will be introduced in the subsequent learning process.
    • 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 calling the method
    • Parameter name: is a variable that is used to receive incoming data when the method is called
    • return keyword: Used to end a method and return a value of the specified type of method
    • Return value: The value returned by the return statement, which is returned to the caller

It is important to note that in the method, the parameter-type parameter name 1, parameter-type parameter name 2 is called the parameter list, which describes the parameters that the method needs to receive when it is called, and if the method does not need to receive any parameters, the argument list is empty, that is, no content is written inside. 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.

Next, a case is used to demonstrate the definition and use of the method, as shown below. Methoddemo01.java

1  Public classMethodDemo01 {2      Public Static voidMain (string[] args) {3         intArea = Getarea (3,5);//calling the Getarea method4System. out. println (" the area is"+Area );5     }6 7     //The following defines a method for calculating rectangular area, receiving two parameters, where x is high and Y is wide8      Public Static intGetarea (intXinty) {9         inttemp = x * y;//use variable temp to remember the result of the operationTen         returnTemp//returns the value of the variable temp One     } A}

In the preceding code, a Getarea () method is defined for the area of the rectangle, and the parameter x and Y are used to receive the high and wide of the incoming call method, and the return statement is used to return the computed area. In the main () method, the area of the rectangle is obtained by calling the Getarea () method, and the result is printed.

Java basic Syntax (2)

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.