Java method parameter-value call, reference call problem

Source: Internet
Author: User
Tags call by reference

(blog content from the core Java Volume one)

1. XX Call: Method parameters are passed in the programming language:

Reference Call (call by reference) : Indicates that the method received a variable address provided by the caller.

value is called (Call by value) : Indicates that the method received a value supplied by the caller.

named Call (Call by name) : has become history.

2. Java uses value calls, and only values are called. This means that the method obtains a copy of the parameter value, not the parameter value itself, so the method cannot modify any of the parameter variables passed to it.

Look at the following code:

Public class Test {

Public Static void Main (string[] args) {

int percent = 10;

Triplevalue (percent);

System. out. println (percent);

}

Public Static void triplevalue (int x) {

x = x * 3;

}

}

Output: 10

As you can see, regardless of how this method is called, after execution, The value of Persent is still ten. Specific implementation process:

The ①x is initialized to a copy of the Persent (that is, ten). At this point x is x,persent is persent, but the value is the same.

②x is multiplied by 3 to be equal to. But persent is still ten.

③ This method ends, the parameter variable x is no longer used.

3. However, there are two types of method parameters: Basic data Type (numeric, Boolean), object reference.

A method cannot modify a parameter of a base data type. The object reference is different as a parameter. At this point, the method obtains a copy of the object reference. object reference and its copy, referencing an object at the same time.

Look at the following code:

Public class Test {

Public Static void Main (string[] args) {

Circle C = new circle ();

C.R = 1;

bigger (c);

System. out. println (C.R);

}

Public Static void bigger (Circle C2) {

C2.R = c2.r+3;

}

}

class circle{

int R;

}

Output: 4

Specific implementation process:

The ①C2 is initialized to a copy of the C value, which is a reference to an object.

The ②bigger method is applied to this object reference. Therefore, the R value of the Circle object referenced by C2 and C increases by 3.

after the ③ method finishes, the parameter variable c2 is no longer used. C continue referencing this R value has increased by 3 for the circle object.

4. many programming languages (especially C + + and Pascal) provide two ways to pass parameters: value invocation and reference invocation. Some programmers think that the Java programming language uses reference calls to objects, which is actually not true. As this misunderstanding has a certain universality, so here is a counter-example, to elaborate on this issue.

First, write a method that swaps two round objects:

Public Static void swap (Circle x,circle y) {

Circle temp = x;

x = y;

y = temp;

}

If the Java programming language uses reference calls to objects, this method should be able to achieve the effect of exchanging data:

Circle A = new circle (1);

Circle B = new circle (2);

Swap (A, B);

System. out. println (A.R);

System. out. println (B.R);

Output:

1

2

However, the method does not alter the object references stored in variables a and b . The parameters x and y of the swap method are initialized to a copy of two object references, and this method swaps the two copies,a and the b the object reference in does not change. Finally, in vain, the parameter variables x and y are discarded at the end of the method . This process illustratesthatJava does not use reference calls to objects, but rather to value passing.

5. Summary:

(1) A method cannot modify a parameter of a base data type (that is, numeric and Boolean).

(2) A method can manipulate the state of the object it refers to by using the object parameter.

(3) A method cannot have an object argument referencing a new object.

Note:C + + has value calls and reference calls. The reference parameter is marked with a & symbol. For example, it is easy to implement the Void swap (circle& a,circle& b) method and implement the purpose of modifying their reference parameters.

Java method parameter-value call, reference call problem

Related Article

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.