In Java and C ++, objects are passed as parameters.

Source: Internet
Author: User

In Java and C ++, objects are passed as parameters.

The problem is caused by the use of objects as parameters in Java and debugging Based on the C ++ idea. It is found that the results are different from those in C ++.

The cause of this problem is that Java and C ++ have different interpretations of objects.

In C ++, the object is called as a parameter. When the real parameter is passed through the form parameter, the object (real parameter) is called) (if there is no explicit definition of the copy constructor, the default copy constructor is automatically called. Its function is to copy the objects in the real parameter to the form parameter as is, this involves the issue of deep copy and shallow copy, but does not affect the analysis of this issue .), The actual operation of the function is to copy the object without affecting the original object.

In Java, when an object is used as a parameter, the form parameter is referenced by the object initialized as a real parameter. If you operate the form parameter, the real parameter (original object) is affected ). This is a bit similar to "Reference call" in C ++, but this is not the case. In Java, "pass value call" is used. The reason for different results is that the object names in Java and C ++ are different. In C ++, the relationship between "class" and "object" can be analogous to the relationship between "type" and "variable". When an object is defined, the memory space is allocated, it actually exists. In Java, the object definition only defines the "object variable". It only points to an object. When the new operator is used, this object is constructed. The object name in Java is a bit like a pointer to an object in C ++. When passed as a parameter, the address of the object in memory is passed.

In C ++ and Java, the String class Object str is stored as follows:

 

The following is an example of passing objects in Java as parameters:

 1 public class Test1 { 2        public static void main(String[] args) { 3         StringBuffer str = new StringBuffer("Hello "); 4         System.out.println("Before change, str = " + str); 5         changeData(str); 6         System.out.println("After changeData, str = " + str); 7     } 8                                                                                      9        public static void changeData(StringBuffer strBuf) {10         strBuf.append("World!");11     }12 }

According to the above analysis, when str is passed to strBuf, the address stored in Hello is passed, and the output result is:

Before change, str = HelloAfter changeData, str = Hello World!

Modify the above Code as follows:

 1 public class Test2 { 2        public static void main(String[] args) { 3         StringBuffer str = new StringBuffer("Hello "); 4         System.out.println("Before change, str = " + str); 5         changeData(str); 6         System.out.println("After changeData, str = " + str); 7     } 8                                                                                                                                                                                             
9 public static void changeData(StringBuffer strBuf) {10 strBuf = new StringBuffer("Hi ");11 strBuf.append("World!");12 }13 }

According to the above analysis, when str is passed to strBuf, both strBuf and str will store the Hello storage address, such:

Then, after strBuf = new StringBuffer ("Hi"); is executed, the above relationship will become:

In this case, the strBuf does not store the Hello address, but the Hi address. After the new operator is successfully operated, a new storage area will be created in the memory.

When strBuf. append ("World! "); In this case, the operation will be Hi, Not Hello, and the result will be:

The output result is as follows:

Before change, str = HelloAfter changeData(n), str = Hello

 

  

 

Related Article

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.