Let's start by defining a simple class:
Class Vehicle {
int passengers;
int fuelcap;
int mpg;
}
With this template, you can use it to create objects:---if you have a vague view of objects and class concepts: objects and classes
Vehicle veh1 = new Vehicle ();
Usually the action of this statement is called the creation of an object, in fact, it contains four of actions.
1. On the right, new Vehicle (); The Vehicle class is the template for creating a Vehicle class object in the heap space (referred to as the Vehicle object)---a vague view of the Java stack can be a stack and memory allocation in Java (It is recommended that you just look at the first part of the stack, and then look at the rest of the stack is better)
2. The end of the () means that after the object is created successfully, the constructor of the vehicle class is called to initialize the object that was just generated. If not, Java will fill in a default constructor.
3. The vehicle veh1 on the left is a reference variable in the stack space that creates a vehicle class, named VEH1, which is a reference to the vehicle object in the future.
4. The middle = Operation Foujian VEH1 reference to the vehicle object that was just created-the address of the vehicle object is stored in VEH1 (equivalent to the house number).
This statement can be split into two parts to write: Vehicle veh1; VEH1 = new Vehicle (); It is quite clear that one is the object reference variable and the other assigns the object address to the reference.
A Vehicle class can create countless objects on this basis, and these objects cannot be called all "Vehicle".
The object has no name and cannot be accessed directly. We can only access objects indirectly through object references.
In order to visualize the object, the reference and the relationship between them, you can make a metaphor that may not be appropriate. The object is like a big balloon, so big that we can't hold it. A reference variable is a rope that can be used to tie a balloon.
If only the first statement is executed (Vehicle veh1; ), the second bar (veh1 = new Vehicle ()) is not executed, and the reference variable VEH1 created does not yet point to any object whose value is null. A reference variable can point to an object, or null.
It is a rope, a rope that has not been tied to any of the balloons. After executing the second sentence, a new balloon was made and tied to the rope on the VEH1. We grabbed the ball by grasping the rope.
One more sentence:
Vehicle Veh2;
He made a rope again, not the SAIC ball. If you add one more sentence:
VEH2 = VEH1;
It's tied up. Here, the replication behavior occurs. However, to illustrate, the object itself is not copied, only the object address is copied. As a result, VEH2 also points to the object that VEH1 points to. Two ropes are the same balloon.
If you create an object with the following sentence:
VEH2 = new Vehicle ();
The reference variable, VEH2, changes to a second object. At this point the VEH1 does not change, of course, the first object is also pointed.
From the above narrative, we can obtain the following conclusions:
(1) An object reference can point to 0 or 1 objects (a rope can not be a balloon, or a balloon can be tied);
(2) An object can have n references pointing to it (there can be n ropes to tie a balloon). --Note: An object can be pointed without reference (the balloon can be white without a rope), generally used to output, for example: System.out.println (New Vehicle ());
If you come back to the following statement:
VEH1 = VEH2;
According to the above inference, VEH1 also points to the second object. That's fine. The question is, what about the first object? There was not a rope to tie it, it flew. Most books say it was recycled by Java's garbage collection mechanism.
It's not exactly. Correctly, it has become the object of garbage collection mechanism processing. As for when to really be recycled, it depends on the garbage collection mechanism mood.
In this view, the following statement should be illegal, right? At least it's useless, right?
New Vehicle ();
Wrong. It is legal and available. For example, if we generate an object just for printing, we don't need to tie it with reference variables. The most common is the print string: System.out.println ("I am java!");
String Object "I am java!" is discarded after printing. Some people call this object a temporary object.
The relationship between the object and the reference continues until most of the object collection is excerpted from the JAVA object reference, and the object is assigned a value
See, this is supposed to be clear, right? Check the code below to verify
public class testc { private int id; private string name; Public int getid () { return id; } public void setid (int id) { this.id = id; } public string getname () { return name; } public void setname (String name) { this.name = name; } @Override public string tostring () { return "testc [id=" + id + ", name=" + name + "]";  } PUBLIC TESTC () {}; PUBLIC TESTC (int id,string name) { this.id= Id; this.name=name; }; public static void main (String[] args) {   TESTC T1=NEW TESTC (1, "Zhang San");//Create a new object, assign the object address to the reference testc t2=t1;// Assigns the first reference (object address) to a second reference system.out.priNtln ("T1:" +t1.tostring () + "--T2:" +t2.tostring ()); t2.setid (2);//Change the value of the second reference t2.setname ("John Doe" ); system.out.println ("T1:" +t1.tostring () + "--T2:" +t2.tostring ()); }}
We will see the results:
T1:testclone [Id=1, Name= Zhang San]--t2:testclone [Id=1, Name= Zhang San]
T1:testclone [id=2, name= John Doe]--t2:testclone [id=2, name= John Doe]
We'll see that the two reference results are the same: because the second reference corresponds to the same object as the first reference. Because the objects are all the same, no matter how he changes, the results of the two references are the same. (using the balloon theory above is the same balloon, whether the balloon is flat or round, it is this one)
At this point, it may be thought that if the reference is not the same, it will change? After appending the code, let's look at:
System.out.println ("======= began to be different ======="); T2=new TESTC (3, "Harry"); System.out.println ("T1:" +t1.tostring () + "--T2:" +t2.tostring ());
T1:TESTC [Id=1, Name= Zhang San]--t2:testc [Id=1, Name= Zhang San]
T1:TESTC [id=2, name= John Doe]--T2:TESTC [id=2, name= John Doe]
======= 's starting to be different =======
T1:TESTC [id=2, name= John Doe]--T2:TESTC [id=3, Name= Harry]
These two references are different. Because the second reference points to the second new object (the second one stores the address of the second object), the first reference does not change (the balloon example above: The second line is now tied to the second balloon, and the first one is on the first balloon.) So now each rope has a balloon, and there is no contact with each other.
Well, the reference to the object is probably the case, so it's recommended to look at the above-mentioned stack and memory allocations in Java to understand deeper
But if you want to just copy the value of the object, not the address of the object, you can implement the Clone interface, or serialization, see the next article--object replication
Java objects and references