The underlying type in Java is stored directly in the stack, the compound type takes a reference type, and the reference is stored in the stack, and the corresponding object is stored in the
The heap. So Java in the memory heap memory and stack memory, defined in the function of some basic types or references are allocated stack memory.
Heap memory is used to hold objects and arrays created by new, or static (class load information).
The memory allocated in the heap is managed by the JVM's GC.
A program can control only the lifetime of a reference, and the lifetime of the object is JVM-controlled.
In a Java application, a copy of the reference, passed by value, when the reference to the object is passed as a parameter of the method. and
Not the reference itself, the object reference and copy of the calling method point to the same object.
Objects are passed by reference, and there is only one parameter passing mechanism in the Java application--value delivery
The semantics of passing by value is that when a parameter is passed to a function, the function accepts a copy of the original value.
The semantics passed by reference is that when a parameter is passed 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);
}
}
Results of the run: Good afternoon
The assignment of an object is a reference to a passing object, SB1 and SB2 all refer to the same object, which is actually the value of the pointer. Here's
Assignment is an assignment between pointers.
1. A reference is a data type that preserves the address of an object in memory, which is not the simple data type we normally speak of and 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, a variable of that class 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 ());
}
}
From the above examples, we can conclude that
1, if the parameter is immutable object, such as primitive type (I.e.int) or Immutable object type (I.e.biginteger)
Such parameters are safe, and no action in the body of the method will affect what is outside the method
2, for the type of mutable object, such as ArrayList v
Calling Add () and remove () on V will result in changes to variables outside the method
This can cause code to be unsafe
However, if the parameter is assigned a value in the method body, such as V = new ArrayList ()
So the address that V points to has changed, and any subsequent operation on V will not affect the content outside of the method
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