Java object references are passed in the method

Source: Internet
Author: User

Java object references are passed in the method

Package com.cy;

Public class Client {

Public Static void Main (string[] args) {

Student std = new Student ("Cy", 24);

Changestudentnameandage (STD);

System. out. println (Std.tostring ());

}

Change student's name and age

1. Direct manipulation of objects

Private Static void changestudentnameandage (Student std) {

Std.setname ("Chenyin");

Std.setage (84);

}

2. Re-new an object

Private Static void changestudentnameandage (Student std) {

std = new Student ("Chenyin", 84);

}

}

Situation 1. The statement executing code 1 output is Student [age=84, Name=chenyin]

Situation 2. The statement that executes the Code 2 output is student [age=24, Name=cy] Why are there two different points of results?

Because the Java programming language only values pass parameters. When an object instance is passed as a parameter to a method, the value of the parameter is a copy of the object's reference. Point to the same object, the contents of the object can be changed in the called method, but the object's reference (not the referenced copy) is never changed.

In the above method Changestudentnameandage (Student std), STD is just a copy of the STD reference in the main function. That

The local variable STD points to the address that the main method variable std points to. Because the parameter in the method is a formal parameter, he simply saves the address of STD point in the main method to the memory area. The STD in STD and mian in Changestudentnameandage (Student std) is not understood to be the same. Because you can write Changestudentnameandage (Student std1) changestudentnameandage (Student std2).

When the situation is 1 o'clock swim changestudentnameandage (Student std) STD points to the address and the main method in Std points to the same address, then to Changestudentnameandage (Student std) Medium STD operation, it also changed the content of Std point in Mian.

and Case 2 is different. Changestudentnameandage (Student STD) STD uses the new method, then Std points back to a storage area. Then the STD operation in Changestudentnameandage (Student std) actually changes the value of the new STD creation area, and the content of the Mian method Std is not affected. Because STD in Mian and Changestudentnameandage (Student std) are not in the same memory area.

Drag and drop the code 1 compiled. class file into eclipse

public class Com.cy.Client {
Method descriptor #6 () V
Stack:1, Locals:1
public Client ();
0 Aload_0 [This]
1 invokespecial java.lang.Object () [8]
4 return
Line numbers:
[Pc:0, Line:3]
Local Variable table:
[Pc:0, Pc:5] local:this index:0 type:com.cy.Client
Method descriptor #15 ([ljava/lang/string;) V
Stack:4, Locals:2
public static void Main (java.lang.string[] args);
0 new Com.cy.Student [16]
3 DUP
4 LDC <string "CY" > [18]
6 Bipush 24
8 invokespecial com.cy.Student (java.lang.String, int) [20]
astore_1 [STD]
aload_1 [STD]
Invokestatic Com.cy.Client.changeStudentNameAndAge (com.cy.Student): void [23]
Getstatic Java.lang.System.out:java.io.PrintStream [27]
aload_1 [STD]
Invokevirtual com.cy.Student.toString (): java.lang.String [33]
Invokevirtual java.io.PrintStream.println (java.lang.String): void [37]
Return
Line numbers:
[Pc:0, Line:11]
[Pc:12, Line:13]
[Pc:16, Line:15]
[Pc:26, Line:17]
Local Variable table:
[Pc:0, pc:27] Local:args index:0 type:java.lang.string[]
[Pc:12, pc:27] local:std index:1 type:com.cy.Student
Method descriptor #26 (lcom/cy/student;) V
Stack:2, Locals:1
private static void Changestudentnameandage (Com.cy.Student std);
0 ALOAD_0 [STD]
1 LDC <string "Chenyin" > [47]
3 invokevirtual Com.cy.Student.setName (java.lang.String): void [49]
6 ALOAD_0 [STD]
7 Bipush 84
9 invokevirtual com.cy.Student.setAge (int): void [52]
Return
Line numbers:
[Pc:0, Line:25]
[Pc:6, line:26]
[Pc:12, Line:28]
Local Variable table:
[Pc:0, Pc:13] local:std index:0 type:com.cy.Student
}

It can be seen that the Student object is stored in the Mian in Changestudentnameandage (com.cy.Student std) ALOAD_0 [STD] that is the address of the method operation and the address requested in main. So changing the name and age in the Changestudentnameandage method causes the variables in main to change synchronously.

Drag and drop the 2 compiled. class to eclipse

public class Com.cy.Client {
Method descriptor #6 () V
Stack:1, Locals:1
public Client ();
0 Aload_0 [This]
1 invokespecial java.lang.Object () [8]
4 return
Line numbers:
[Pc:0, Line:3]
Local Variable table:
[Pc:0, Pc:5] local:this index:0 type:com.cy.Client
Method descriptor #15 ([ljava/lang/string;) V
Stack:4, Locals:2
public static void Main (java.lang.string[] args);
0 new Com.cy.Student [16]
3 DUP
4 LDC <string "CY" > [18]
6 Bipush 24
8 invokespecial com.cy.Student (java.lang.String, int) [20]
astore_1 [STD]
aload_1 [STD]
Invokestatic Com.cy.Client.changeStudentNameAndAge (com.cy.Student): void [23]
Getstatic Java.lang.System.out:java.io.PrintStream [27]
aload_1 [STD]
Invokevirtual com.cy.Student.toString (): java.lang.String [33]
Invokevirtual java.io.PrintStream.println (java.lang.String): void [37]
Return
Line numbers:
[Pc:0, Line:11]
[Pc:12, Line:14]
[Pc:16, Line:16]
[Pc:26, Line:18]
Local Variable table:
[Pc:0, pc:27] Local:args index:0 type:java.lang.string[]
[Pc:12, pc:27] local:std index:1 type:com.cy.Student
Method descriptor #26 (lcom/cy/student;) V
Stack:4, Locals:1
private static void Changestudentnameandage (Com.cy.Student std);
0 new Com.cy.Student [16]
3 DUP
4 LDC <string "Chenyin" > [47]
6 Bipush 85
8 invokespecial com.cy.Student (java.lang.String, int) [20]
ASTORE_0 [STD]
Return
Line numbers:
[Pc:0, Line:23]
[Pc:12, line:29]
Local Variable table:
[Pc:0, Pc:13] local:std index:0 type:com.cy.Student
}

You can see that STD in main uses astore_1 [STD] and changestudentnameandage uses ASTORE_0 [STD] and it's obvious that the Changestudentnameandage in code 2 uses the new The Student () method causes the address that STD points to in Mian and the address of STD in changestudentnameandage not to use a module. Therefore, the change of STD in changestudentnameandage can not affect Mian.

As the above example shows, when you pass a reference to an object to a method, only the address to which the reference is directed is passed to the method. Or simply copy a copy of the object's reference. The two references point to the same piece of address, and when you manipulate the reference copy directly in the method, it changes the reference copy and the area that the reference points to. However, if the referenced copy is re-directed to a memory area in the method, the operation of the reference copy does not cause a change in the contents of the reference to the address.

Java object references are passed in the method

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.