Java programming things 52-method declaration

Source: Internet
Author: User
Tags mathematical functions

Java programming those things 52-method statement Zhengzhou game institute Chen yuefeng from: http://blog.csdn.net/mailbomb Chapter 7 MethodsMethod is called a function in a process-oriented language and a subroutine in an assembly language. It is a code function block that implements a specific function. In actual program development, the method is a basic way to organize code. This section describes concepts related to methods, related syntaxes, and issues that need to be paid attention to during actual use. 7.1 method OverviewThe concept of a method comes from mathematical functions. In mathematics, when data has a certain law, a function is used to code the law of the number, such as F (n) = n indicates 1, 2, 3 ,...... Such a series. In mathematics, n is a parameter, and only one f (n) value corresponds to a certain N value. A method is a set of code blocks to implement specific functions. The syntax functions of the method mainly include: l structured Code organizes the code according to the function, so that the structure of the Code is clear and easy to read and modify, that is, the maintainability of the program is strong. L reduce code repeat a fixed function and may be used multiple times in the program. You only need to call the written method when using it, instead of repeatedly writing the corresponding functional code. When writing a method, you must pay attention to the following two points: l a complete function implemented by a rigorous logical method. Therefore, when writing a method, you must consider various possible situations and make appropriate treatment for each situation. L a versatile method is a function. In actual implementation, you can make the method universal as needed. Unless necessary, do not write a dedicated method. In the Java language, proper use will make the program more elegant and easy to read and use. The syntax format of the method declaration is described below. 7.2 method declarationThe method declaration is written inside the class declaration in the code, and the method declaration is external. The pseudocode is as follows: public class Hello {method declaration 1 method declaration 2 ......} In Java, there is no sequence between method declarations. A method declaration is to declare a new function or create a new function. For example, the following code declares a method to calculate the absolute value of int data: Public int ABS (int n) {If (n> 0) {return n ;}else {return-N ;}} here we have implemented the int value absolute value function. In order to make this function universal, a parameter n is used to represent the value that requires the absolute value. In the method, the absolute value Logic is used: the absolute value of a positive number is itself, and the absolute value of a negative number is the opposite. Return the result of the method operation using the return statement. The syntax format of a specific method declaration is as follows: Access Controller [modifier] Return Value Type method name (parameter list) {method body} The above content needs to be determined in sequence when a method is actually declared. The following is a specific description: 1. The Access Controller limits the visible range of the method, or the scope of the method being called. There are four access control operators for the method. The values range from large to small: public, protected, no access control operator, and private. No access control operator, no keywords. The specific scope will be detailed later. 2. modifier modifiers are optional, that is, they can be left empty during method declaration. Modifiers are used to add specific syntax functions to a method, without affecting the logical functions implemented by the method. There are five access controllers for the method: U static -- Static U final -- Final U abstract -- Abstract U synchronized -- synchronous U native -- specific local modifier will be described in detail later. 3. Return Value Type return value type refers to the result type that needs to be obtained after implementation of the method function. This type can be any data type in Java, including basic data types and composite data types. If no feedback is required after the method function is implemented, the return value type is written as void. When writing a method, you must first consider whether the method needs to feedback the result. If the result is fed back, what is the type of the result? This is determined based on the needs of the method. For example, the method for calculating the absolute value above, the absolute value of the int type is still the int type, so the return value type is converted into the int type. Declare the return value type in the method declaration to obtain the return value when calling the method, and assign values and perform operations on the return value. 4. method name the method name is an identifier used to represent the function block. When calling a method, the method name is required to determine the content of the call. To enhance code readability, the method name identifier and the function of this method are used all the time. For example, you can set the method name to sort to implement the array sorting method. In Java coding specifications, the first letter of the method must be in lowercase, and the first letter between the word and the word interval in the method name must be in uppercase, for example, bubblesort. 5. The parameter list is the data type and number that need to be passed in from the external to declare the method. For example, the method for finding the absolute value of the int Type above needs to input an int type value from the external each time, this requires declaration in the parameter list. Syntax format: Data Type parameter name multiple parameters format: Data Type parameter name 1, data type parameter name 2 ,...... When declaring a parameter, the type is in the front and the name is behind. If there are multiple parameters, use commas to separate the parameters. The value of a parameter is specified when the method is called. Inside the method, the parameter can be regarded as a variable that has been initialized and used directly. The parameter list part is the most important implementation part of method versatility. Theoretically, the more parameters there are, the more universal the method is. When declaring a method, you can determine the number of parameters as needed, and parameter type. The order of parameters in the parameter list is only related to the method call time. 6. The method body is the function implementation code of the method. The method body implements the function of the method logically. This part is the specific implementation code, and the differences between different logic implementation codes will be large. In the method body, if you want to return the result value, you can use the return statement. The syntax format is: return result value; or return if no result is returned; if the return value type of a method is not void, return can be used to return the result value. The return value type must be consistent with that of the method declaration. If the return value type is void, you can use the return statement to return the value without returning the value. When the code is executed to the Return Statement, the method ends, so the code in the subsequent writing order of the Return Statement, for example: Return 0; int n = 0; // syntax error, this statement can never be executed. In addition, if the return value type is not void, you must ensure that a value is returned. For example, the following method has a syntax error: Public int test (int) {if (a <0) {return 0 ;}} then in the declaration code of this method, when the value of A is greater than or equal to zero, no return value is returned. This syntax is called loss of return values, this is an issue that requires special attention when writing a return statement.
Related Article

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.