Java value transfer call

Source: Internet
Author: User

1. The basic type is used as a parameter,
When a method parameter is of the basic type, it is called by passing values.
(1) The value of the passed real parameter is a copy.
(2) One-way transmission. The parameter value can be modified inside the method, but such modification does not affect the corresponding real parameters.
Intuitively, the value passing process is equivalent to a value assignment process. The real parameters are the right values and the left parameters. They are associated only at the time of the call, and there is no association between them in the future.
2 composite type as parameter
If the real parameter is a class object, the system will pass the address value of the object to the form parameter when calling the corresponding method.
Although there is no pointer in Java, programmers do not need to master it, but there are still pointers in the system.
Because c ++ provides both value transfer and application call, some programmers also think that ijava objects use reference transfer, which is a misunderstanding. Java uses the call method of passing address values. In some cases, although the call effect is the same as that of the reference call, the difference between the two can be seen in other cases.
For example
Class onlytest {
Private int x = 0;
Public void setx (int ix ){
X = IX;
}
Public int getx (){
Return X;
}
}
Public class booleantest {
Public void swap (onlytest one, onlytest two ){
Onlytest temp;
Temp = one;
One = two;
Two = temp;
}
Public void showdiffer (){
Onlytest ox = new onlytest ();
Onlytest Oy = new onlytest ();
Ox. setx (100 );
Oy. setx (200 );
System. Out. println ("value before swap method call ");
System. Out. println ("Ox. x =" + ox. getx () + ", oy. x =" + oy. getx ());
Swap (OX, Oy );
System. Out. println ("value after swap method call ");
System. Out. println ("Ox. x =" + ox. getx () + ", oy. x =" + oy. getx ());
}
Public static void main (string ARGs []) {
Booleantest Va = new booleantest ();
Va. showdiffer ();
}
}

In method swap (), the form parameter is two onlytest objects. If it is a reference call, exchanging the values of the two objects will affect the real parameters ox and oy, the output result is as follows:
Value before the swap method is called
Ox. x = 100, oy. x = 200
Value after the swap method is called
Ox. x = 100, oy. x = 200
Java uses the address value transfer method. Therefore, when passing parameters, the address value is transmitted from ox to one, And oy to two. in this case, if ox and one point to the same region in the memory, oy and two point to the same region. In the exchange, the addresses of one and two are converted, But Ox and oy are not changed.

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.