Whether Java is a value pass or a reference pass-summary

Source: Internet
Author: User

This topic has always been a hot topic for Java programmers, arguing constantly, but whether you search the Baidu or to see the official documents indicated in the same, get only one conclusion:Java only value is passed .

Here is no code to explain carefully, let's take a look at some of the arguments:

1.

"Java is always value-passed . Unfortunately, they (Sun) decided to call ' an object's address ' as ' reference '. When we pass the value of an object, we pass his reference . This will confuse beginners. ”

2.

"The crux of the matter is that the word ' reference ' is completely different from the word ' references ' in Java when it expresses ' passing by reference '.

' referencing ' in Java usually means ' a reference to an object '. But in programming language theory, the term ' reference/value passing ' is said to be ' a reference to a storage unit that stores variables ', which is completely different. ”

3.

From Ken Arnold and James Gosling (the former is not very familiar with the latter I know is the father of Java) "Java Programming Language Second Edition (PS: Although now all out of the fourth edition)":

"Some people would mistakenly say that objects in Java are" reference passing. " The term "pass by reference" correctly means that when a party arguments are passed to a function, the calling function obtains a reference to the original value instead of a copy of his value (my humble humble opinion: the reference to the original value here is a copy of the object that we often say ' passed ' '). If the function modifies its arguments, the values in the calling code are also changed, because the parameters and parameters are the same memory slots. Quite certain, Java has only one way of passing values -which helps to keep things simple. ”

4.

"For those who need more (evidence) to be convinced: you cannot write a swap function directly in Java to exchange two values passed as parameters, whether they are objects or simple values. To date, the swap function is trivial for those languages that allow reference passing .

So Java is a value passed, including a pointer to an object of a method. It's so simple.

The real problem is that Java likes to call itself without pointers, which is part of its reputation as a safe language. So although it actually has pointers (more secure than C + +), it must be called something other than "pointers". Sun chooses to call it "reference", although it is not what the amateur enterprise usually thinks when distinguishing between "pointers" and "references". Java " references " are not like C + + " references ". Java has a pointer to an object whose value is passed to the method. ”

=============================== Split Line ===============================

Summary:"Java is always value-passed ." We think that by reference, the value of the address in the reference (and a copy of it) is actually passed, or it can be understood as the value of the pointer. What we are puzzled about is the meaning of the word "citation", and the reference in Java is different from what we widely think.

PS: If the article where there are different views or doubts, can be pointed out, together to explore, if there are errors, please forgive me, put forward to do amendments, thanks.

Resources:

1.Stack overflow:is Java "pass-by-reference" or "Pass-by-value"?

2.wiki:java Passes by Value

3. There is also a clear analysis of whether Java is a value pass or a reference pass?

Whether Java is a value pass or a reference pass-summary

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.