Vii. method Definition:
Method is a block of code that accomplishes a specific function
In many languages there are definitions of functions
Functions are called methods in Java.
Format:
Modifier returns a value type method name (parameter type argument name 1, argument type parameter Name 2 ...) {
function body;
return value;
}
Description
More modifiers, public static, etc.
The return value type is used to qualify the data type of the return value
Method name a name, in order to facilitate our call method
Parameter types qualify the data type of the parameter passed in when the method is called
The parameter name is a variable that receives the arguments passed in when the method is called
Code for method Body completion function
Return End method and return method value of the specified type
Returns the result returned by the return value of the program returned to the caller
Precautions:
method does not call do not execute
Methods and methods are peer relationships and cannot be nested defined
The parameters are separated by commas when the method is defined
Pass a variable or constant value directly at the time of the method invocation
If the method has a definite return value, be sure to have return bring back a value
Method invocation:
Method calls with a definite return value:
Separate call, no meaning
Output calls, which make sense, but do not necessarily output the results
Assignment invocation, recommended way
function calls with no explicit return value:
is actually called by the Void type method
Can only be called separately
Method Overloading Overview
In the same class, more than one method of the same name is allowed, as long as the number of arguments or the type of the parameter are different.
Characteristics
Regardless of the return value type, see only the method name and argument list
The JVM distinguishes the same name method by different argument lists when it is called
Viii. Overview of Arrays:
An array is a collection of multiple elements that store the same data type. can also be seen as a container.
Arrays can store either the base data type or the reference data type.
Format:
Format 1: Data type [] array name;
Format 2: Data type array name [];
Note: These two definitions are done, and there are no element values in the array.
Initialization: Overview
Arrays in Java must be initialized before they can be used.
Initialize: Allocates the memory space for array elements in arrays and assigns values to each array element.
Initialization mode
Dynamic initialization: Only the array length is specified when initializing, and the initial value is assigned by the system array.
Format: data type [] Array name = new data type [array length];
The length of the array is actually the number of elements in the array.
Example:
int[] arr = new INT[3];
Explanation: An array of type int is defined, which can hold a value of 3 int type.
Static initialization: Specifies the initial value of each array element when initialized, and the system determines the length of the array
Format:
data type [] Array name = new data type []{element 1, element 2,...};
Example:
int[] arr = new int[]{1,2,3};
Explanation: An array of type int is defined, which can hold 3 values of type int, and the values are three-to-one.
Actually, there's a simplified notation for this.
Int[] arr = {A-i};
Memory Allocations in Java
Java programs need to allocate space in memory at run time. In order to improve the efficiency of computing, space is divided into different regions, because each region has a specific way of processing data and memory management.
Stack store local Variables
Heap storage new out of things
Method area
Local method area (and system-dependent)
Register (for CPU use)
Graphic:
Array operations common two small problems array index out of bounds
ArrayIndexOutOfBoundsException
Occurs when an index that does not exist in the array is accessed.
Null pointer exception
NullPointerException
An array reference does not point to an entity, but when an element in an entity is manipulated.
A two-dimensional array overview
A two-dimensional array is an array of one-dimensional arrays of elements.
Format Format 1:
data type [] Variable name = new data type [m][n];
M indicates how many one-dimensional arrays of this two-dimensional array
n represents the number of elements per one-dimensional array
Example:
int[][] arr = new int[3][2];
Defines a two-dimensional array, arr
This two-dimensional array has 3 one-dimensional arrays, the name is arr[0],arr[1],arr[2]
Each one-dimensional array has 2 elements, which can be obtained by arr[m][n]
Represents the first N+1 element of the m+1 one-dimensional array
Format 2:
data type [] Variable name = new data type [m][];
M indicates how many one-dimensional arrays of this two-dimensional array
This time the number of elements of a one-dimensional array is not given directly, and can be given dynamically.
Example:
int[][] arr = new int[3][];
Arr[0] = new INT[2];
ARR[1] = new Int[3]
ARR[2] = new INT[1];
Format 3:
data type [] Variable name = new data type [][]{{element ...},{element ...},{element ...}};
Simplified format:
data type [] Variable name = {{element ...},{element ...},{element ...}};
Example:
Int[][] arr = {{1,2,3},{4,6},{6}};
Nine, keyboard input data overview
We are currently writing programs, the data values are fixed, but the actual development, the data value is definitely a change, so I am going to improve the data to keyboard entry, improve the flexibility of the program.
Keyboard input data
Guide package (position on top of class definition)
Import Java.util.Scanner;
Creating objects
Scanner sc = new Scanner (system.in);
Receive data
int x = Sc.nextint ();
Java basic Syntax (iii)