The similarities and differences between C + + parameter passing and Java parameter passing

Source: Internet
Author: User

1, C + + Pass-through method:

(1) basic type parameter passing

int main () {int TT = 22;//transmission basic type Common (TT); cout << "tt value=" << tt << Endl;} int common (int a) {int i = 2;a = I;cout << a << endl;return 0;} The above code output: 2                2

(2) pointer type parameter passing

<span style= "White-space:pre" ></span>int main () {int tt = 22;int *P1 = &tt;//pass pointer int v = valuepass (p1); cout << "*p1=" << *p1 << Endl;} int valuepass (int *p1) {int a = 1;P1 = &a;cout << *p1 << endl;return 0;} <p> above code output: 1 <span style= "WHITE-SPACE:PRE;" ></span>22

(3) reference type parameter passing

int main () {int tt = 22;int *P1 = &tt;//pass pointer int v = valuepass (p1); cout << "*p1=" << *p1 << endl;//Transmission    With V = Refpass (p1), cout << "*p1 refer=" << *p1 << Endl; }<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= "code" class= "CPP" ><span style= "White-space:pre" ></span>int refPass ( int *&p1) {int a =1;p1 = &a;cout << *p1 << endl;return 0;}
<p> above code output: 1 </p><p><span style= "WHITE-SPACE:PRE;" > </span>1</p>


As you can see, the parameters from (1)---(2)--(3) are getting stronger, the range of parameter changes is increasing, (1) is a pure copy, (2) The address copy is passed, and (3) the variable itself passes.

In Java, however, only the usage of (1) and (2) is provided, but it is consistent with the way that C + + handles data.

Details of the Java parameter passing another time blog: http://blog.csdn.net/tangkund3218/article/details/18087543




The similarities and differences between C + + parameter passing and Java parameter 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.