In a few words, you can find out whether to pass the Java parameter value or to reference it.

Source: Internet
Author: User

Author: fbysss
MSN: jameslastchina@hotmail.com
Blog: blog.csdn.net/fbysss
Disclaimer: This article is original from fbysss. for reprinting, please indicate the source
Keywords: Java value transfer reference

This is an old-fashioned question that has aroused numerous debates, but it can be said that there has never been a satisfactory answer.

Some people have to say that the reference is the reference value in the stack, and that the reference value is not included in the stack. Let's look at this classic famous saying:

O 'Reilly's Java in a nutshell by David Flanagan (see resources) puts it best: "Java manipulates objects 'by reference,' but it passes object references to Methods 'by value. '"

We can also see from here that David is not so blunt, but from which point you can understand this question. If you are used to the Understanding Method of passing parameters in C ++, why can't we do this?

Someone has summarized the following:

See

Http://www.javaresearch.org/article/3156.htm

1. objects are passed by reference.
2. Java applications have only one parameter passing mechanism, that is, passing by value.
3. Pass by value means that when a parameter is passed to a function, the function receives a copy of the original value.
4. Pass by reference means that when a parameter is passed to a function, the function receives the memory address of the original value, not the copy of the value.
That's right, but there are too many words. The second one is confused. You have to read four carefully before you can understand it. The doubt about the string type is not solved.

Why is it so simple? Why can't no one go with C ++, so I want to work with Java?

Summary:

1. The object is a reference

2. The original type is to pass the value.

3. Because immutable types such as string do not provide their own modified functions, each operation is a new object, so special treatment is required. It can be considered as passing values.

Other references:

Http://www-128.ibm.com/developerworks/cn/java/l-jpointer/index.html

Http://dreamhead.blogbus.com/logs/2005/05/1189478.html

Http://www.javaeye.com/topic/12961

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.