The basic types in Java are directly stored in the stack. The composite type uses the reference type to store references in the stack, and the corresponding objects are stored in the stack. Therefore, in Java, stack memory is allocated for some basic types or references defined in the function. Heap memory is used to store objects and arrays created by new, or static (class loading information ).
Memory allocated in the heap is managed by the jvm gc.
The program can only control the reference lifetime, and the object lifetime is controlled by JVM.
In Java applications, when an object is referenced as a parameter of a method, a copy of the reference is passed (passed by value ). Instead of referencing itself, the object references and copies of the call method all point to the same object.
Objects are passed by reference. Java applications have only one parameter transfer mechanism-value transfer
When a parameter is passed to a function, the function accepts a copy of the original value.
When a parameter is passed to a function, the function accepts the memory address of the original value rather than 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 );
}
}
Running result: Good afternoon
The value assignment operation of an object is to pass the reference of an object. Both SB1 and sb2 refer to the same object. Here, the value is actually passed and the pointer value is passed. Here
A value is a value between pointers.
1. Reference is a data type that stores the address of the object in the memory. This type is not a simple data type that we usually call or a class instance (object );
2. Different references may point to the same object. In other words, an object can have multiple references, that is, variables of this type.
Public class test1 {
Public void fun (string s ){
S = "hehe ";
}
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 Al = new arraylist ();
Test. Fun (Al );
Iterator 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 ("Haha ");
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 ());
}
}
The above example shows that
1. If the parameter is an immutable object, such as primitive type (I .e.int) or immutable object type (I. e. biginteger)
Such parameters are safe, and any action in the method body will not affect content outside the method.
2. For variable object types, such as arraylist v
Calling add (), remove (), and other methods on V will lead to changes in variables outside the method.
This may cause Insecure code.
However, if a value is assigned to this parameter in the method body, for example, V = new arraylist ()
In this way, the address pointed to by V has changed, and any subsequent operations on V will not affect content outside the method.