Objects and references in Java

Source: Internet
Author: User

Objects and references in Java

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.

If you cannot tell between objects and object references, you cannot fully understand the following object-oriented technology and write down your own understanding. This may reduce the possibility of detours for beginners of Java.

To facilitate the description, we first define a simple class:

Class student {

Int name;

Int age;

Int sex;

}

With this class (Template), you can use it to create an object: student stu1 = new student ();

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

1) the "new student" on the right creates a student Class Object (also referred to as the student object) in the heap space as the template ).

2) The () at the end means that after the object is created, the student 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) The student stu1 on the left creates a student class to reference the variable. The so-called student class reference can be used to point to

The object reference of the student object, which points to the memory address of a student object (a pointer in C ).

4) The "=" operator points the object reference to the newly created student object.

We can split this statement into two parts: student stu1; stu1 = new student (); the effect is the same.

In this case, it is clear that there are two entities: one is the object reference variable (stu1). In Sun's implementation, the object reference is a handle, which contains a pair of pointers: A pointer points to the method table of the object, one pointing to the data of the object, and the other is the object itself (the new object ).

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. Furthermore, let's take a closer look at the second sentence and think about the name of the newly created student object?

It is said to be "student ". No. "student" is the name of the class (Object creation template. A student class can create countless Objects Based on this. These objects cannot all be called "student ". 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, object references, and their relationships, we can make a metaphor that may be inappropriate:

An object is like a kite without a line. A referenced variable is a line that can be used to fasten a kite. If only the first statement is executed and the second statement is not executed, the reference variable stu1 created at this time does not point to any object, and its value is null. The reference variable can point to an object, or null. At this time, stu1 is a line, and a line is not tied to any kite.

After executing the second sentence, a new kite was made and tied to the stu1 line. We caught this line and caught the kite.

Another sentence: student stu2; there was another line, and no kite was tied. If you add: stu2 = stu1; fasten the kite.

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 stu2 points to the object stu1 points to, that is, the two wires are the same kite.

If you use the following sentence to create another object: stu2 = new student (); then reference the variable stu2 to point 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 line can be a kite or not), and can be changed;

(2) An object can have N references pointing to it (there can be N lines tied to the same kite ).

If you have the following statement: stu1 = stu2;

According to the above inference, stu1 also points to the second object. This is okay. What is the first object? No line is tied, and it is flying.

Many books say that it has been recycled by Java's garbage collection mechanism. This is not accurate. To be precise, it has become the processing object of Java's garbage collection mechanism.

When the garbage collection mechanism is actually being recycled depends on the mood of the garbage collection mechanism. From this point of view, the new student (); statement should be invalid,

At least it's useless, right? 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 print character is System. out. println ("I am Java! "); String object" I am Java! "It is discarded after printing. Someone calls this object a temporary object.

 

 

Constructor in java

Principles:

1. the constructor cannot declare the return type and cannot be modified by static, final, synchronized, abstract, or native.

2. when a class does not have a custom constructor, the compiler will automatically generate a default constructor for you. This is a constructor without parameters and without any code; however, once you define a constructor for your class, the compiler will no longer generate a default constructor for you, regardless of whether the parameter is included.

3. the constructor of the parent class cannot inherit from the quilt class. A subclass cannot directly call a constructor of the parent class through the method name, but calls a constructor of the parent class through the super keyword. The super statement must be located in the first row of the subclass constructor.

4. when you try to call a constructor of a Class (temporarily named method a) to generate an instance of this class, you first need to call the constructor of its parent class, there are two situations: (1) method a uses the super keyword to call a constructor of the parent class (whether it includes a parameter or not) (note that the super sentence must appear in the first line of method a at this time); (2) method a does not explicitly use the super keyword to call a constructor of the parent class, at this time, the compiler adds the super (); Statement to the first line of method a when compiling the subclass.

5. when a class has several constructor methods, if you want to call another constructor (B) In one Constructor (), you need to use the this keyword instead of using the method name to call it directly, and this sentence must be placed in the first line; in this case, if the constructor B called using the this keyword explicitly calls a constructor of the parent class, the compiler will not add super () to method (); to call the default constructor of the parent class.

 


Let's also talk about Java value transfer-What is passed?

In java, There is only value transfer. This is the truth that remains unchanged. However, due to the special nature of the object type, some people may still wonder about value transfer? Or is it passed by reference?

It may be caused mainly by this sentence: in Java, the passing of method parameters, the object is the passing of references, and the basic data type is the passing of values.

If only values are passed in java, why do we need to say that the object is passed by reference?

If the basic data type is value transfer, no one will be confused. So what does the object pass?

In fact, this is due to a obfuscation of JAVA in interpreting its variable types: JAVA contains only eight basic data types and Object Types

Therefore, the basic data type is to pass the value, and the object is to pass the reference.

In fact, if the object type is also interpreted as a basic data type, there will be no doubt that the object is passed for reference.

The following describes the object type as a new basic type, which is called olong (object long). That is, all object variables are olong. Although the object type can be written in java, for example, Long a; but the transfer is actually olong;

Let's get to know olong again:

Assignment:


Long a = 1234;

Olong oa = new Long (1234); // Long oa = new Long (1234 );


Transfer:

Long B =;

Olong ob = oa; // Long ob = oa;

 

Change:

B = 4321;

Ob = new Long (4321); // ob = new Long (4321 );


What has changed? :

System. out. println (a); // output 1234

System. out. println (B); // output 4321

System. out. println (oa. longValue (); // output 1234

System. out. println (ob. longValue (); // output 4321


What is passed in:

Long:

Long a = 1234; // The value of a is 1234.

Long B = a; // The value of B is 1234; the value of a is 1234.

B = 4321; // The value of B is changed to 4321. However, a is not affected and is still 1234.


Olong:

Long variables are stored and transmitted as the basic type values. What about olong?

Olong variables can be considered to be stored and transmitted as a memory address pointing to a specific object,

Olong oa = new Long (1234); // assume that the address of the object generated by new Long is 0001, the oa value is 0001.

Olong ob = oa; // value transfer; ob value: 0001; oa value: 0001

Ob = new Long (4321); // The value of ob is assigned to the address of a new object, which is assumed to be 0002, but the oa value is not affected and is still 0001

Therefore, the value assignment result of the olong type is consistent with that of the long type, and it is also a value transfer.


Why is the object a reference?

In fact, this sentence is also good, because the olong variable itself is a reference, referencing an object (memory address)

But it is actually a value transfer! Just because the value of olong has another alias called reference, it is called a confusing new word: Reference transfer!


Summary:

The introduction of olong is just to facilitate the interpretation of the value transfer of Java objects. It means that the transfer of object variables is essentially different from that of basic variables during parameter transfer. Therefore, in Java, there is no reference transfer, and only value transfer!


 

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.