Comparison between C ++ function parameters and Java Transmission

Source: Internet
Author: User

The comparison between C ++ function parameters and Java transmission is a headache for many technical staff and some developers, after reading this article, you will understand what is Java value passing and what is C ++ function parameter value passing.

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 also includes the type of the content that cannot be changed once initialized, such as String and BufferString, which can be changed after initialization. Then let's take a look at the sample code:

 
 
  1. public class Test {  
  2.  public static void main(String args[]) {  
  3.   Integer interger1, interger2;  
  4.   int i, j;  
  5.   interger1 = new Integer(10);  
  6.   interger2 = new Integer(50);  
  7.   i = 5;  
  8.   j = 9;  
  9.   System.out.println("Before Swap, Interger1 is " + interger1);  
  10.   System.out.println("Before Swap, Interger2 is " + interger2);  
  11.   swap(interger1, interger2);  
  12.   System.out.println("After Swap Interger1 is " + interger1);  
  13.   System.out.println("After Swap Interger2 is " + interger2);  
  14.   System.out.println("Before Swap i is " + i);  
  15.   System.out.println("Before Swap j is " + j);  
  16.   swap(i, j);  
  17.   System.out.println("After Swap i is " + i);  
  18.   System.out.println("After Swap j is " + j);  
  19.  
  20.   StringBuffer sb = new StringBuffer("I am StringBuffer");  
  21.   System.out.println("Before change, sb is <" + sb + ">");  
  22.   change(sb);  
  23.   System.out.println("After change sb is <" + sb + ">");  
  24.  }  
  25.  
  26.  public static void swap(Integer ia, Integer ib) {  
  27.   Integer temp = ia;  
  28.   ia = ib;  
  29.   ib = temp;  
  30.  } 

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 the object type, we can also think that the C ++ function parameter is passed to the "memory unit" that stores the pointer of the object type. Although Java does not have the pointer concept, but this does not prevent us from understanding ). In this way, in the swap function, doing any operation on the value of its pointer 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 itself through operations similar. (The StringBuffer object itself has only one copy.) Then C ++ is used. The basic types such as int values in the StringBuffer object are passed to everyone.

Next, another type of value passing can be referred to as pass-by-value argument of pointer (this is similar to the object type value passing in Java described above). You can perform the * operation, change the pointer value. The sample program is as follows:

 
 
  1. #include<iostream.h> 
  2.  
  3. int main(){  
  4.  void test(int*, const char*);  
  5.  int i = 1;  
  6.  int* iptr = &i;  
  7.  cout<<"Before pass-by-value:"<<"\n\n";  
  8.  cout<<"i = "<<i<<", It's value of i"<<endl;  
  9.  cout<<"&i = "<<&i<<", It's address of i and value of iptr"<<endl;  
  10.  cout<<"*iptr = "<<*iptr<<", It's value of i"<<endl;  
  11.  cout<<"iptr = "<<iptr<<", It's value of iptr and address of i"<<endl;  
  12.  cout<<"&iptr = "<<&iptr<<", It's address of iptr-self"<<"\n\n";  
  13.    
  14.  test(iptr, "pass-by-iptr");  
  15.  
  16.  test(&i, "pass-by-&i");  
  17.  
  18.  return 0;  

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

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design
  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.