Today I read a great blog post that reviewed the knowledge of parameter passing in Java (that is, the difference between value passing and reference passing). Refer to website http://www.cnblogs.com/binyue/p/3862276.html. Below I will record their understanding through the blog, but also hope to help learn Java or review Java friends.
One, basic types and reference types in memory of the contents of the Save
Variables in Java are divided into basic types and reference types. A variable of the underlying type holds the value itself, whereas a variable of the reference type holds the reference value, which is the address to the memory space.
Basic types include: Byte,char,int Short,long,float,double,boolean;
Reference types include: Class types, interface types, and arrays.
Ii. differences between basic and reference types
* A base type allocates space for a variable when it declares it:
intvalue;
value = 10;//正确,因为声明a时就分配了空间
* Whereas references differ, when a referenced declaration is made, only the reference space is declared for the variable, and no data space is allocated:
Date date;
Instantiate, open up the data space to hold the date object, and then pass the first address of the space to the Today variable
Date=new Date ();
If you comment out the previous action
The local variable date may not be been initialized
This means that the object's data space is not assigned
Date.getdate ();
* The initialization process is as follows:
Date a,b; //在内存开辟两个引用空间
a = newDate();//开辟存储Date对象的数据空间,并把该空间的首地址赋给a
b = a;
//将a存储空间中的地址写到b的存储空间中# #: Note: References are also taking up space, and an empty object's reference size is about 4byte.
iii. Reference passing and value passing
* Value passing: When a method is called, the actual parameter passes its value to the corresponding formal parameter, and the function receives a copy of the original value, at which time there are two equal basic types in memory, and if the method performs a processing operation on the parameter, it does not affect the value of the actual argument.
* Reference passing: When a method is called, the reference to the actual parameter (that is, the address, not the value of the parameter) is passed to the corresponding formal parameter in the method, the function receives the memory address of the original value, and in the method, the shape participates in the same argument, and the processing of the parameter affects the value of the argument. The specific code is as follows:
public
class
ReferencePkValue2 {
public
static
void
main(String[] args) {
ReferencePkValue2 t =
new
ReferencePkValue2();
int
a=
99
;
t.test1(a);
//这里传递的参数a就是按值传递
System.out.println(a);
MyObj obj=
new
MyObj();
t.test2(obj);
//这里传递的参数obj就是引用传递
System.out.println(obj.b);
}
public void test1(int a){
a=a++;
System.out.println(a);
}
public
void
test2(MyObj obj){
obj.b=
100
;
System.out.println(obj.b);
}
}The result of the output is: - - - -
* Here is a special consideration of string, as well as the integer, double, and several basic types of wrapper classes, which are immutable types,
because there is no function to provide its own modification, each operation is a new object, so special treatment, it can be considered to be similar to the basic data type, transfer value operation.
Iv. Conclusion
1) When a formal parameter is a basic type, the processing of the parameter does not affect the argument.
2) When a parameter is a reference type, processing of the parameter affects the argument.
3) Special handling of immutable types such as string,integer,double can be understood as value passing, and parameter manipulation does not affect the argument object.
Understanding of Java in-form participation in arguments