1. Method:
The method is to wrap up a piece of code and give the code a name. When you use this code, call the name.
1. The method must be applied in the class
2. Methods in addition to methods and classes, and other how to write it.
3. Methods can call "Learn Java, to the Kaige123.com school" other methods, but cannot call each other, there will be a method nested call overflow error
"Java.lang.StackOverflowError"
4. Delivery is primarily by value (base data type) and by reference.
5. Set parameters
A data type plus three points plus a variable name
static void A1 (INT...B) {}
And
static void A2 (int[] b) {}
This writing is actually the same meaning.
A1 (New int[] {123,456,798});
A2 (123,456,789);
6. Method overloading
Method name, multiple methods with different parameter types. is called method overloading.
It invokes the method of the corresponding parameter type according to the different types of arguments given.
For example:
public class Test6 {
static void method(int a) { System.out.println("int");}static void method(byte a) { System.out.println("byte");}static void method(short a) { System.out.println("short");}static void method(float a) { System.out.println("float");}static void method(double a) { System.out.println("double");}static void method(char a) { System.out.println("char");}public static void main(String[] args) { byte b = 100; method(‘A‘);}
}
6. Method return value
The value returned when the method finishes executing. Only one data type can be returned.
Keyword: return; In a method that has no return value, is the meaning of the end method.
In the method that has the return value is the end and return the value out of the meaning.
For example:
static int A1 () {
return 0;
}
The return value type is type int, and return goes out with a value of zero.
6. Method recursion
is a self-nesting of their own "learning Java, to the Kaige123.com Academy" algorithm. For example, find a folder or find files, open a clip and a clip, and open a clip, know to find the desired file.
For example:
static int method (int i) {
if (i = = 1) {
return 1;
}
Return i + method (i-1);
}
Will I be equal to 1, 1 will return 1. Not equal to 1 Reyurn i+ calls its own method (I-1). The parameters given are I minus 1. Always loop down to know I equals 1. Then return the value on a layer of layer.
Method (3);
Return i + method (3-1); 3+3
Return i + method (2-1); 2+1 up.
The result is equal to 6.
?
-java Basics-Methods