Go: Java objects and object reference variables

Source: Internet
Author: User

Java objects and their references

Some basic concepts between objects and references.

Beginner Java, for a long time, always feel that the basic concept is very vague. Later, in many Java books, the references to objects and objects are confused. However, if I can't tell the object from the object reference,

That's really not a good way to understand the following object-oriented techniques. Write down a little bit of yourself, maybe let the beginner Java friends to take a little detour.

For illustrative purposes, we will first define a simple class:

Class Vehicle {

int passengers;

int fuelcap;

int mpg;

}

With this template, you can use it to create objects:

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) The "New Vehicle" on the right is a template for the Vehicle class, creating a Vehicle class object (also referred to as Vehicle object) in the heap space.

2) The end of the () means that immediately after the object is created, the constructor of the vehicle class is called, initializing the object that was just generated. Constructors are definitely there. If you do not write, Java will give you a default constructor.

3) "Vehicle Veh 1" On the left creates a Vehicle class reference variable. The so-called vehicle class reference is the object reference that can then be used to point to the vehicle object.

4) The "=" operator causes the object reference to point to the vehicle object that was just created.

We can split this statement into two parts:

Vehicle VEH1;

VEH1 = new Vehicle ();

The effect is the same. In this case, it is more clear that there are two entities: one is the object reference variable, and the other is the object itself.

The entities created in the heap space are different from the entities created in the data segment and in the stack space. Even though they are entities that do exist, we cannot see or touch them. Not only this,

Let's take a closer look at the second sentence and find out what the name of the object you just created. Some say it is called "Vehicle". No, "Vehicle" is the name of the class (the object's creation template).

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 and no second is executed, 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 reference 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.

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).

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 to print a 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 will persist until the object is recycled

Java handles alias references at run time

Go: Java objects and object reference variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.