Java basic syntax (4)

Source: Internet
Author: User

Java basic syntax (4)
Method 7

How to define the so-called methods in Java is an orderly combination of code used to solve a type of problem. It is a functional module. Generally

Syntax for defining a method is:

Where:

1. access modifier: the permitted range of methods, which can be public, protected, private, or even omitted. public indicates

This method can be called by any other code.

2. Return Value Type: Type of the method return value. If the method does not return any value, the return value type is specified as void. If the method has a return value

You must specify the type of the return value and use the return statement to return the value in the method body.

3. Method Name: the name of the defined method. A valid identifier must be used.

4. Parameter List: a list of parameters passed to a method. Multiple parameters can be included. Multiple parameters are separated by commas (,). Each parameter consists of the parameter type and parameter name.

And are separated by spaces.

Methods can be divided into four categories based on whether the method has a parameter or a returned value:

1) method without parameters and return values;

2) Return Value Method without parameters;

3) method without returning values;

4) Return Value Method with parameters;

(1) use of methods without parameters and return values

If a method does not contain a parameter and has no return value, it is called a method without a parameter or a return value.

The method can be used in two steps:

Step 1: Define the Method

For example, the following code defines a method named show with no parameters and no return value. The operation to be executed is to output "welcome

Imooc ."

Note the following:

1. Place the method in a pair of braces to perform specific operations.

2. The method name is mainly used when calling this method. Note the naming rules. Generally, the first letter is lowercase, and the first letter is the first letter of other words.

In upper case.

Step 2: Call the Method

When you need to call a method to execute an operation, you can first create the class object, and then implement it by the object name. Method Name.

For example, in the following code, we create an object named hello and call the show () method of the object to output information.

Running result: welcome to imooc.

(2) Use of the return value method without Parameters

If a method does not contain parameters but has returned values, it is called a method without parameters and return values.

For example, the following code defines a method named calSum with no parameters, but the return value is an int type method. The operation executed is to calculate two numbers.

And returns the result.

In the calSum () method, the return value type is int. Therefore, you must use return to return an integer in the method body.

When calling a method with a return value, you must note that because a result is returned after the method is executed, the method with a return value is generally received

Return Value and process it. For example:

Running result: the sum of the two is 17.

Note the following:

1 If the return type of a method is void, return values cannot be used in the method.

2. the return value of the method can only have one value, but cannot return multiple values.

3. the type of the method return value must be compatible. For example, if the return value type is int, the String type value cannot be returned.

(3) Use of methods without returning values

Sometimes the execution of a method depends on certain conditions. In other words, to complete a specific function through a method, additional information rows must be provided for it.

You can add a parameter list to the method to receive external incoming data information. A parameter can be of any basic or reference type.

Let's first look at a method with parameters but no return value:

The code above defines a show method with a parameter name to output welcome messages.

The syntax for calling a method with parameters is similar to that for calling a method without parameters, but the actual parameter value must be input during the call.

For example:

Running result: Welcome, love class!

Most of the time, we call a parameter when defining a method as a form parameter to define the number and type of parameters to be passed in a method.

The parameter is called a real parameter and is the value that is passed to the method to be processed.

Note the following:

1. When calling the method with parameters, make sure that the quantity, type, and order of the parameters correspond to the parameters one by one.

2. When calling a method, you do not need to specify the data type for the real parameter, as shown in figure

3. The parameters of the method can be basic data types, such as int and double, or reference data types, such as String and array.

4. When there are multiple method parameters, multiple parameters are separated by commas.

(4) use of the return value method with Parameters

If a method contains both parameters and return values, it is called a method with parameters and return values.

For example, the following code defines a show method with a parameter name. After the method is executed, a String type result is returned.

Call the method with parameters and return values:

Running result: Welcome, love class!

(5) method overloading in Java

If the same class contains two or more methods with the same name, number of method parameters, order, or type, it is called the method weight.

Or the method is overloaded. The names of the four methods are show, but the parameters of the methods are different. Therefore, all methods are overloaded:

When you call a method that is overloaded, Java determines which method to call based on the number and type of parameters.

Executed. For example:

Running result:

Basis for determining method overloading:

1 must be in the same class;

2. The method names are the same;

3. The number, sequence, or type of method parameters are different;

4. It is irrelevant to the modifier or return value of the method;

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.