About Java Array Memory Allocation

Source: Internet
Author: User

Maybe everyone is familiar with Java arrays. Recently I encountered a problem about Java Array Memory allocation.

Haha. Suddenly I found that this sentence in many books is completely wrong. The following is a simple example code:

 

           String[] names = { "Michael", "Orson", "Andrew"        String[] animal =  String[4        names =

 

 

This may have been heard by everyone. The above code has a problem. The length of animal [] is 4, while that of names [] array is only 3, but after a value assignment statement, the size of both arrays is changed to 4. Does this change the array size? The problem is blocked! Well, ask the technical predecessors to have a new understanding of the array storage method. The following is my understanding: (if something is wrong, you will be able to point it out .)

The above names and animal do not represent this array object, but only the array variables. They are the same as pointers in C. Such variables are called reference variables. The array object is stored in the heap memory, and the size cannot be changed, but the array variable can point to other array objects. You can see the figure below:

 

The blue dotted line is the value assignment statement names = animal; the array objects in the heap memory pointed to by the previous names and animal array variables;

The red line is that the value assignment statement names = animal; after that, both names and animal array variables point to an array object at the same time. Of course, at this time, the Java garbage collection mechanism will find the unreferenced array object and then take it away.

We can also see that "Michael", "Orson", and "Andrew" are basic data types. But they are stored in the heap memory.

In fact, we should say:

Java objects in heap memory are generally not allowed to be accessed directly, but you can think of the consequences of direct access. To access the objects in the heap memory, you need to reference the variable mediation.

When is the variable stored in the stack memory just a reference variable? When has it changed its identity to a genuine JAVA object? Well, let's look at the example below:

      .name =.age = + " " +           Animal[] animal =  Animal[2=  Animal("cat", 1=  Animal("dog", 20] =1] =

Only when the reference variable in the stack memory calls the object method or points to the object's attributes, it truly becomes an object from the variable. (For example, in the example above, cat, dog object references the variable, animal [] array variable ).

Pass

Animal [0] = dog; animal [1] = cat;

So that both variables point to the objects stored in the heap memory, so they print the same information.


The blue line is a value assignment statement:
Animal [0] = dog; animal [1] = cat;
The previous variables point to the State. The red dotted line is the State after the value assignment statement. animal [0] And dog, animal [1] and cat all point to the same heap memory space.

(PS: I still want to thank the interviewers for their abuse of me from beginning to end over the past few months. Although there is no letter of approval for my internship, when I find that I still have a long way to go, the cause of this problem is also an interviewer's question. "Do you know how to store arrays in Java? ")

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.