1 we all know that there is no concept of pointers in Java and C #. But the difference between objects and references is often overlooked in programming, does Java really have pointers? What is a handle? Where is the variable address? No address is impossible, the key is how to avoid the concept of pointers in Java. 2 There are two ways in which memory is allocated in Java, one is allocated in the heap, one is allocated on the stack, all new objects are allocated in the heap, and the arguments in the function are allocated in the stack. In general, heap memory can be very large, such as 32-bit operating system virtual memory can be used by the heap (when the memory is tight and even the hard disk can be a heap of storage space), and the memory allocation of the stack is limited. 3 4This is the same as memory allocation in C + +. There are several basic types in Java, such as int,float,Double,Char, Byte, etc., they are not objects, except that everything is an object, and all objects are allocated on the heap. But it seems that in C # These are some of the methods that are encapsulated, and should be considered objects. 5 6What is an array of objects in Java, similar to C + +, is a handle array or an array of pointers, which holds the address of each element. Unlike in C + +, Java does not have operator overloading and copy constructors (it doesn't matter if you don't understand them), so when you create an object or assign a value to an object that has already been created (note that the object is not the base type): Object a=NewObject and Object a=B (b is a subtype of object or the same type), the object address is passed and copied. This is the delivery and assignment of the said handle. 7 8 The handle is stored in the object's address, the handle is a pointer, but you can not get the address, Java is through this clever to hide the pointer. When an object is passed as a parameter to a method, the address of the object is passed, and the row parameter holds a copy of the address of the argument (this is the most critical place, also the value pass, and the value pass is the copy of the value of the argument as the row parameter)9 such as:Ten Public classexample{ One A intI=0; - - } the - Public classa{ - - Public intI=0; + - PublicExample add0 (Example e) + A { at -e.i++; - - returne; - - } in - to + Public voidadd1 (Example e) - the { * $e.i++;Panax Notoginseng - } the Public voidmodify0 (Example e) + A { the +Example b=e;//assigns the address of the E-line parameter object to handle B - $b.i++;//also modifies the values of the E.I and arguments $ - } - the - Wuyi Public voidmodify1 (Example e) the - { Wu -E=NewExample (); About $e.i++; - - } - A Public Static voidMain (string[] args) + the { - $Example ex=NewExample (); the theA a=NewA (); the theA=a.add0 (ex);//equivalent to A.add0 (ex), no return value is required because the value of I in ex is modified directly by passing the object address (handle) - inA.ADD1 (ex);//ADD0,ADD1 directly modifies the value of ex.i in the method body, so the return value of add0 is somewhat superfluous the theA.modify0 (ex);//The effect on EX is the same as Add1 About theA.modify1 (ex);//does not have any effect on EX (and that is equivalent to nothing done). theThis may make some people confused. Why is it? Because it is a copy of the object address, "value passing", in Modify1 e=NewExample (); in fact, E is just a handle to the copy of the address of the ex object, when the E assignment is only the assignment of E in the stack (the variable e assignment to the copy of the Ex Pointer), and does not change the point of the ex's handle, and when the method call is complete the stack pops up, E is garbage collected. No use whatsoever. Of course you can use it as a return value, which is another matter. the + } - the }BayiIf you can understand this principle, you can avoid some potential logic errors, such as: The object is changed in the method, you may not know! Remember that C + + at this point and Java is very different, C + + default is the value of the pass, the line participants by the bit copy argument (if the pointer or reference is similar to Java), in the method as a parameter to pass the object, Java is more like a C + + Pass the reference, of course, there is a difference, it is The reference in the object cannot be re-assigned to another object.
Java pointers and references (reprint)