Java FAQ series (v) -- has the value passed or the reference passed?
Variable types in Java:
Basic type variables: including char, byte, short, Int, long, float, double, and Boolean.
Reference type variables include classes, interfaces, and arrays (basic type arrays and object arrays ).
When a variable of the basic type is passed as a parameter to a method, the Java Virtual Machine copies the value and then transmits the copied value to the method. After the method is executed, the lifecycle of the local variable ends.
When a referenced variable is passed as a parameter to a method, the Java Virtual Machine also copies the reference held by the variable and then passes it to the local variable created for the method by the Java Virtual Machine, the two variables point to the same object.
The Java programming language 2.6.5. parameter Values section: All parameters to methods are passed "by value. "In other words, values of parameter variables in a method are copies of the values the invoker specified as arguments ...... You shoshould note that when the parameter is an object reference, it is the object reference not the object itself that is passed "by value. "Thus, you can change which object a parameter refers to inside the method without affecting the reference that was passed. but if you change any fields of the object or invoke methods that change the object's state, the object is changed for every part of the program that holds a reference to it.
Supplement: string type
String is not the basic data type of Java and is defined as follows:
PublicFinalClass string extends object implements serializable, comparable <string>, charsequence
Their values cannot be changed after they are created.
Run the following code:
Public class test {
Public static void oprator (string test ){
Test. Replace ('A', 'E ');
Test. tolowercase ();
}
Public static void main (string [] ARGs ){
String STR = "Bea ";
Oprator (STR );
System. Out. println (STR );
}
}
Bea output