The use of arrays in java

An array is a collection of a set of data. Each data in the array becomes an element of the array. In java, the array is also an object. The data in the array can be of any type (basic data type and reference data type), but in the same array Store data of the same data type.

Basic data types: Integer: short byte int Long Floating point: float double Character char Boolean: boolean

Reference data type: Array object interface, the default value of reference array type is null

1. Basic data types:

byte : The smallest data type in Java, occupying 8 bits (bit) in memory, that is, 1 byte, the value range is -128~127, the default value is 0

short : short integer, occupying 16 bits in memory, that is, 2 bytes, the value range is -32768~32717, the default value is 0

int : Integer, used to store integers, occupies 32 bits in the inner, that is, 4 bytes, the value range is -2147483648~2147483647, the default value is 0

long : long integer, occupying 64 bits in memory, that is, 8 bytes -2^63~2^63-1, the default value is 0L

float : floating point type, occupying 32 bits in memory, that is, 4 bytes, used to store numbers with decimal points (the difference from double is that the effective decimal point of float type has only 6~7 digits), the default value is 0

double : Double-precision floating-point type, used to store numbers with a decimal point, occupies 64 bits in memory, that is, 8 bytes, the default value is 0

char : character type, used to store a single character, occupying 16 bits, that is, 2 bytes, the value range is 0~65535, the default value is empty

boolean : Boolean type, occupying 1 byte, used to judge true or false (only two values, namely true, false), the default value is false

Second, the definition of the array:

There are three types of array definitions:

1. Array type [] array name = new array type [array length]

2. Data type[] array name={element 1, element 2, element 3, element 4}

2. data type[] array name=new data type[]{element 1, element 2, element 3}

After the length of the array is defined, it is immutable

The first type of defined array is static initialization. When the array is created, the system will automatically set the initial value of each element (default value)

The second type of defined array is a shortcut for creating an array, which is called the literal syntax of an array, which belongs to dynamic initialization.

The third type of defined array is dynamically initialized, and the value of each element is set at the same time when the array is created.

Mostly arrays:

Arrays stored in arrays in java belong to the nesting of arrays, called multidimensional arrays

One-dimensional arrays: One-dimensional arrays store basic data types

Two-dimensional array: A two-dimensional array stores a one-bit array

Array operations:

query array element array name[index];

add array element arrayname[index]=value;

Modify array element array name[index] = new value;

delete array element arrayname[index]=default;

3. Value transfer

Java contains a lot of data, which are often passed to other locations for use

Data transfer method: value transfer address transfer

Pass by value: copy the data in variable i and pass it to variable j

Common value passing:

The 8 basic data types in java belong to value transfer;

Address transfer: copy the memory address contained in variable a and pass it to variable b;

Common address passing: 3 reference data types [array, object, interface]

The difference between pass-by-address and pass-by-value:

Pass-by-value passes the ordinary data contained in the current variable; pass-by-address passes the memory address contained in the current variable;

Whether it is value transfer or address transfer, the data contained in the current variable is passed;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326133562&siteId=291194637