Differences between references in Java and pointers in C ++

Source: Internet
Author: User

1. Type: reference the data element whose value is the address. The address encapsulated by Java can be converted into a string for viewing. The length does not need to be concerned. The C ++ pointer is an address variable. Its length is generally the length of a computer and can be considered as an int.

2. Memory occupied: No entity exists when the declaration is referenced, and no space is occupied. The C ++ pointer will be assigned a value if it is declared and will not be allocated if it is not used.

3. type conversion: The referenced type conversion may fail, or the IDE cannot pass. The C ++ pointer is only a memory address pointing to it. It is also an address for the program, but it may not be the address that the program wants!

4. Initial Value: The reference initial value is the Java keyword null. The C ++ pointer is an int. if the pointer is not initialized, its value is not fixed.

5. calculation: reference cannot be calculated. The C ++ pointer is int, so it can be computed. Therefore, it is often used as a pointer to replace the array subscript.

6. Control: the reference cannot be calculated, so it can only be controlled in its own program. The C ++ pointer is a memory address and can be calculated. Therefore, it may point to a memory address not used by its own program. It is very dangerous for other programs, it is not easy for your programs to be controlled.

7. Memory leakage: Java references will not cause memory leakage. The C ++ pointer is prone to memory leakage, so programmers should use it with caution and recycle it in time.

8. as a parameter: the Java method parameter only transmits the value. When the reference is used as a parameter, it returns the copy of the referenced value in the function. Therefore, it is meaningless to exchange two referenced parameters in the function, because the function only exchanges the copy value of the parameter, it makes sense to change the attribute of a referenced parameter in the function, because the copy object of the referenced parameter references the same object as the referenced parameter. The C ++ pointer is used as a parameter for the function. In fact, the address he refers to is being operated by the function, therefore, operations using pointer parameters in a function will directly apply to the address (variables, objects, functions, and so on) pointed to by the pointer ).

9. Operators and declarations: "." is used in Java ".". The C ++ pointer uses "->" use "*" to declare the pointer. (It seems this article is useless !)

10. essence: References in Java and pointers in C ++ are essentially intended to find the target (variable object, etc.) to be operated through a reference or pointer, so as to facilitate operations in the program. The difference is that the Java method is safer and more convenient, but without the flexibility and efficiency of C ++.

References in Java and pointers in C ++ should not be mixed together or considered to be the same thing. However, their functions are similar and they are always unconsciously compared. However, when writing a Java program, we 'd better forget the pointer to C ++. When writing a C ++ program, do not remember the reference of Java. As long as you remember that the reference has no size, security, and cannot be calculated, you should be careful when turning strong.

 

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.