Passing Java parameters by value? Pass by reference
Sometimes I want to know whether to pass by value or by reference when calling a method. Some people say that the basic data type is passed by value, and the reference type is passed by reference. At that time, it seems to make sense.
I will not investigate the ability to identify and read words here. I will explain my understanding of this issue. I don't want to say whether it is passing by value or by reference. I just want to understand it myself, after all, java is the king.
First, let's look at the following code:
- Package shb. java. testmemory;
-
- Public class TestMeo {
-
- /** Pass the basic data type and reference type parameters by value
- * @ Description:
- * @ Author shaobn
- * @ Param args
- * @ Date: 7:53:56 AM
- */
- Public static void main (String [] args ){
- // TODO Auto-generated method stub
- TestInt ();
- TestStr ();
- TestPack ();
- TestObj ();
- TestObj_2 ();
- }
- // NO1. Test Basic Data Type
- Public static void testInt (){
- Int num1 = 12;
- System. out. println ("Before change:" + num1 );
- ChangeInt (num1 );
- System. out. println ("After change:" + num1 );
- }
- // Test the string type
- Public static void testStr (){
- String str = "helloworld ";
- System. out. println ("Before change:" + str );
- ChangeStr (str );
- System. out. println ("After change:" + str );
- }
- // Test the packaging type
- Public static void testPack (){
- Integer integer = new Integer (42 );
- System. out. println ("Before change:" + integer );
- ChangePack (integer );
- System. out. println ("After change:" + integer );
- }
- // Test the reference type
- Public static void testObj (){
- Person person = new Person ();
- System. out. println ("Before change:" + person. age );
- ChangeObj (person );
- System. out. println ("After change:" + person. age );
- }
- // Method 2 of the test reference type
- Public static void testObj_2 (){
- Person person = new Person ();
- System. out. println ("Before change:" + person. age );
- ChangeObj_2 (person );
- System. out. println ("After change:" + person. age );
- }
- Public static void changeInt (int num ){
- Num = 21;
- }
- Public static void changeStr (String str ){
- Str = "hellobeijing ";
- }
- Public static void changePack (Integer integer ){
- Integer = new Integer (89 );
- }
- Public static void changeObj (Person person ){
- Person. age = 87;
- }
- Public static void changeObj_2 (Person person ){
- Person = new Person ();
- Person. age = 78;
- }
- }
- // Reference type test class
- Class Person {
- Public int age = 78;
- }
Look NO1:
Note: The two images I have drawn above are not exactly the same, so we can only do this. Let's analyze: when the data is of the basic data type, only a Copy of the real parameter is passed to the form parameter). Of course, due to the Features shared by the stack memory variables, these two variables point to the value of this variable.
When we change the form parameter, first, we will find whether new variable values exist in the stack memory. If yes, it points to the new variable value to reflect the characteristics of stack memory data sharing ). If no, open up a space in the stack memory to store new variable values. Meanwhile, the variable points to the new variable value.
At this time, we found that the variable value has nothing to do with the variable of the real parameter, two independent variables. Therefore, the variable value changed after the function is irrelevant to the previous one, so the previous variable value is output.
In addition, we can see that when passing an object reference, the person reference variable stores the memory address of the Person object in the heap memory, therefore, the memory address is transmitted as a string of numbers ). At this time, the two parameters have a common memory address value, so they point to the same memory object. We observe
It is found that when we change the attribute values of an object, we feel like a whole body. As long as you change this object, this object will be changed, but there is no other concept to open up an object except the String type and the packaging type ).
PS: I haven't finished writing it yet. I'm at work. Try again at night!
In case of any errors, please help us to correct them.