Simple analysis of objects and object reference instances in Java

  • 2020-04-01 03:53:28
  • OfStack

This article illustrates objects and object references in Java. Share with you for your reference. Specific analysis is as follows:

In Java, there is a group of nouns that often appear together, they are "object and object reference", many friends in the beginning of Java may often confuse these two concepts, think they are the same thing, in fact they are not. Today we'll look at the differences and connections between objects and object references.

1. What is an object?

There is a popular phrase in Java called "everything is an object," which was one of the early ideas in the design of the Java language. To understand what an object is, you need to understand it in conjunction with a class. Here's a quote from Java programming ideas:

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

From this sentence, we can understand the essence of object. In short, it is an instance of class. For example, all people are collectively referred to as "human".

2. What is an object reference?

Let's start with a passage:

"Each programming language has its own way of handling data. Sometimes, programmers have to be careful about what type of data they're dealing with. Do you manipulate elements directly, or do you manipulate objects using an indirect representation based on a particular syntax (such as a pointer in C/C++)? All of this is simplified in Java, where everything is treated as an object. Therefore, we can adopt a unified syntax. Although everything is "treated" as an object, the manipulated identifier is actually a "reference" to an object.

This is from Java programming ideas, and it's clear from this that objects and object references are not the same thing, but two completely different concepts. For example, we usually create an object with the following line of code:


Person person = new Person(" Zhang SAN ");

One could say that the person here is an object, an instance of the person class.

Some would also say that the person here is not really an object, but a reference to the object being created.

Which of these statements is true? Before we get too hung up on which is true, let's look at two lines of code:


Person person;
person = new Person(" Zhang SAN ");

These two lines of code do exactly the same thing as the one above. As you all know, new is used to create objects on the heap in Java, so if person is an object, why would the second behavior create objects with new? So the person is not the object being created, what is it? The above paragraph makes it clear that "the manipulated identifier is actually a reference to an object," meaning that a person is a reference to an object that can point to the person class. The statement that actually creates the object is the new Person on the right (" zhang SAN ");

Here's another example:


Person person;
person = new Person(" Zhang SAN ");
person = new Person(" Li si ");

Here, the person points to the object "zhang SAN", and then to the object "li si". That is, Person Person, this sentence just declares a reference to the Person class, which can point to any instance of the Person class. This is the same as the following code:


int a;
a=2;
a=3;

Here, it is clear that a variable of type int, a, can be assigned a value of 2, followed by a value of 3. In other words, a variable of type int can be assigned a value of 2 or 3, as long as it is a valid int value.

That is, a reference can point to more than one object, and can an object be referred to by more than one reference? The answer, of course, is yes.

Such as:


Person person1 = new Person(" Zhang SAN ");
Person person2 = person1;

Person1 and person2 both point to the object "zhang SAN".

So much for the distinction and connection between objects and object references for now, interested friends can refer to the relevant documents and materials.

I hope this article has been helpful to your Java programming.


Related articles: