An analysis of object and object reference examples in Java

Source: Internet
Author: User

The examples in this article describe the objects and object references in Java. Share to everyone for your reference. The specific analysis is as follows:

In Java, there is a group of nouns often appear together, they are "objects and object references", many friends in the beginner Java may often confuse these 2 concepts, think they are one thing, in fact, otherwise. Today we'll come together to understand the differences and relationships between objects and object references.

1. What is Object?

In Java, there is a popular word, called "Everything is Object", this is the Java language design at the beginning of one of the ideas. Understanding what is an object needs to be combined with the class. Here's a quote from the Java programming idea:

In layman's terms, each object is an instance of a class (instance), where ' class ' is synonymous with ' type '. ”

From this sentence we can understand the nature of the object, in short, it is an example of a class, for example, all people collectively referred to as "human", where "human" is a class (a type of species), and specific to everyone, such as John, it is the object, is the "human" example.

2. What is an object reference?

Let's look at a passage first:

"Each programming language has its own way of processing data." Sometimes, programmers must be aware of the type of data that will be processed. You manipulate the element directly, or you manipulate the object with some kind of indirect representation based on special syntax (such as a pointer in C + +). All of this is simplified in Java, and everything is considered an object. Therefore, we can adopt a uniform syntax. Although everything is "considered" as an object, the manipulated identifier is actually a "reference" to an object (reference). ”

This passage comes from the Java programming idea, and it is clear from this passage that objects and object references are not the same thing, and are two completely different concepts. For example, we typically use the following line of code to create an object:

?

1 person person = new Person ("John");

One would say that the person here is an object and is an instance of the People class.

Others would say that the person here is not a real object, but a reference to the object being created.

What kind of statement is right? Let's not be anxious about what is right, and then look at two lines of code:

?

1 2 Person of person; person = new Person ("John");

The two lines of code implement the same functionality as the one line above. As you all know, in Java, New is used to create objects on the heap, and if person is an object, why would the second line create objects with new? Thus, the person is not the object created, what is it? The above paragraph says clearly, " The manipulated identifier is actually a reference to an object, meaning that person is a reference to an object that can point to the person class. The statement that really creates the object is the new person on the right ("John");

Look at one more example:

?

1 2 3 Person of person; person = new Person ("John"); person = new Person ("Dick");

This lets the person point to the object "John" first and then to the "Dick" object. That is, the person person, which simply declares a reference to a person class, can point to an instance of any person class. This is the same as the following code:

?

1 2 3 int A; a=2; A=3;

This first declares a variable a of type int, first assigns a value of 2, and then assigns a value of 3. That is, the variable a of type int can give it a value of 2 or 3, as long as it is a valid value of type int.

In other words, a reference can point to multiple objects, and could an object be referred to by multiple references? Of course it is.

Like what:

?

1 2 Person Person1 = new Person ("John"); Person Person2 = Person1;

Person1 and Person2 all point to the object "John".

About the differences and connections between objects and object references for the time being so much, interested friends can consult the relevant documents and materials.

I hope this article will help you with your Java programming.

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.