I have reviewed the Eight Native Data Types of Java. In fact, we will use an important reference type during development.
The reference type is used on objects. An object can be pointed to by multiple references. At the same time, each reference can only point to a unique object. For example,
In the stack, there are four references: people1, people2, People3, and people4 pointing to the same object people object. If an object is pointed to by multiple references, any modification to the object's attributes will be reflected in other references. Example:
Public class people {// the object's attribute int age = 20; // you can change the object's attribute public void change (People people) {people. age = 30;} public static void main (string [] ARGs) {// a reference of the people type, pointing to a people object people = new people (); people. change (people); system. out. println (people. age); // 30 // people2 also points to the people object people people2 = people; system. out. println (lele2.age); // 30 }}
In the above example, the stack analysis is as follows:
(The red part indicates that the object is cleared.) People points to the people object. First, change the age to 30. Now, the age of the people object is 30, so the age of people is 30. le2 points to the people object, therefore, the age value of eagle2 is also 30. Let's look at the following code:
Public class people {// object attribute int age = 20; // change the object's attribute public void change (People people2) {// people points to the new object people2 = new people (); people2.age = 30; system. out. println (lele2.age); // 30} public static void main (string [] ARGs) {// a reference of the people type, pointing to a people object people = new people (); people. change (people); system. out. println (people. age); // 20 // People3 also points to the people object people People3 = people; system. out. println (lele3.age); // 20 }}
In the above example, the stack analysis is as follows:
(The red part indicates that the object is cleared) in the change () method, people2 points to the new object, so it changes the age attribute value of the new object to 30, however, people points to another people object. In this program, no reference is provided to modify the value of this object's age attribute. Therefore, the age attribute value of people is still 20. As shown in the code, eagle3 points to the object pointed to by people, so the age attribute value of eagle3 is also 20.
What is the result of the following code?
Public class datatest {public static void change (int A) {A = 4; system. Out. println (A); // 4} public static void main (string [] ARGs) {// create a variable A in the stack, and open up a literal value of 1, A points to it int A = 1; datatest. change (a); system. out. println (a); // 1 }}
As shown above, when the change () method is executed, the literal value 1 is actually passed to a in the method. The change () method is the modified literal value () method A does not have any relationship. After the change () method is executed, the entire stack (method call) process ends. Continue to execute the following program. The value of a of main () is still 1.
. This process is executed in the stack. Unlike the reference, it references an address information and references objects in the stack.