I:
1: If the parameter is of the basic data type (four types and eight types: byte, short, Int, long, double, float, double,
Boolean, char), then the value of the variable is passed, for example: int A = 10; then the passed value is 10!
Example:
Package qqwwffg. pass_method_paramater;
Public class primitivetype {
Public void change (int ){
A = 3;
}
Public static void main (string [] ARGs ){
Int A = 1;
Primitivetype test = new primitivetype ();
Test. Change ();
System. Out. println (a); // 1, instead of 3
}
}
When the change method is called, a space is allocated in the memory for storage. The stored value is 3. at the end of the method, the memory will wait for garbage collection. In the main method, the value of a printed by the method is the memory space allocated when the main method is called, there is no relationship between them, so the print is still 1.
2: If the parameter is a reference data type (class, array, interface), it is also the value of the variable, for example: Point Param = new point (); the value of the Point variable is passed in, instead of the point object! The value of the Point variable is actually the address (also called reference) of the point object in the memory )!
Example:
Public class referencetype {
Public void changepoint (point ){
Point. x = 5;
Point. Y = 6;
}
Public void changereference (point ){
Point = NULL;
}
Public void testpassbystirng (string PARAM ){
Param = "newvalue ";
}
Public static void main (string [] ARGs ){
Int A = 1;
Point point = new point (1, 2 );
Referencetype test = new referencetype ();
Test. changepoint (point );
System. Out. println (point. X); // 5 instead of 1
System. Out. println (point. Y); // 6 instead of 2
Test. changereference (point); // after this call, will there be a null pointer below?
Int x = point. X; // No
System. Out. println ("the value of X is" + x );
String Param = "value ";
Test. testpassbystirng (PARAM );
System. Out. println (PARAM); // value instead of newvalue
}
}
Class Point {
Int X;
Int y;
Public point (int x, int y ){
This. x = X;
This. Y = y;
}
}
In Java, the access object is operated by pointing to the object reference, and an object can be pointed to by multiple references, no matter which reference modifies the content of this object, it will affect another reference. Although there are multiple references, there is only one object!
When the changepoint method is called, you pass the reference point of the point object in the main method to the changepoint parameter.
Point. At this time, this point reference also points to the Point Object (new) in the main method, and in this method,
The Point Object (new) is modified with the reference of point (point. X = 5; point. y = 6;), because they point to the same point object (new), after the method is executed, this modification will reflect this
At this time, you can print the value of X and y of the point object, all of which are changed! That is, it is no longer 1 and 2, but 5 and 6.
Summary:
In Java, the passing of method parameters is always a pass-through value. For the basic data type, the value is the value you assign to the variable. For the reference data type, this value is an object reference, not the object itself.
2. in Java, through method calls, only the content of the object can be changed, rather than the reference of the object.
As shown in the above method public void changereference (point ){
Point = NULL;
}
Point = NULL. When you call the changereference method, the reference of the original point corresponds to
The point references the same object. When you call point = NULL, it no longer points to the original point object.
At this time, it only changes its own point, and does not modify the point reference in the main method.
Or point to the point object in Main! Therefore, if you call point. X in the main method, no NULL pointer exception will occur.
That is to say, in the method, you change the content of the object corresponding to the reference, and it will reflect to the object.
At this time, when you retrieve the object content, you have changed it! No matter how you reference the past
Change (point = NULL). The reference always points to its original object!
Here, instructor Zhang said in the video that when string is passed as a parameter, it is determined that the string object itself is a constant object! The testpassbystirng method above.
My own idea is: string type, which is a constant object, but it conforms to two:
When it is passed as a parameter, if the reference operation such as: paramstring = "change the original value ";
This is: "changing the original value" is actually an anonymous object of the string class.
Method, re-construct a String object, and assign the reference of this object to paramstring. At this time,
Changed the reference paramstring, but the passed reference is not changed!