In Java, only "value delivery" is a parameter transmission method.
1. Press value delivery and paying by reference delivery definition to pass the two terms by reference to the reference. Press value conversion means that when a parameter is passed to a function, the function receives a copy of the original value. Therefore, if the function modifies the parameter, only the copy is changed, while the original value remains unchanged. According to reference, it means that when a parameter is passed to a function, the function is received is the memory address of the original value, not a copy of the value. Therefore, if the function modifies this parameter, the original value in the call code also changes. 2.
Object (Object) and reference (Reference)
We know: a a = new a (); generating a type A type object, A is a reference to this object, that is, a point to the true object in HEAP, and A and other basic data types are stored together in Stack. It is Object to manipulate by Reference, in the underlying, A is more like a pointer. 3. A help understanding:
Public Class Passtest
{
Float ptvalue;
Public Void Changeint (int value)
{
Value = 55;
}
Public Void Changestr (String Value)
{
Value = New String ("DIFFERENT");
}
Public Void ChangeObjValue (Passtest Ref)
{
Ref.ptValue = 99f;
}
Public static void main (string args [])
{
String Str;
Intval
PASSTEST PT = New Passtest ();
VAL = 11;
Pt.changeint (VAL);
System.out.println ("int value is: val);
Str = new string ("Hello");
Pt.changestr (STR);
System.out.println ("Str value is:" STR);
Pt.PtValue = 101F;
Pt.changeObjValue (PT);
System.out.println ("CURRENT PTVALUE IS: PT.PTVALUE);
}
}
The result of the last output of this program will be:
11
Hello
99F
For the INT type parameter 11, although the value of the parameter is changed, the arctic is not changed, which is the same as the C language. For the String type variable STR, because string in Java is handled as an object, refer to the above analysis, Str is a reference (pointer) of "hello", the saying of values is a side piece of the STR, so it is also one Quote. But he passed Value = New String ("Different"); if Value, VALUE, points to a new object. So the content referred to by the STR is not changed.
PT is an object of the passtest created in the main function. By passing it, you can complete the delivery of the address, in the child function, the REF and the main function PT point to the actual memory unit, then modify the value of the PTValue in the Ref, also It is the value of the PTValue in the PT, even if there is no last data returns, this data will also change.