Java value passing or reference passing

Source: Internet
Author: User

There are often questions about whether the Java language is a value pass or a reference pass. Here I would like to explain my personal views on this question. Many of the arguments about how the Java parameter is passed will be compared to C + + or C #, so I'm going to make a simple description of how to pass the parameters of C + + or C #. There is a clear reference delivery method in the C + + or C # language to differentiate the value delivery method. The use of pointers to implement reference passing in the C + + language defines the data type of the parameter as a pointer type when the function is defined. When a function is called, the variable address or pointer variable is passed as an argument. The actual pass is a copy of the address (pointer), both the argument and the parameter point are the same address, so you can assume that the modification of the parameters in the function can be "brought back". When the value is passed, the argument is worth copying, and any modification of the "copy" in the function does not affect the original value. To give a simple example, a value is equivalent to a TV set, and the pointer is the remote control of the TV. Referring to the transfer is equivalent to copying a new remote control, with the new remote control to change the platform, the TV will be "changed". Value passing is equivalent to copying a new TV set, and your adjustment to the new TV will not affect the original TV.

The data types of the Java language are divided into basic types and reference types. The base type data contains only the value part. Reference type data contains a reference and a value of two parts, which refer to pointers similar to the C language (Java deletes the concept of pointers for security reasons) but we can't manipulate it like C. I can manipulate the value it points to by reference, and we can't leave the reference to find the value part, because we can't get the real address of the value in memory. When a method is called, if the parameter is data of the base data type, a copy of the value itself is passed, and if the parameter is a reference type of data, the referenced copy is passed. So whether Java is a value pass or a reference pass is determined by the data type of the parameter being passed, or it is a pseudo proposition at all. For the basic data type parameter to implement the effect of a reference pass, you can change the parameter type of the base data type to its corresponding wrapper type, because the wrapper type belongs to the reference type. Starting from JDK1.5 the Java language enables automatic conversion between basic data types and wrapper types, simplifying the complexity of the code.

As for the C # language, when you define a method, you can force the argument to pass through a modifier before the parameter type is either a value pass or a reference pass, otherwise it is similar to the Java language.


Java value passing or reference passing

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.