The basic type in Java is stored directly in the stack. The composite type uses the reference type, and the reference is also stored in the stack, and the corresponding object is stored in the heap. Therefore, in Java, the memory points in the memory and the stack memory, some basic types or references defined in the function allocate the stack memory. Pile of memory to store objects and arrays created by New, or static (class load information). The memory allocated in the heap is managed by the JVM GC. The program can only control the quotation period, and the living life of the object is the JVM control.
In the Java application, when the reference is a parameter that is passed, the reference is passed, (passed by value). Instead of reference itself, object references and copies of calling methods are to the same object. Objects are passed by reference, and only one parameter delivery mechanism --- value passes the semantics passing the value transfer is to pass a parameter to a function, the function accepts a copy of the original value. The semantics transmitted by reference is to pass a parameter to a function, the function accepts the memory address of the original value, not a copy. Public class test {public static void main (string args []) {stringbuffer sb1 = new stringbuffer ("good"); stringbuffer SB2 = SB1; sb2.append ("afternoon"); system.out.println ("sb1 == " SB1);}} Run: The assignment operation of the Good Afternoon object is a reference to the transmit object, both SB1 and SB2 refer to the same object, which is actually a transmission value, and it is the value of the pointer. The assignment here is the assignment between the pointers. 1. Quote is a data type that saves the address in memory in memory. This type is not what we usually say is not a class instance (object); 2. Different references may point to the same object, In other words, an object can have multiple references, that is, the variable of the type of type.
Public class test1 {public void fun (string s) {s = "hei";} public static void main (string args []) {test1 test = new test1 (); string str = "haha"; test.fun (STR ); System.out.println ("str ==" STR);}}
Public class test2 {public void fun (arraylist al) {al.add ("hehe"); al.add ("haha");} public static void main (string args []) {Test2 test = new test2 (); Arraylist (); test.fun (al); item it = al.Iterator (); while (it.hasnext ()) {system.out.println (" (string) it.next () }}}
Public class test3 {public void fun (arraylist al) {ArrayList Ala = new arraylist (); Ala.Add ("Ha"); Ala.Add ("HEHE"); al = Ala;} public static void main (String Args []) {Test3 test = new test3 (); arraylist al = new arraylist (); test.fun (al); system.out.println (" al.size ());}} You can get through the above example Out, if the parameter is an informable object, such as the original type (IEINT) or the non-variable object type (Iebiginteger) is secure, any action in the method does not affect the contents of the method 2, For types of variable objects, such as ArrayList V call add (), remove (), etc. can cause changes in variables, which may cause the code to be unsafe, but if the parameter is re-assigned to this parameter in the method Operation, such as v = new arraylist () This V-pointed address has changed, and then any operation of V will not affect the content outside of the method.