Home  >  Article  >  Java  >  The difference between references in java and pointers in c++

The difference between references in java and pointers in c++

王林
王林Original
2019-12-31 16:38:262421browse

The difference between references in java and pointers in c++

Java references and C pointers both point to a memory address. Operations on memory data are completed through references or pointers, but they have differences in implementation, principles, functions, etc. the difference.

(1) Type: Reference to a data element whose value is an address. The address encapsulated by Java can be converted into a string for viewing, and there is no need to worry about the length. A C pointer is a variable that holds an address. The length is generally the computer word length and can be considered an int.

Free online video tutorial recommendation: java video tutorial

(2) Memory occupied: There is no entity when the reference is declared and it does not occupy space. A C pointer will only be assigned a value if it will be used after it is declared. Memory will not be allocated if it is not needed.

(3) Type conversion: The type conversion of the reference may not be successful, an exception will be thrown at runtime or the compilation will not pass. The C pointer indicates a memory address and points to the memory. To the program, it is still an address, but the address pointed to may not be what the program wants.

(4) Initial value: The initial value of the reference is the java keyword null. C pointers are ints. If the pointer is not initialized, its value will not be fixed, which is very dangerous.

(5) Calculation: Reference cannot be calculated. C pointers are int, which can be calculated, such as or --, so pointers are often used instead of array subscripts.

(6) Memory leak: Java references will not cause memory leaks. C pointers are prone to memory leaks, so programmers should use them carefully and recycle them in time.

(7) As a parameter: Java method parameters only pass values. When a reference is used as a parameter, it will give a COPY of the value referenced in the function, so it is meaningless to exchange two reference parameters within the function. Because the function only exchanges the COPY value of the parameter, it makes sense to change the properties of a reference parameter within the function, because the object referenced by the COPY of the reference parameter is the same object as the reference parameter.

C When a pointer is used as a parameter for a function, it is actually the address it points to that is operated by the function, so operations using pointer parameters within the function will directly affect the address pointed to by the pointer (variable, object, function, etc.).

Recommended related articles and tutorials: java introductory tutorial

The above is the detailed content of The difference between references in java and pointers in c++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn