In-depth understanding of Java Learning Notes reference

Source: Internet
Author: User

Introduction : The way data is passed in Java, in addition to thebasic data type is passed by value, all other types are passed by reference , which is very different from C + +, but many online articles are not clear, even wrong, after consulting the information, A relatively easy-to-understand version is compiled below.

We know that references have many names, such as string references, array references, and so on, depending on the type of reference.

One, stack memory and heap memory

We array to elicit and interpret these two concepts.

An array reference variable is simply a reference, and this reference can point to any valid memory.

The simple understanding is that this reference is used to hold the data address (the data address points to the data in memory location), when declaring the reference variable, just reserved a space to store the address, but has not really assigned to the reference variable an address, you assign it which data address, This reference points to this address (so it says "this reference can point to any valid memory"), so you can access the data from that reference.

such as string[] p = new String (5);

P is an array reference variable, which contains 5 elements. However, the actual array elements are stored in the heap, and the array reference variables are present in stack memory , such as:

That is, the storage of an array in memory is actually stored in two different kinds of memory: stack memory and heap memory. In fact, the same is true of other reference variables in Java.

Ii. references to classes

Consider one of the following self-established simple classes:

class test{   privateint  A;   Test ()   {       = 0;    }     Public void Set (int  b)   {       = b;    }          Public void Showinfo ()   {       System.out.println ("The value of A is:" + a);}   }  

If we have the following statement:

Test m = new test ()

We often see this saying: M is a reference variable to the test class, it feels so hard to understand, how to instantiate a class is a reference? Let's take a step-by-step look at this instantiation process.

Let's split the above statement into the following statement:

Test m;

m = new Test ();

We know that in Java, in addition to the built-in primitives, the other types are all references, and test is certainly not a built-in base type, so test m creates a reference to the test class:

Test m is a reference variable m that declares a test class, which tells the compiler to reserve a portion of the stack memory to M, and I will use M to store an address pointing to the storage unit of the test class object, and note that the test class object is stored in heap memory just like the array element above. But Test m is just a statement, but to which test class object, it is not known, because we have not assigned to it a test class object address, how can it know where to point?

m = new test () is to indicate the direction of M, new test () constructs a test class object, the system will allocate a certain amount of memory space for this object to store its own data, through the operation m = new test (), Assign the address of the newly created Test class to M in memory (heap memory) so that M knows where it should point:

The following points are summed up:

    • Java in addition to the built-in basic data types (int, double, float, and so on) are value-passing, other types are reference
    • When declaring a reference to a type, only a storage address space is reserved for the reference variable, which can point to any valid memory unit
    • Java uses a lot of references to reduce the cost of copying data during value transfer and to improve efficiency.

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.