Call overloaded method definitions, methods, methods - Day05 - Java foundation

Definition and method of using Java

In the program writing process to write the name of the specified method, and provides data for performing the specified functions, called block method parameters
written in a code block comprises performing one or more statements in a particular grammar definition

In other languages ​​also called a function,

The method consists of five parts:
Public static void main (String [] args) {Code}
modifier: A method for modifying a method having certain "permission"
Return Value: returns the data type of the end of the process running java in not replaced with a void
(if the method definition defines a non-void must be used in the code block return returns the specified data type)
method name: a method of identifying an identifier for their own definition of query method by calling the method name
list of parameters: data input dynamic method call (which may consist of from 0 to infinite variables)
block: Code executed during the particular method of performing

Format:
Modifier return type method name (parameter type parameter name 1, parameter type parameter name 2 ...) {
function body;
return value return;
}

Other parameters according to the number 0, or a method return value Have divided into four categories:
None Return Value non-parametric method

for fixing those functions. Fixed code defines a method (defined in this method is not recommended, is not conducive to the code multiplexing)
method has no return value of the parameter
for dynamically performing some of the methods and the results can be directly output shown (but may be defined to minimize, the use of the data convenient)
has no parameters return a value
for the fixed data return (less use, directly define the variables used is not much different)
has a return parameter values are methods
may be applied to most of the methods defined (using up most improve the function of the whole point)

The method defined dynamically select a relatively sophisticated methods based on actual demand

Method calls Note
method does not call does not execute
method and the method is same level relationship can not be nested definitions
define methods when separated by commas between parameters
when invoked method is not used in passing data types
If the method has a clear return values, certain It has brought back a return value

The benefits of the method
1, improve code reusability, reduce redundant code
(the same code can be called multiple times by the method name, reduce the number in the same scope duplicate code appears)
2, easy to modify and maintain the code, Add
(when there is a problem of the code error method of directly modifying the wrong approach appears functional positioning methods)
, subject to certain functional method for defining careful when dividing method definition, each method performs only one function per se do not appear a method for processing a plurality of functions

The method of calculating the return value is derived from the parameters return value but does not necessarily coincide with the data type of the parameter

Java method definition and invocation shaped involved in arguments
concept • formal parameters and arguments
• parameter: called "formal parameters" are parameters used in the definition of the method name and the method body time to receive a call the method when the incoming actual value (variable argument list declaration)
• argument: is called "actual parameters" is passed when calling the method to the actual value (specific value) of the method
• Note
• when you call the method given the actual parameter type to the formal parameter type is defined when the same method, but also the same order
// method invocation process
// first of all find a way to execute based on the method name, parameter list positioning
// we will call the method when writing arguments parameter passed to
// the value corresponding to the location assigned to the corresponding variable transmission position
// declaration corresponding to a plurality of variables, but not during the process of assignment statement
// when the method is invoked for an order in accordance with the variable declared as a variable assignment (If the data type does not match method does)

Method overloading
methods can be the same name in the same scope it?
May, but does not allow two identical methods exist
method call the specified method is a code block is executed by the execution jvm
Jvm virtual machine on behalf of a given sense dynamically call a method which calls a method of a method according to the list name and parameters of the call

Two methods of the same name is defined according to the process defined in claim specifies a method called overload occurs

In the same class, the same method name, a list of different parameters, then we call the overloaded methods happened, or two mutually overloaded methods

For a list of different overloads method
1, the number of different parameters
2, the same number of parameters, the different types of data
3, the same number of parameters (two or more), the same (two or more) data types, order of the different
method overloading nothing to do with the parameter name (parameter name)

Select a virtual machine to execute the process corresponding method
1, a method to find if there is only one method to specify parameters passed for execution (for automatic type conversion) according to the method name to call
2, if the method has a plurality of parameters each parameter list if the data type can not find a match continue to find whether there is a method called by matching the parameters of automatic conversion of the order
3, according to final data type method name and parameter list of parameters in order to obtain the specified method of execution and assignment

Released eight original articles · won praise 0 · Views 99

Guess you like

Origin blog.csdn.net/Rockandrollman/article/details/103933350