Is the Java calling method parameter actually a value or a pass-through?

Source: Internet
Author: User

Before reading "Head first Java", remember that there are mentions, Java in the call method, passing parameters, the use of Pass-by-copy method, passing a copy of the content, that is, the value of the transfer.
To give a simple example:

1  Public classTest {2      Public Static voidMain (string[] args) {3         intNumbera = 1;4         intNumberb = 2;5 swap (Numbera, numberb);6 System.out.println (Numbera);7 System.out.println (numberb);8     }9     Ten      Public Static voidSwapintAintb) { One         intc =A; AA =b; -b =C; -     } the}

Here, the purpose of theswap (int a, int b) method is to exchange the value of the parameter a, b , but this is not achieved.

Although the value of variable a is assigned to a temp variable in the method , the value of variable b is assigned to a, and then the value of temp is assigned to b. At this point,b holds the value from the previous a , anda holds the value in b , at least in the swap () method,a and b The value has been exchanged.
Note, however, that the method of calling parameters in Java is pass-by-copy, that is, although in the swap () method, Parameters a and b(so-called formal parameters) get the Numbera and Numberb values (so-called arguments), but the method to get the value is to assign the value of the argument to the parameter, not to have the parameter directly point to the address of the argument in memory (the so-called pointer).
So, the result of this code output is:

12
Click to view

In this case, the primitive type (Primitive type)int, is that the case for reference types? Let's take a look at the following code:

1 Importjava.util.ArrayList;2 Importjava.util.List;3 4  Public classTest {5      Public Static voidMain (string[] args) {6List<integer> alist =NewArraylist<integer>();7Alist.add (1);8 addtolist (alist);9 System.out.println (alist);Ten     } One  A      Public Static voidAddtolist (list<integer>list) { -List.add (2); -     } the}

In this code, we first create a new ArrayList alist, and add a number "1" to it. Then we try to call the addtolist (list<integer> List) method to add the number "2" to the alist . Will this be successful?

The answer is that it will succeed. The output is:

[1, 2]
Click to view

Nani? Did you just say that Java is not a pass-by-copy value?
Shouldn't this be the case:list is just a copy of alist , no matter what the list does in the addtolist () method, it doesn't end up affecting Alist () ?

I've been thinking about this for a while, and I've been arguing with my colleagues for a similar problem for a long time. He insisted that this was a site, but I remember clearly that "Head first Java" tells us that Java is pass-by-value.
But for now, the method being called actually affects the value of the key method parameter. So where does the problem really go?

For this question, I seriously think about it, plus the recent study of OCA also mentioned this, to collate my own understanding.
First of all, Java is indeed a value (Pass-by-value), in the above example, the pass is indeed a copy, but do not forget, the reference type (Reference type) variables stored in the value of what.
Our reference variable alist is declared as the list<integer> type, that is, thealist variable can only receive the List<integer a reference to the > object.
The "Reference", in fact, is the address, that is, the pointer.
In other words, when we call the addtolist (list<integer> list) method, the value passed to the parameter list is actually a reference to the same object. In the analogy of the remote control and home appliance in Head first Java, we only have a TV and a remote control. Then we copied an identical remote control out, two remote control has the same function, such as switch, select Table, adjust volume and so on. And we have only one TV, so, with another remote control, it is true that this TV can be operated.
So it's clear here that Java is still the Pass-by-value language, and the key is what kind of value you're passing.

Finally, let's take a look at the OCA above on this part of the knowledge point of a small exercise, there are a few small traps, their own analysis:

1  Public classReturningvalues {2      Public Static voidMain (string[] args) {3         intNumber = 1;4String letters = "ABC";5 Number (number);6Letters =letters (letters);7SYSTEM.OUT.PRINTLN (number +letters);8     }9     Ten      Public Static intNumberintNumber ) { Onenumber++; A         returnNumber ; -     } -      the      Public Staticstring Letters (string letters) { -Letters + = "D"; -         returnletters; -     } +}

Do it yourself first, and then look at the answer after you finish it:

1abcd
Click to view

Did you do the right thing? If you do it wrong, the most likely reason is that you didn't notice that line 5th just called the method, and didn't get the return value of the method. When you write your own code in the future must pay attention to avoid making this mistake!

PS: In order to fold the answer, it has already been written with Markdown, obstinately opened a new article with the TINYMCE Editor to change HTML, although no one will see.

Is the Java calling method parameter actually a value or a pass-through?

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.