C++,java value passing, reference passing

Source: Internet
Author: User

1. In addition to the built-in types,STL iterators, and Function objects are "pass-by-value"( value passing ), others use as much Pass-by-reference-const replaces pass-by-value, realizes form "const a&";

2.Java the value passed in, the reference delivery problem:

(1): " in Java parameter passing is passed by value" This sentence means: Pass by value is a copy of the value passed, by reference is actually passed the value of the referenced address, so collectively by value passed.

Java programming languages have only value-passing parameters. When an object instance is passed as a parameter to a method, the value of the parameter is a copy of the object's reference. Point to the same object , the contents of the object can be changed in the called method, but the object's reference ( not the referenced copy ) is never changed.

If the parameter type is the original type, then a copy of the parameter is passed in, that is, the value of the original parameter, which is the same as the value that was previously discussed. If you change the value of a copy in a function, the original value is not changed .

If the parameter type is a reference type, then a copy of the reference parameter is passed in, and the copy holds the address of the parameter. If you do not change the address of the copy in the function, but instead change the value in the address, the changes within the function will affect the parameters passed in. If you change the address of a copy in a function, such as new , the copy points to a new address, and the passed in parameter points to the original address, so the value of the parameter is not changed.

(2): There are only basic types in Java and strings that are defined in the following way are passed by value, others are passed by reference. is to define the string directly using double quotes:stringstr ="Java";


C + + the difference from Java :


test1.cpp:c++ implementation//#include "stdafx.h" #include <iostream>using std::cout;class a{public:int num = 1;}; void Changea (a& A) {a.num = 2;} void Changeaa (a& a) {a other;a = Other;a.num = 3;} int _tmain (int argc, _tchar* argv[]) {A test;cout<<test.num<<std::endl;//output 1changeA (test); cout << Test.num << std::endl;//output 2changeAA (test), cout << test.num << std::endl;//output 3return 0;}

</pre><pre class= "java" name= "code" >//javapackage Test;public class Main10 {private static Class A{public int num = 1;} private static void Changea (a a) {a.num = 2;} private static void Changeaa (a a) {a = new A (); a.num = 3;} public static void Main (string[] args) {A test = new A (); System.out.println (test.num);//output 1changeA (test); System.out.println (test.num);//output 2changeAA (test); System.out.println (test.num);//Output 2}}












C++,java value passing, 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.