The example of this article tells the object and object reference in Java. Share it for everyone for your reference. The specific analysis is as follows:
In Java, a group of nouns often appear together. They are "objects and object references". Many friends may often confuse these two concepts when they first learn Java. They feel that they are the same thing, but in fact, it is not. Today, let's take a look at the differences and connections between objects and object references.
1. What is the object?
There is a more popular word in Java, called "everything is object", which is one of the concepts of the beginning of the Java language design. To understand what object is, you need to understand it with a class. The following paragraph is quoted from a paragraph in "Java Programming Thought":
"According to popular claims, each object is an instance (Instance) of a certain class (Class).
From this sentence, it can be understood to the essence of the object. In short, it is an instance of the class, such as all people collectively referred to as "human". Specific to everyone, such as Zhang San, it is the object and the instance of "human".
2. What is object reference?
Let's read a paragraph first:
"Each programming language has its own data processing method. Sometimes, programmers must pay attention to what type of data to be processed. Do you directly manipulate elements, or use some indirect representation based on special grammar (such as C/C ++ in C/C ++ The pointer) to operate the operation. It actually refers to the "reference" of an object.
This passage comes from "Java Programming Thought". Obviously, it can be seen from this passage that the object and object reference are not the same thing, it is two completely different concepts. For example, we usually use the following line code to create an object:
Person Person = New Person ("Zhang San");
Some people will say that Person here is an object and an instance of the Person class.
Some people will say that the Person here is not the real object, but to the reference to the object created.
What kind of statement is right? We don't rush to tangle which saying is right, look at the two lines of code:
Person Person; Person = New Person ("Zhang San");
The function of the two lines of code is exactly the same as the above line code. Everyone knows that NEW in Java is used to create objects on piles. If Person is an object, why should the second line create objects through NEW? It can be seen that Person is not the object created. What is it? The above paragraph is very clear, "the manipulating identifier actually refers to a reference to an object", that is, Person is a reference, which refers to a reference to an object that can point to the Person class. The statement of the real creative object is New Person on the right ("Zhang San");
Look at another example:
Person Person; Person = New Person ("Zhang San"); Person = New Person ("Li Si");
Here, Person pointed to the object "Zhang San", and then to the object of "Li Si". In other words, Person Person, this sentence only declares a reference to a Person class, which can point to an instance of any Person class. This principle is the same as the following code:
int a; a = 2; a = 3;
Here are a variable A of an INT type first, which is assigned to A to 2, and the subsequent assignment is 3. That is to say, the value of the INT type can make it a value of 2, or 3, as long as it is legitimate. The value of INT type can be.
In other words, one reference can point to multiple objects, and can one object be referred to by multiple references? The answer is certainly possible.
for example:
Person Person1 = New Person ("Zhang San"); Person Person2 = Person1;
Person1 and Person2 both point to the object "Zhang San".
There are so many differences and connections about objects and object references. Interested friends can consult relevant documents and materials.
It is hoped that this article is helpful to everyone's Java program design.