Java objects and object reference variables

Source: Internet
Author: User

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 do not know the object and object references, it is not very good to understand the following object-oriented technology. Write down a little bit of yourself, maybe let the beginner Java friends to take a little detour.  For illustrative purposes, 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 (); Usually the action of this statement is called the creation of an object, in fact, it contains four actions. 1) on the right"New Vehicle",is to create a vehicle class object in the heap space as a template in the vehicle class (also known asVehicle Object)。 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) on the left"Vehicle veh 1"A vehicle class was createdReference 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 that, we look at the second sentence, looking for the object just created what is the name? 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. Another sentence: Vehicle veh2, and then made a rope, not the SAIC ball. If add a sentence: Veh2 = veh1; 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 to the second object. From the above narrative, we can obtain the following conclusions: (1) An object referencecan point to a 0 or 1 object(a rope can be either a balloon or a balloon); (2) an objectcan have n references pointing to it(Can have n ropes to tie a balloon). If you come back to the following statement: VEH1 = VEH2; by inference above, 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 (); No. 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, also known as an anonymous object. Object-to-reference relationships persist until object recycling Java processes alias references at run time

Java objects and object reference variables

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.