References and pointers in Java.
There are two ways to allocate memory in java. One is to allocate memory in the heap, the other is to allocate memory in the stack, and all new objects are allocated in the heap, the parameter transfer in the function is allocated in the stack. Generally, the heap memory can be large, for example, the virtual memory in the 32-bit operating system can be used by the heap (when the memory is insufficient, even the hard disk can be the storage space of the heap), and the memory allocation of the stack is limited.
This is similar to the memory allocation in c ++. Java has several basic types, such as int, float, double, char, and byte. They are not objects. In addition, all objects are allocated on the stack. But it seems that there are some encapsulated methods in C #, which should be regarded as objects.
What is an object array in java? similar to c ++, It is a handle array or a pointer array, which stores the address of each element. Unlike in c ++, java does not have operators to overload or copy constructor (it does not matter if you do not understand this ), therefore, when creating an object or assigning values to the created object (note that the object is not a basic type ): when Object a = new Object and Object a = B (B is the Object sub-type or of the same type), the Object address is transmitted and copied. This is the transfer and assignment of the handle.
In the handle, the object address is stored, and the handle is the pointer, but the address you cannot get. java uses this to cleverly hide the pointer. When an object is passed as a parameter to a method, the object address is passed, while the row parameter stores a copy of the real parameter address (this is the most critical part and also the value transfer, passing a value is to use a copy of the value of the real parameter as the row parameter)
For example:
Public class Example {
Int I = 0;
}
Public class {
Public int I = 0;
Public Example add0 (Example e)
{
E. I ++;
Return e;
}
Public void add1 (Example e)
{
E. I ++;
}
Public void modify0 (Example e)
{
Example B = e; // assign the address of the e-row parameter object to handle B
B. I ++; // also modifies the values of e. I and real parameters.
}
Public void modify1 (Example e)
{
E = new Example ();
E. I ++;
}
Public static void main (String [] args)
{
Example ex = new Example ();
A a = new ();
A =. add0 (ex); // equivalent to. add0 (ex), no return value, because the I value in ex is directly modified by passing the object address (handle)
A. add1 (ex); // add0 and add1 both directly modify the value of ex. I in the method body. Therefore, the return value of add0 is redundant.
A. modify0 (ex); // The effect on ex is the same as that on add1.
A. modify1 (ex); // No effect on ex (and this is equivalent to nothing ).
This may make some people confused. Why? Because it is a copy of the object address "value transfer", in modify1 e = new Example (); in fact, e is only a handle to save the copy of the ex object address, when assigning values to e, it only assigns values to e in the stack (assign values to the variable e in the copy of the ex pointer), but does not change the pointer of the ex handle. When the stack pops up after the method is called, e is about to be recycled, Which is useless. Of course you can use it as the return value. This is another thing.
}
}
If you can understand this principle, you can avoid some potential logical errors, such as modifying the object in the method. Remember that c ++ is very different from java in this regard, c ++ transfers values by default, and row parameters copy real parameters in BITs (if pointers or references are used, they are similar to java), and passing objects as parameters in methods, java is more like passing references in c ++. Of course there is a difference, that is, the reference of objects in c ++ cannot be assigned to another object.
This article is reprinted!