java objects, object references

1 Introduction

In Java objects and object reference are confusing two concepts, is described in detail by the following sample code.

2. Meaning

For convenience of illustration, first define a Person class, it has two properties name and age.

 1 public class Person {
 2 
 3     private String name;
 4     private int age;
 5     
 6     public Person(String name, int age){
 7         this.name =name;
 8         this.age = age;
 9     }
10 }

Now create an object:

Person p1 = new Person("Wang", 12);

We usually call the above code creates an object, then how to distinguish between p1 correct, the relationship between them new Person ( "Wang", 12) and do? Further split below:

1 Person p1;
2 p1 = new Person("Wang", 12);

Separate the above two statements contribute to our understanding, we know that to create an object using the new keyword in Java, so that the new Person ( "Wang", 12) are creating an object, or

Who can say it is the object itself; so what is the first statement in the p1 say then, what we call in Java object reference variables, the full expression is "p1 is a pointer to the Person object

Reference (variable). "

3. Relationship

In understanding the object, object reference, we further illustrate the mapping between them.

1 Person p2;
2 p2 = p1;
3 p2 = new Person("Li", 11);

Specified as follows:

Line 1 creates an object reference to a Person class p2, it points to NULL.

Line 2 will p1 object reference points to the object (the object's address) to the copied p2, note that at this time the two object references p1 and p2 point to the same object "Wang", a modification of any

It will result in a corresponding change another parameter.

Line 3 p2 object reference point to a new object "Li", p1 and p2 at this time point to different objects.

Note: the above described relationship shows that an object can have multiple references, a reference can bind simultaneously 0 or an object.

4. The parameter passing

In understanding the object, the relationship between the object reference, look at the mass participation issues in Java. If the function is passed the basic type, it will be a copy of this basic type;

If the incoming are passing references, get a copy of this reference . Code examples are as follows:

 1 public class Test {
 2     private int i, j;
 3 
 4     private void test_m1(Test a) {
 5         Test b = new Test();
 6         b.i = 1;
 7         b.j = 2;
 8         a = b;
 9     }
10 
11     private void test_m2(Test a) {
12         a.i = 1;
13         a.j = 2;
14     }
15 
16     public static void main(String[] args) {
17         Test t = new Test();
18         t.i = 5;
19         t.j = 6;
20         System.out.println("t.i   =   " + t.i + "   t.j=   " + t.j); // 5,6
21 
22         t.test_m1(t);
23         System.out.println("t.i   =   " + t.i + "   t.j=   " + t.j); // 5,6
24 
25         t.test_m2(t);
26         System.out.println("t.i   =   " + t.i + "   t.j=   " + t.j); // 1,2
27     }
28 }

Execution of the function test_m1 (t) on line 22, now reference is a function of the copied referenced object address owned by t, i.e., t a and point to the same object (i = 5, j = 6). New in the function

Building an object (i = 1, j = 2 ), and b using a reference to it. Finally, make a copy of the references cited b, that is a reference point to a new object, but references t does not change , then it is still

Point to the original object.

Line 25 performs the function test_m2 (t), the function now reference a copy of the referenced object address owned by t, i.e., t a and point to the same object (i = 5, j = 6). At this point cited by

When modifying an object with a (i = 1, j = 2), since the object references also point t , the values of i and j at this time is changed.

5. References

https://www.cnblogs.com/dolphin0520/p/3592498.html

https://www.iteye.com/blog/zwmf-1738574

!!!

 

Guess you like

Origin www.cnblogs.com/jfl-xx/p/12369647.html