The missing pictures in this article have been filled up, please rest assured to read.

In front of the article sometimes leave some thinking questions, mainly want to let everyone think more about the content of the article, so did not leave the answer, the comment section reply is also varied. Write this article is to help you to consolidate the previous content, son once said: “review the old and know the new, can be king”. There are four basic data types in Java, as shown in the following table:

So if you take out the four types and the eight basic types, everything else is an object, which is a reference type, including arrays.

Take a look at some sample code:

A Person class that provides a constructor and some get/set methods:

Here is the main method to test:

Let’s look at the first line of code:

The basic data types declared in the method body are in stack memory, so let’s draw them

Proceed with the following code

For basic data types, assignment (=) copies the value int1, 100, to int2 and continues drawing

Int1 =500, directly change the value of int1 to 500, as shown in the following figure

I’m going to print int1, int2, and I’m sure nobody’s going to get it wrong, 500,100.

Let’s do the initialization of the array

Arr1 is initialized first, and when the new keyword is executed, memory space is allocated in the heap and the address of the memory space is assigned to ARR1.

Proceed with the following code

When arR2 is initialized, it does not use the new keyword, so it does not create a new block of heap memory. Instead, it assigns the address of the heap memory stored in ARR1 directly to ARR2. For reference types, the assignment (= sign) is equivalent to copying the memory address. The situation is shown below

The following code is executed

We’re just modifying the arr1 array at subscript 3

But since array arr1 and array arR2 point to the same heap memory, print the values of arr1[3] and arR2 [3], both of which are 8. Did you get that right?

Let’s look at the initialization of the object

So when you see this new, this thing is definitely creating some space in the heap, so there’s a String called name in Person, and String is a little bit special, so it doesn’t have the new keyword, but it’s creating some space in the heap, String is a very common class. We will not go into details here. String is an array implementation, array is also a reference type, age is a basic data type, as shown in the following figure

The contents of the large box above are the entire Person object in heap memory. Continue with the following code

Without the new keyword, per2 does not create new space in the heap. Like arrays, per1’s memory address is assigned to Per2 directly

When we modify the properties of per1

As shown in the two red boxes below, assigning a value to an object (an array is also an object) is essentially a reference to a block of memory. The basic data type is a direct modification of the value, as shown in the figure below

If you print the name and age of per1 or per2, the result will be “li Si” or “35”. Finally, let’s verify that the results are consistent with the text.

The result is exactly the same. If you look back at the Java “==” comparator, the result is not hard to understand, as shown below

The result is false, true, and true, respectively. When == is a primitive data type, == compares two values to be equal. When == is a reference type, == compares two memory addresses, which can also be viewed as whether the two references refer to the same address in the heap

Beginners may often make mistakes in reference types when learning Java, such as arr1 and arR2 mentioned in this article. Many people want to copy a value to use when writing code, but do not know that when modifying ARR1, the value of ARR2 also changes.

This is the end of this article, drawing is not easy, I hope you think more about variables in memory, learning can get twice the result with half the effort. Int, int, int, int, int, int, int, int, int


Finally, a few final thoughts from the previous article, starting with the puzzling Java code article

It’s simply false

Both objects are new, creating two memory Spaces. References to i7 and i8 do not refer to the same address in the heap, so they print false.

As for arrays in Java at the end of the article

I believe that those who have read this article carefully know how much it is printed out. If it is not clear, I suggest re-reading this article until I understand it.

Note: The printing of a char array is a bit special. The printing of an int array prints an address, whereas the printing of a char array prints the contents of the array. All the code in this article has been uploaded to the public account: saysayJava, please pay attention to download.


If you liked this series, please give me a thumbs up or share it with me. Your support is what keeps me going, and leave a comment in the comments section where you can find out what you want to know about this column.

String is a very common class – Java things

Automatic boxing/unboxing in Java – Java stuff

Reprint unlimited welcome, but please indicate “author” and “original address”. Please keep this paragraph in the article, thank you for your respect for the author’s copyright. For commercial reprint or publication, please contact the author for authorization