Dry define how Java method calls, method stacks (function stack), the advantages of modular code, function overloading, variable scope

Is a group of statements to complete the combined operation. For example, pre-defined in the Java libraries we first contact methods, such as:. System.out.println, System.exit, Math pow and Math .random, then I will share how to define their own methods and application methods to solve abstract complex issues.

First, the definition method

 
1, the method defined by the method name , parameters , return type , and the method body composition .
 
2, the method defined syntax is as follows :
 
Modifier return type method name (parameter list) {
 
              Method body;
 
}
 
 
 
3. Glossary:
 
1) the first method: the method is a modifier, return type , method name, parameters and methods of composition.
 
Method 2) with a return value: method returns a value
 
. 3)  void Method: The method does not return value
 
4) in the form of parameters: the variables defined in the method is to form the first parameter, also known parameter.
 
5) the actual parameter: when calling the method , gave a passed parameter value , this value is referred to as the actual parameters, also called argument.
 
6) the list of parameters: Type of specified process parameters, and the order number .
 
7) method signature: together constitute a method name and parameter list.
 
Note:
 
1) method may return a value. Some methods only certain requirements to complete the operation, but not the return value . In this case its keyword void . For example : In the main process method returns the data type value is void , in the System . Exit, the System . OUT . PRI NT LN return type of the method is true.
 
2) parameter is optional , that is, the method may not contain parameters. For example : the Math . RandomO method no parameter
 
 
3) In some other languages , a method called procedure ( Procedure ) or function ( fimction ) . The method is referred to as the function return value, the return value of type void method called process .
 
4) In the first method, the need for a separate data type declaration for each parameter . For example : Sub ( int numl, . 1 NT NUM 2 ) is correct , and Sub ( int numl , MIM 2 ) is wrong .
 
5) " define methods " and " declaring variables " nuances . It refers to the definition of what items are defined is that while the statement usually refers to allocate memory for entries is declared to store data .
 
Function icon parameter passing
 

 

 

Second, call the method

 
1, call the method refers to code execution method.
 
2, call the method of approach:
 

1) If the method returns a value, the call to the method usually used as a value deal.

For example: int z = sub (7, 3); call the method sub (7, 3) and the result is attached to the variable z. Another treat it as an example of the value of calls handled are:
System.out.println (sub (7, 3)); return value of this statement calls the print method sub (7, 3).


2) If the method returns void, calling on the method must be a statement.

For example, println method returns void. The following call is a statement: System.out.println ( "Weicome to Java!");

 
Note:
(1) In Java in , with the return value of the method can also be invoked as a statement . In this case , the function caller simply ignore the return value. Although However, this situation is rare , however , if the caller is not interested in the return value , this is also allowed.
 
(2) When a program calls a method , program control is transferred to the called method . When completed the implementation of return when the statement or execution method to indicate the end of a right parenthesis , the method is called program returns control to the caller. For the process of the return value, return statement is required.
 
3, the method can result in the sharing and reuse of code . In addition to TestSub  call sub  method , you can also call it in other classes. If you create a new class , you can use " class name . Method name " ( ie TestSub. Sub  invoked sub) method.
 
4, in the end is how the method is called?

The first: call by object name the method name, which is the most common and most common type is called.

The second: call the constructor method new keywords, this is the method used when the object is instantiated.

Third: the method name invoked by class name, when the need to call a static method (there's static) methods use.
 
Note:
 
1, when calling the method , the need to provide arguments , which must be the same method signature sequence parameter corresponds. This is called the order of the parameters match. Argument must be on the order and number match the parameter defined in the method signature, compatible with the type . Means compatible with the type does not require explicit type conversions value of the argument can be passed to the parameter , e.g. , the int transfer type argument value to the double -type parameter.
 
2, the variable parameter is a process having its own storage space . Local variables are allocated when calling the method, when the method returns to the caller after it disappeared .
 

Third, the method stacks

The method is based on the operation of the stack memory, a container structure after the stack is a last-out, such as bullets into the stack in sequence 123456, 654321 the stack sequentially, each may be
 
Is understood to bullet functions (function frame / stack frame), the frame at the top has a function of a priority operation, the main function of the absolute first into the stack.
 
return current function End -> the current function popping
 
 
The method of flow chart runtime stack
 
 
 
 
 
 
 
 
 

Fourth, modular code

Modular makes code easier to maintain and debug , and so the code can be reused . Use redundant code can be reduced, to improve reusability of code . The method can also be used
 
To modularize the code , in order to improve the quality of the program. By the greatest common divisor code is encapsulated in one approach, the program has the following advantages:
 
1) It calculates the greatest common divisor of problems other code and separated from the main method of doing so will make the logic clearer and more readable program.
 
2) the greatest common divisor of the grate meter error is defined on the gcd method, thus narrowing the range of debugging.
 
3) Now, the other programs can reuse gcd method.

 

Fifth, overloaded methods

 
1, overloaded methods so that you can use the same name to define different ways , as long as they can be different signatures, java compiler determines which method to use based on the signature method.
 
2、重载的优点:可以使得程序更加清楚, 以及更加具有可读性 执行同样功能但是具有不同参数类型的方法应该使用同样的名字。
 
1) 被重载的方法必须具有不同的参数列表 不能基于不同修饰符或返田值类型来重载方法。
2)有时调用一个方法时 会有两个或更多可能的匹配 但是 编译器无法判断哪个是最精确的匹配。 这称为歧义调用 ambiguous invocation ) 歧义调用会产生一个编译错误
考虑如下代码
class Test01{
    public static void main(String[] args){
        System.out.println(add(3,3));
    }
    public static double add(int a,double b){
        System.out.println("int+double");
        return a+b;
     }
    public static double add(double a,int b){
        System.out.println("double+int");
        return a+b;
     }
}
 

六、变量的作用域

 
变量的作用域是指变量可以在程序中引用的范围
 
局部变量:在方法中定义的变量,其 作用域从声明变量的地方开始,直到包含该变量的块结束为止。 局部变量都必须
 
在使用之前进行声明和陚值 。可以在一个方法中的不同块里声明同名的局部变量,但是不能在嵌套块中或同一块中两次声明同一
 
个局部变量,— 个变量可以在非嵌套的块中多次声明,而在嵌套块中只能声明一次。不要在块内声明一个变量然后企图在块外使
 
用它。下面是一个常见错误的例子:
int sum = 0;
    for(int i =1;i<=8;i++){
    sum =sum + i;
    }
    i=sum + 1;
    System.out.print(i);

 

因为变量 i 没有在 for 循环外定义 所以i = sum + 1;就会产生一个语法错误。
 
全局变量在方法外定义的变量,在整个工程文件内都有效;即从定义变量的位置到本源文件结束都有效。由于同一文件中的所
 
有函数都能引用全局变量的值,因此如果在一个函数中改变了全局变量的值, 就能影响到其他函数中全局变量的值。
 
发布了13 篇原创文章 · 获赞 19 · 访问量 2871

Guess you like

Origin blog.csdn.net/MillionSong/article/details/104322049