Talking about object and object reference in Java
In Java, a set of nouns often appear together, they are "objects and object references", many friends in the beginning of Java may often confuse the 2 concepts, think they are one thing, in fact, otherwise. We'll come together today. Learn about the differences and linkages between object and object references.
1. What is an object?
In Java, there is a more popular word, called "everything Objects", which is one of the concepts of Java language design at the beginning. To understand what an object is, you need to understand it together with the class. The following is a quote from the idea of Java programming:
"According to popular parlance, each object is an instance of a Class (class) (instance), where ' class ' is synonymous with ' type '. ”
From this sentence can understand the nature of the object, in short, it is an example of the class, such as all people collectively known as "Human", where the "human" is a class (a type of species), and specific to everyone, such as Zhang San this person, it is the object, is the "human" example.
2. What is an object reference?
Let's read a paragraph first:
"Each programming language has its own way of handling data. Sometimes, programmers have to be aware of what type of data they are going to work with. Do you manipulate an element directly, or use an indirect representation based on a particular syntax, such as a pointer in C + +, to manipulate an object. All of this has been simplified in Java, and everything is treated as 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 idea of Java programming, 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 create an object with the following line of code:
person person = new Person ("Zhang San");
Someone would say that the person here is an object and is an instance of the person class.
Others will say that the person here is not a real object, but rather a reference to the object being created.
What kind of statement is right? We don't have to worry about what the argument is right, and then look at two lines of code:
Person Person;person = new Person ("Zhang San");
The two lines of code implement exactly the same functionality as the one on the line above. As you all know, in Java new is used to create objects on the heap, if the person is an object, then why the second row to create objects through new? This shows that person is not the object created, what is it? It is clear from the above that "the manipulated identifier is actually a reference to an object", meaning that person is a reference and 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 ("Zhang San");
Let's look at an example:
Person Person;person = new Person ("Zhang San");p Erson = new Person ("John Doe");
Here, the person points to the object "Zhang San" and then to the object "John Doe". That is, the person person, this sentence simply declares a reference to a person class, which can point to an instance of any person class. This is the same as the following code:
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, a variable of type int can have 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? The answer is of course possible.
Like what:
Person Person1 = new Person ("Zhang San"); Person Person2 = Person1;
Both Person1 and Person2 point to the object "Zhang San".
About the differences and connections between object and object references so much for the time being that interested friends can consult the relevant documents and materials.
Talking about object and object reference in Java