what is a method
A Java method is a collection of statements that perform a function together, such as driving a driver, teaching a teacher, etc.
method is an orderly combination of steps to solve a class of problems
Methods in classes and objects
Method is created in the program and is referenced elsewhere
Advantages of the method
Make the program shorter and clearer;
Facilitates maintenance
Improve program development efficiency
Increase the weight of the code (Cong) with the use of more than one write to eat
Definition of a method
In general, define a method as follows
Modifier returns the type method name (parameter type parameter name, parameter type argument name 2) {... Method body ... return value;}
Modifier |
Optionally, tells the compiler how to compile the method, and defines the type of access for the method. The modifiers of the method are divided into Access control modifiers and non-access control modifiers. The modifiers are then specifically learned. |
return type |
The return value type of the method, if the method does not return any value, is void. |
Parameter type |
Used to qualify an incoming data type when calling a method. |
Name of parameter |
A variable is used to receive parameters that are received by the calling method. |
Return |
The value that is used to end the method and return the method to the specified type. The method return value must be the repeating type of the method declaration. If there is no return value, that is, the return type is void, then return can be omitted.
|
return value
|
The value returned by the Renturn statement is returned to the caller.
|
Method Body: |
The method body contains specific statements that define the functionality of the method. |
Parameter list ( argument type parameter name 1, argument type parameter name 2) |
Used to describe the type of parameter that needs to be accepted when the method is called. The parameter is optional and can be written without any parameters () only; |
Method name |
Custom names, but to conform to the naming conventions, preferably using the lowercamelcase style, must be in accordance with the Hump form |
Example:
public static int Age (Int.) {...}
Multiple parameters
static float GetNumber (float a,float b) {...}
Invocation of the method
Java supports two methods of calling methods:
When the program calls the method, it gives control to the method being called, and returns to the program when the returned statement of the invoked method is executed or when the method body is closed in parentheses.
When a method returns a value, the method call is usually treated as a value;
Invocation Mode: Object name. Method Name
int larger = max (30, 40);
When the return value of a method is void, the method invocation must be a statement such as:
System.out.println ("Hello world! ");
Example: defines a Getarea () method for calculating the area of a rectangle, where the parameter x and Y are used to receive the height and width of the incoming call method, and the return statement is used to return the computed area. In the main () method, the area of the rectangle is obtained by calling the Getarea () method, and the result is printed. method println returns void. The following call is a statement:
public class MethodDemo01 {public static void main (string[] args) {int area = Getarea (3, 5);//Call Getarea method Sy Stem.out.println ("The area was" + area);} The following defines a method for calculating rectangular area, receiving two parameters, where x is high, Y is wide public static int Getarea (int x, int y) {int temp = x * y;//Use variable temp to remember the result of the operation RET Urn temp; Returns the value of the variable Temp}}
The head of the main method is invariant, as shown in the example, with modifier public and static, return void type value, method name is main, plus one string[] type parameter. String[] Indicates that the parameter is an array of strings.
Procedure for calling a method
From the Mian method entry, start executing the program
Start executing the program from the code of the Mian method
Call method Getarea, passing arguments 3 and 5 in. (The parameters are passed in order)
Getarea method starts execution
Calculate temp=w*h=30
return temp; Returns the data after the return to the caller of the method.
After the method is defined, the parameter variable w,h does not have a specific value, and when called, the value is passed over by passing it.
The parametric variable of the method is called the formal parameter, the parameter intgetarea (int w,int h) WH is the formal argument
When the method is called, the data passed is the actual parameter, that is, the argument int a= Getarea (A, A, b) AB is the argument
The JVM divides the memory into 5 regions.
Register: between memory and CPU
Local method Stack: The JVM invokes the functionality of the system,
Method and data sharing area: Run time Claa file in the place
Method Stacks: All methods run when the memory is entered
Heap: Stores containers and objects. For example, arrays
Method call Memory Graph
During program run time, you must enter memory
After running the file, compile the class file inside the method and data sharing area
The JVM virtual machine calls the Mian method,
Main method Start method stack Run
Call method Getarea in Mian method and pass parameters
Getarea method in-stack operation
The result of the calculation is returned by the caller.
Method Getarea end the stack, freeing the memory resource
Considerations for method Definitions:
Method cannot be defined in another method
Wrong method name
Wrong List of parameters
Method Duplicate definition problem
When calling a method, the return value is void and cannot be unloaded in the output statement
Method parameters
Pass by value: The method accepts the value supplied by the caller
Pass by reference : The method accepts the variable value corresponding to the variable supplied by the caller.
A method can modify the value of the variable that corresponds to the passed reference, and cannot change the value of the variable that corresponds to the passed value
Java programming languages are always called by value
There are two methods of method parameters:
Basic data type:
Object references:
Basic data type Examples:
Publci static void value (int x) {
x = 3 * x;
}
Call this method
int a = 10;
Value (a)
Result A is 10
Either way, after calling this method, the value of a is still 10, and the specific process
Parameter x is initialized with a copy of value A (i.e. 10)
X by 3 equals 30 but A is still 10;
X is not in use after method end
A method cannot modify a parameter of a base data type,
Object reference Examples
public class methoddemo_3{
public static void Main (string[] args) {
Int[] arr = {1,2,3,4};
System.out.println (arr[2]); 3
Change (arr);
System.out.println (arr[2]); 100
}
public static void Change (int[] arr) {
ARR[2] = 100;
}
}
Summary: A method cannot modify the parameters of a basic data type (numeric and Boolean)
A method can change the state of an object parameter
A method cannot have an object reference a new object
Method overloading
If more than one method has the same name and different parameters, the overload is generated. The compiler must pick the specific execution of that method, and the type of arguments given by the various methods and the specific method call
Use the type to match to pick out the appropriate method. If the compiler cannot find a matching parameter, or if it finds multiple possible matches, a compile-time error occurs. This process is called overload resolution,
Java runs overloaded with any method.
Therefore, to fully describe a method needs to indicate the method name and parameter type, which is called the signature of the method.
Return type is not part of a method signature that is, you cannot have two names, same parameter types, but return different types of methods
public class methodoverloaddemo{
public static void Main (string[] args) {
For overloaded calls, differentiate according to parameter passing
System.out.println ();
Double sum = getsum (2.3,3.5);
SYSTEM.OUT.PRINTLN (sum);
}
/*
Summing the parameters, using the overloaded properties of the method
*/
public static int getsum (int a,int b) {
SYSTEM.OUT.PRINTLN ("two int parameters");
return a+b;
}
public static int getsum (int a,int b,int c) {
SYSTEM.OUT.PRINTLN ("three int parameters");
return a+b+c;
}
public static double Getsum (double a,double b) {
System.out.println ("Two double parameters");
return a+b;
}
/*
Define the method, sum the parameters
parameters, not specified, data type
public static int Getsum (int a, int b) {
return a+b;
}
public static double getsumdouble (double a,double b) {
return a+b;
}
public static int getSum3 (int a,int b, int c) {
return a+b+c;
}*/
}
Fifth day Java