"Reprint" The difference between a reference in Java and a pointer in C + +

Source: Internet
Author: User
Tags java reference

1. Type: Reference the data element whose value is the address, Java encapsulates the address, can be turned into a string view, length can not be concerned. C + + pointer is an address-loaded variable, the length of the computer is usually a word, can be considered an int.

2. Occupied memory: There is no entity at the time of referencing the declaration and does not occupy space. C + + pointers are assigned if they are declared, and memory is not allocated if they are not used.

3. Type conversion: The type conversion of a reference, or it may not succeed, throw a different, or the IDE will not pass. C + + pointer is just a memory address, pointing there, to the program is also an address, but may refer to the address is not what the program wants!

4. Initial value: The reference initial value is the Java keyword NULL. The C + + pointer is int, and if you do not initialize the pointer, then his value is not fixed.

5. Calculation: References cannot be calculated. C + + pointers are int, so he can calculate, so often use pointers instead of array subscripts.

6. Control: References cannot be calculated, so he can only be controlled in his own program. C + + pointers are memory addresses and can be computed, so he might point to a memory address that is not used by his program, which is dangerous for other programs and is not easily controlled for his own programs.

7. Memory leaks: Java references do not cause a memory leak. C + + pointers are prone to memory leaks, so programmers should use them carefully and recycle them in a timely manner.

8. As a parameter: Java's method parameter is only a value, reference is used as a parameter, back to the function of the reference value of copy, so it is meaningless to exchange two reference parameters within the function, because the function only exchanges the copy value of the parameter, but it is meaningful to change the property of a reference parameter within the function, Because the reference parameter of copy refers to an object that is the same object as the reference parameter. The C + + pointer is used as a parameter to the function, in fact, the address that he refers to is manipulated by the function, so the operation with pointer parameters within the function will directly affect the address (variables, objects, functions, etc.) that the pointer points to.

9. Operators and declarations: "." In Java. The C + + pointer declares the pointer with "*". (It doesn't seem to work!) )

10. Nature: References in Java and pointers in C + + are essentially intended to be used in a program by means of something called a reference or pointer, to find the target (variable object, etc.) to manipulate. The difference is that the Java approach is more secure and convenient, but without the flexibility and efficiency of C + +.

References in Java and pointers in C + + are best not to be mixed together, not to be considered one thing, except that their roles are somewhat similar, and are often unconsciously contrasted. But when we write Java programs, it is best to forget C + + pointers, when writing C + + programs, do not always remember the Java reference. Just remember to quote no size, security, not calculated, strong turn to be careful on the good.

"Reprint" The difference between a reference in Java and a pointer in C + +

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.