Comparison between Java and C ++ function parameter passing

Source: Internet
Author: User

In short, Java uses pass-by-value, while C ++ includes pass-by-value) and pass-by-reference ).

First, let's talk about Java. First, let's make a few notes:

In Java, there are only two types: basic type and object type inherited from object, the object type includes the string type. Once initialized, the content type and bufferstring can be changed.

To change the content type.

Then let's take a look at the sample code:

 

Java code

package test;   

public class Test {
public static void main(String args[]) {
Integer interger1, interger2;
int i, j;
interger1 = new Integer(10);
interger2 = new Integer(50);
i = 5;
j = 9;
System.out.println("Before Swap, Interger1 is " + interger1);
System.out.println("Before Swap, Interger2 is " + interger2);
swap(interger1, interger2);
System.out.println("After Swap Interger1 is " + interger1);
System.out.println("After Swap Interger2 is " + interger2);
System.out.println("Before Swap i is " + i);
System.out.println("Before Swap j is " + j);
swap(i, j);
System.out.println("After Swap i is " + i);
System.out.println("After Swap j is " + j);

StringBuffer sb = new StringBuffer("I am StringBuffer");
System.out.println("Before change, sb is <" + sb + ">");
change(sb);
System.out.println("After change sb is <" + sb + ">");
}

public static void swap(Integer ia, Integer ib) {
Integer temp = ia;
ia = ib;
ib = temp;
}

public static void swap(int li, int lj) {
int temp = li;
li = lj;
lj = temp;
}

public static void change(StringBuffer ia) {
ia.append(", but my content can be changed");
//ia = new StringBuffer(",but my content can be changed");
}
}
 

Output:

Before swap, interger1 is 10
Before swap, interger2 is 50
After swap interger1 is 10
After swap interger2 is 50
Before swap I is 5
Before swap J is 9
After swap I is 5
After swap J is 9
Before change, Sb is <I am stringbuffer>
After change Sb is <I am stringbuffer, but my content can be changed>

This is a good explanation. for basic types such as int, a copy of the "memory unit" that stores the int value is passed in, so the int in the function SWAp and the int outside are not a thing at all. Of course, it cannot be reflected out to influence the outside.

. For object types, we can also think that what is passed in is a copy of the "memory unit" for storing object type pointers (although there is no pointer concept in Java, but this does not prevent us from understanding ). In this way,

In the swap function, doing any operation on the value of the pointer itself will certainly not affect the integer outside, because the value in the "memory unit" of interger1 and interger2 will not change, the object type it points to has not changed.

Next, we need to explain a problem, that is, the stringbuffer type object. Because its content can be changed, the "Pointer" in the change function changes the stringbuffer object through operations similar "*".

Itself. (The stringbuffer object has only one copy)

Then let's talk about C ++. The passing of basic types such as int values in it is easy to understand, so we won't talk about it here. Another value transfer can be called pass-by-value argument of pointer (this class ).

Similar to the object type value transfer in Java mentioned above), you can use the * operation to change the value pointed to by the pointer. The sample program is as follows:

CPP Code
#include<iostream.h>   

int main(){
void test(int*, const char*);
int i = 1;
int* iptr = &i;
cout<<"Before pass-by-value:"<<"\n\n";
cout<<"i = "<<i<<", It's value of i"<<endl;
cout<<"&i = "<<&i<<", It's address of i and value of iptr"<<endl;
cout<<"*iptr = "<<*iptr<<", It's value of i"<<endl;
cout<<"iptr = "<<iptr<<", It's value of iptr and address of i"<<endl;
cout<<"&iptr = "<<&iptr<<", It's address of iptr-self"<<"\n\n";

test(iptr, "pass-by-iptr");

test(&i, "pass-by-&i");

return 0;
}

void test(int* iiptr, const char* string){
cout<<"When pass-by-value and :"<<"\n\n";
cout<<"*iiptr = "<<*iiptr<<", It's value of i"<<endl;
cout<<"iiptr = "<<iiptr<<", It's value of iiptr and address of i"<<endl;
cout<<"&iiptr = "<<&iiptr<<", It's address of iiptr-self, different with iptr!"<<"\n\n";
}

Output:

Before pass-by-value:

I = 1, it's value of I
& I = 0x0012ff7c, it's address of I and value of iptr
* Iptr = 1, it's value of I
Iptr = 0x0012ff7c, it's value of iptr and address of I
& Iptr = 0x0012ff78, it's address of iptr-self

When pass-by-value and:

* Iiptr = 1, it's value of I
Iiptr = 0x0012ff7c, it's value of iiptr and address of I
& Iiptr = 0x0012ff24, it's address of iiptr-self, different with iptr!

When pass-by-value and:

* Iiptr = 1, it's value of I
Iiptr = 0x0012ff7c, it's value of iiptr and address of I
& Iiptr = 0x0012ff24, it's address of iiptr-self, different with iptr!

In C ++, the second type is pass-by-reference ). See the following example:

CPP Code
  1. # Include <iostream. h>
  2. Int main (){
  3. Void test (Int &, const char *);
  4. Int I = 1;
  5. Int & iref = I;
  6. Cout <"before pass-by-reference:" <"\ n ";
  7. Cout <"I =" <I <", it's value of I" <Endl;
  8. Cout <"& I =" <& I <", it's address of I and value of iptr" <Endl;
  9. Cout <"iref =" <iref <", it's value of iref and value of I" <Endl;
  10. Cout <"& iref =" <& iref <", it's address of iref-self, the same as I! "<" \ N ";
  11. Test (iref, "pass-by-iref ");
  12. Test (I, "pass-by-I ");
  13. Return 0;
  14. }
  15. Void test (Int & iiref, const char * string ){
  16. Cout <"when pass-by-reference and" <string <"\ n ";
  17. Cout <"iiref =" <iiref <", it's value of iiref and value of I" <Endl;
  18. Cout <"& iiref =" <& iiref <", it's address of iiref-self, the same as I! "<" \ N ";
  19. }

Output:

Before pass-by-reference:

I = 1, it's value of I
& I = 0x0012ff7c, it's address of I and value of iptr
Iref = 1, it's value of iref and value of I
& Iref = 0x0012ff7c, it's address of iref-self, the same as I!

When pass-by-reference and pass-by-iref

Iiref = 1, it's value of iiref and value of I
& Amp; iiref = 0x0012ff7c, it's address of iiref-self, the same as I!

When pass-by-reference and pass-by-I

Iiref = 1, it's value of iiref and value of I
& Amp; iiref = 0x0012ff7c, it's address of iiref-self, the same as I!

The reference here is clear, that is, an alias of the passed parameter, or more directly, that is, the passed parameter itself, but the name is different. Now that you have all been passed, you can do whatever you want in the function.

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.