Go to: variables referenced by Java objects and objects, and variables referenced by java objects

Source: Internet
Author: User

Go to: variables referenced by Java objects and objects, and variables referenced by java objects
Java object and its reference

Some basic concepts about objects and references.

When I was a beginner in Java, I thought the basic concepts were vague for a long time. Later, I learned that in many Java books, objects and object references are confused. However, if I cannot tell between objects and object references,

It is really hard to understand the following object-oriented technology. Writing down your own understanding may reduce Java beginners from detours.

To facilitate the description, we 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 ();

Generally, the action of this statement is called to create an object. In fact, it contains four actions.

1) the "new Vehicle" on the right is a Vehicle class template that creates a Vehicle class Object (also referred to as a Vehicle object) in the heap space ).

2) The () at the end means that after the object is created, the Vehicle class constructor is called immediately to initialize the newly generated object. Constructors are certainly there. If you do not write it, Java will add you a default constructor.

3) "Vehicle veh 1" on the left creates a Vehicle class reference variable. The so-called Vehicle class reference is an object reference that can be used to point to the Vehicle object in the future.

4) The "=" operator directs the object reference to the created Vehicle object.

We can split this statement into two parts:

Vehicle veh1;

Veh1 = new Vehicle ();

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

The object created in the heap space is different from the object created in the Data Segment and stack space. Even though they are actually entities, they are invisible and invisible. Not only that,

Let's take a closer look at the second sentence and find out the name of the newly created object? It is said to be "Vehicle ". No. "Vehicle" is the name of the class (Object creation template.

A Vehicle class can create countless objects accordingly. These objects cannot all be called "Vehicle ".

The object does not even have a name and cannot be accessed directly. We can only indirectly access objects through object reference.

To visually describe objects, references, and relationships between them, we can make a metaphor that may be inappropriate. The object is like a very large balloon, so big that we can't catch it. The referenced variable is a rope that can be used to fasten the balloon.

If only the first statement is executed and the second statement is not executed, the referenced variable veh1 has not pointed to any object yet, and its value is null. The referenced variable can point to an object or be null.

It is a rope that has not been tied to any balloon. After the second sentence is executed, a new balloon is made and tied to the veh1 rope. We caught the rope, and we caught the balloon.

Another sentence:

Vehicle veh2;

I made another rope and didn't fasten the balloon. If you add:

Veh2 = veh1;

Fasten it. Here, a replication occurs. However, it should be noted that the object itself is not copied, And the Copied object is only referenced. The result is that veh2 also points to the object pointed to by veh1. The two ropes are tied to the same balloon.

If you use the following sentence to create another object:

Veh2 = new Vehicle ();

The reference variable veh2 points to the second object.

From the above descriptions, we can draw the following conclusions:

(1) An object reference can point to 0 or 1 objects (a rope can be without a balloon or a balloon );

(2) An object can have N references pointing to it (N Ropes can hold a balloon ).

If the following statement is used:

Veh1 = veh2;

According to the above inference, veh1 also points to the second object. This is okay. What is the first object? No rope tied it, and it flew. In most books, it is recycled by Java's garbage collection mechanism.

This is not exact. Correctly, it has become the processing object of the garbage collection mechanism. When the garbage collection mechanism is actually being recycled depends on the mood of the garbage collection mechanism.

In this case, should the following statements be invalid? At least it's useless, right?

New Vehicle ();

No. It is legal and available. For example, if we only generate an object for printing, we do not need to use reference variables to hold it. The most common one is to print strings:

System. out. println ("I am Java !");

String object "I am Java !" It is discarded after printing. Someone calls this object a temporary object.

The relationship between the object and the reference will continue until the object is recycled.

The alias reference is processed only when Java is running.

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.