2. Functions and arrays, 2. Function Arrays
I. Functions
1. Definition:
Modifier Return Value Type Function Name (parameter type parameter 1, parameter type parameter 2 ......) {
Execute the statement;
Return value;
}
Modifier: There are many modifiers in Java, such as public, static, and final modifiers that restrict access permissions.
Return Value Type: specifies the type of value returned by the method, that is, the value type after return
Function Name: a variable used to receive data passed in when a method is called.
Return: keyword, end method, return the value of the specified type of the Method
Return Value: The value returned by return. The value is returned to the caller of the function.
2. functions:
Improves code reusability and functional reusability
3. Function overloading:
More than one function of the same name is allowed in the same class, as long as the number of parameters, parameter types, or Parameter order is different. (Note: the overload of a function has nothing to do with the type of the returned value. It is only related to the parameter list)
4. Notes:
If the function operation does not return a value, "void" is used to represent the return value type. If the return value type is "void", "return" can be omitted"
The function is executed only when it is called.
2. Array
1. Definition:
When defining an array, only the length of the array is specified. The system automatically assigns values to elements by means of dynamic initialization.
Array type [] array name = new data type [number of elements or array length];
When defining an array, each element is assigned a static initialization value.
Data Type [] array name = new data type [] {element 1, element 2 ,......};
Data Type [] array name = {element 1, element 2 ,......};
2. Notes:
The minimum index in the array is 0, and the maximum index is "array length-1 ";
When accessing an array, the index cannot exceed the length of the array, that is, 0 -- array name. length-1; otherwise, the index will be out of bounds.
The variable must point to a valid array. If the value of the variable is null, a null pointer exception will occur if it does not point to any array.
3. default values of various Arrays:
The default values of byte, short, int, and long are 0;
The default value of float and double is 0.0;
The default value of char is space (\ u0000 ";
The default value of boolean is false;
The default value of the referenced data type is null, indicating that no data is referenced;
4. Two-dimensional array:
Format: Data Type [] [] array name = new data type [x] [y];
Data Type [] [] array name = new data type [x] [];
Data Type [] [] array name = {1, 2}, {1, 3, 5}, {1, 3 }};