Java Basics - Methods

1. Method: The method is to wrap a piece of code and give this piece of code a name. Whenever you use this piece of code, you can just call this name.

1. The method must be applied in the class

2. In addition to methods and classes, other methods can be written as they want.

3. The method can call [Learn Java, go to Kaige School kaige123.com ] other methods, but cannot call each other, there will be a method nested call overflow error "java.lang.StackOverflowError"

4. Mainly pass-by-value (basic data types) and pass-by-reference. image

5. Set parameter A data type plus three dots plus a variable name static void a1(int…b){} and static void a2(int[] b){} are actually the same. a1(new int[] {123,456,798}); a2(123,456,789);

6. Method overloading Multiple methods with the same method name and different parameter types. It's called method overloading. It will call the method of the corresponding parameter type according to the different types of parameters 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 after the method is executed. Only any one data type can be returned. Keyword: return; means the end of the method in a method without a return value. In a method with a return value, it means to end and return the value to go out. For example: static int a1(){

return 0; } The return value type is int, and a value of zero is returned. 6. Method recursion is a kind of algorithm that nests itself [Learn Java, go to Kaige School kaige123.com ]. For example, find a folder or find a file, open a folder and another folder, and then open another folder, until you find the desired file. For example: static int method(int i) { if (i == 1) { return 1; } return i + method(i - 1); } Please ask if i is equal to 1, if it is equal to 1, it will return 1. If it is not equal to 1, reyurn i+ calls its own method (i-1). The parameter given is i minus 1. Keep looping until i is equal to 1. Then return the value layer by layer. method(3); return i + method(3- 1); //3+3 return i + method(2 - 1); //2+1 adds up to 6.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327105427&siteId=291194637