Java SE 032 Java array memory address resolution

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must bear the suffering of life. How painful it is Deep comprehension.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Conquering opponents twice, the first time in the heart.
(7) Writing is really not easy. If you like it or have something for you Help remember to like + follow or favorite~

JavaSE 032 Java array memory address resolution

1. Array

A collection of data of the same type is called an array.

2. How to define an array

Method 1: int [] a = new int[4];
Method 2: int a[] = new int[10];
Method 3: int [] a = {1,2,3,4};
Method 4: int [] b = new int[]{1,2,3,4};This method cannot directly specify the number of array elements in [], because the number is determined by the assignment. If specified, in No errors will be reported during compilation, and errors will occur during execution. Specifying it is actually a redundant operation, because the number of elements can be determined when assigning a value.
Indicates that an array of type int is defined. The name is a and the length of the array is 4.

type[] variable name = new type[number of elements in the array];

Or type variable name[] = new type[number of elements in the array]

3. Attention

The index of the element in the array starts from 0. For arrays, the largest index == array index -1.

4. The fourth way to define an array

type[] variable name = new type[]{comma separated list of initialization values}

5. The length property of the array

Every array in Java has a property called length, which represents the length of the array. The length attribute is public, final, int. Once the length of the array is determined, the size cannot be changed.

6. Compare whether the contents in the array are the same

(1) Do not use the equals method. The so-called comparing whether the content is the same is to compare whether the values ​​of the elements at the same position are the same. (2) If the number of array lengths is different, there is no need to compare at all. There is no need for equals because each array is an object, stored in a different space.

int a [] = {
    
    1,2,3};
int b [] = {
    
    1,2,3};
System.out.println(a.equals(b));
false;

(3) This equals method is still the equals method in the extended Object class. The comparison is the address. The Array class does not override the equals method, so the comparison result here is false;

7.int[] a = new int[10]

Where a is a reference, which points to the first address of the generated array object, each element in the array is of type int, which only stores the data value itself

Guess you like

Origin blog.csdn.net/xiogjie_67/article/details/108501088