Only value passing exists in Java

Source: Internet
Author: User
Tags integer numbers

There is no reference transfer in Java (that is, address transfer). For variables (either basic data type or reference data type), it can be understood as an address. There is a copy operation. Give a column:

1. In method parameters, the basic data type is passed.
Defines a method for exchanging Two integer numbers:

1
Public static void swap (int a, int B ){
2
Int temp =;
3
A = B;
4
B = temp;
5
System. out. println ("In swap: a =" + a + ", B =" + B );
6
}
Initialize Two integer variables in the main method and call the swap method:

1
Public static void main (String [] args ){
2
Int a = 1;
3
Int B = 2;
4

5
System. out. println ("Before swap: a =" + a + ", B =" + B );
6
Swap (a, B );
7
System. out. println ("After swap: a =" + a + ", B =" + B );
8
}
Note: assume that the real parameter a is 0x0001 and the real parameter B is 0x0002. They point to 1 and 2 respectively.


Output result:

Before swap: a = 1, B = 2
In swap: a = 2, B = 1
After swap: a = 1, B = 2

Note: When the swap method is called, it is assumed that the parameter a is 0x0003 and the parameter B is 0x0004. In this case, the arguments a and B copy the values they point. According to the order in which the called method parameter list corresponds to the declared method parameter list, 0x0003 points to 1 and 0x0004 points to 2. When the corresponding value is passed, the copied values are 1 and 2.

The function of the swap method is to exchange the values pointed to by 0x0003 and 0x0004. That is, the value pointed to by 0x0003 is changed to 2, and the value pointed to by 0x0004 is changed to 1. Therefore, real parameters a and B are not affected.

If a reference is passed, the parameter value is the address, that is, the copied values are 0x0001 and 0x0002.

The exchange effect will be reflected on the real parameters a and B.


2. In method parameters, the referenced data type is passed.

Here, the Person class is used as an example:

01
Class Person {
02
Private String name;
03

04
Public Person (String name ){
05
This. name = name;
06
}
07

08
@ Override
09
Public String toString (){
10
Return "My name is" + this. name + ".";
11
}
12
}
Similarly, define a swap method to exchange two reference data types:
1
Public static void swap (Person c, Person d ){
2
Person temp = null;
3
Temp = c;
4
C = d;
5
D = temp;
6
System. out. println ("In swap:" + c + "," + d );
7
}
Initialize two references of the Person type in the main method and call the swap method:

1
Public static void main (String [] args ){
2
Person c = new Person ("c ");
3
Person d = new Person ("d ");
4
 
5
System. out. println ("For referenced data type ");
6
System. out. println ("Before swap:" + c + "," + d );
7
Swap (c, d );
8
System. out. println ("After swap:" + c + "," + d );
9
}
Note: assume that the real parameter c is 0x0001 and the real parameter d is 0x0002. They have their own referenced objects 0x000c and 0x000d. These are the storage addresses of objects in the heap.


Output result:

For referenced data types
Before swap: My name is c., My name is d.
In swap: My name is d., My name is c.
After swap: My name is c., My name is d.

Note: When the swap method is called, it is assumed that the parameter c is 0x0003 and the parameter d is 0x0004. According to the order in which the called method parameter list corresponds to the declared method parameter list, the parameter c (0x0003) references the real parameter c (0x0001) the referenced object. The parameter d (0x0004) references the object referenced by the real parameter d (0x0002. When the corresponding value is passed, the copied values are 0x000c and 0x000d.

In this case, the swap method is used to exchange 0x0003 and 0x0004 for object reference.

If reference transfer exists, 0x0001 and 0x0002 are copied.

The exchange effect will be reflected in the real parameters c and d.

Note: variables are stored in the stack area, and objects are stored in the stack area.

Illustration of the transfer of reference data type values:
1. Before swap is called:

2. When swap is called:

3. After the swap method is executed:

4. After swap is called:


The complete instance program code is as follows:

01
Package com. test;
02
 
03
Public class Test {
04
Public static void swap (int a, int B ){
05
Int temp =;
06
A = B;
07
B = temp;
08
System. out. println ("In swap: a =" + a + ", B =" + B );
09
}
10

11
Public static void swap (Person c, Person d ){
12
Person temp = null;
13
Temp = c;
14
C = d;
15
D = temp;
16
System. out. println ("In swap:" + c + "," + d );
17
}
18

19
Public static void main (String [] args ){
20
Int a = 1;
21
Int B = 2;
22
Person c = new Person ("c ");
23
Person d = new Person ("d ");
24

25
System. out. println ("for basic data type ");
26
System. out. println ("Before swap: a =" + a + ", B =" + B );
27
Swap (a, B );
28 www.2cto.com
System. out. println ("After swap: a =" + a + ", B =" + B );
29

30
System. out. println ("For referenced data type ");
31
System. out. println ("Before swap:" + c + "," + d );
32
Swap (c, d );
33
System. out. println ("After swap:" + c + "," + d );
34
}
35
}
36
 
37
Class Person {
38
Private String name;
39

40
Public Person (String name ){
41
This. name = name;
42
}
43

44
@ Override
45
Public String toString (){
46
Return "My name is" + this. name + ".";
47
}
48
}
Summary:
1. Only value passing exists in Java, and no reference passing exists.
2. If the parameter reference modifies the status of the referenced object, it will also be reflected in the real parameter reference.

Author: Wu xiachi

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.