How to define a method in Java

Source: Internet
Author: User

The so-called method, which is used to solve a class of problems, is an orderly combination of code, is a functional module.

In general, the syntax for defining a method is:

which

1, Access modifier: The method allows access to the scope of the permission, can be public, protected, private can even omit, where public means that the method can be called by any other code, the use of several other modifiers in the later chapters will explain in detail the drop

2, return value type: The type of the return value of the method, if the method does not return any value, the return value type is specified as void, if the method has a return value, you need to specify the type of the return value, and return the value in the method body using the return statement

3. Method Name: The name of the method defined, must use a valid identifier

4. Parameter list: Parameter list passed to method, parameter can have multiple, multiple parameters separated by commas, each parameter consists of parameter type and parameter name, separated by space

The method can be divided into four classes depending on whether the method has parameters or not, and whether it has a return value:

? No parameter no return value method

? Non-parametric return value method

? No return value method with parameter

? Return value method with a parameter

Use of no parameter-return-value method in Java

If the method does not contain a parameter, and there is no return value, we call it a method without parameters and no return value.

The use of the method is in two steps:

The first step is to define the method

For example, the following code defines a method named show with no parameters and no return value, and the operation is output "Welcome to IMOOC."

Attention, OH:

1, the method body is placed in a pair of curly braces, to achieve a specific operation

2, the method name is mainly used in calling this method, you need to pay attention to the naming of the specification, generally use the first letter lowercase, other words capitalized form

The second step is to invoke the method

When you need to invoke a method to perform an action, you can first create an object of the class, and then pass the object name. Method name (); To implement (the concept of classes and objects in the later chapters will be explained in detail, first familiar with grammar, table anxious Oh ~ ~)

For example: In the following code, we create an object named Hello, and then output the information by calling the show () method of the object

The result of the operation is: Welcome to IMOOC.

Use of the non-parametric return value method in Java

If the method does not contain a parameter, but has a return value, we call it a method with no parameter return value.

For example: The following code, which defines a method named Calsum, has no arguments, but the return value is of type int, performs an operation that calculates the sum of two numbers and returns the result

In the Calsum () method, the return value type is type int, so you must return an integer value using return in the method body.

When calling a method with a return value, it is important to note that because the method executes returns a result, the return value is generally received and processed when the return value method is called. Such as:

The result of the operation is: two number of sum:

"Small traps" that cannot be overlooked:

1. If the return type of the method is void, the return value cannot be used in the method!

2. The return value of a method can have at most one and cannot return multiple values

3, the type of the method return value must be compatible, for example, if the return value type is int, you cannot return a String value

Use of no return value method with parameters in Java

Sometimes the execution of a method depends on certain conditions, in other words, it is necessary to provide additional information to accomplish a particular function through the method. For example, the real-life rice cooker can achieve the function of "cooking rice", but only if we have to provide the ingredients, if we do not provide anything, it is really "paddle". We can receive external incoming data information by adding a parameter list to the method, which can be any basic type data or reference type data.

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

The code above defines a show method with a parameter name, which implements the output welcome message.

Calling the parameter method is similar to invoking the syntax of the parameterless method, but must pass in the actual parameter value when called

For example:

The result of the operation is: welcome, Adore Class!

Many times, we refer to the parameter defining the method as a formal parameter, which is used to define the number and type of parameters that the method needs to pass in, and to call the method when the argument is called an argument, which is the value passed to the method that is actually processed.

A question that must not be overlooked:

1. When calling the parameter method, the number, type, and order of the arguments must be guaranteed to correspond to the parameter one by one.

2. When calling a method, the argument does not need to specify a data type, such as

3, the parameters of the method can be basic data types, such as int, double, etc., can also be reference data type, such as String, array, etc.

4, when the method parameter has multiple, multiple parameters are separated by commas

Use of the return value method with a parameter in Java

If the method contains both parameters and return values, we are called methods with the return value of the parameter.

For example: The following code, which defines a show method with a parameter name, returns a String result after the method executes

Call a method with a parameter return value:

The result of the operation is: welcome, Adore Class!

Overloading of methods in Java

Q: What are the overloads of a method?

A: If the same class contains two or more than two methods with the same name, the number of method arguments, the order, or a different type of method, it is called an overload of the method, or it can be said that the method is overloaded. The 4 method names shown below are show, but the parameters of the methods are different and therefore belong to the overloads of the method:

Q: How do you differentiate which overloaded method is called?

A: When calling overloaded methods, Java determines which overloaded method should be called based on the number and type of parameters, and the method that exactly matches the parameters is executed. Such as:

Operation Result:

The basis for judging method overloading:

1. Must be in the same class

2. Same method name

3, the number of method parameters, order or type is different

4, not related to the modifier or return value of a method

How to define a method 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.