The difference between references in java and pointers in c++
Dec 31, 2019 pm 04:38 PMJava 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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Computed has a cache, and multiple accesses are not recalculated when the dependency remains unchanged, while methods are executed every time they are called; 2.computed is suitable for calculations based on responsive data. Methods are suitable for scenarios where parameters are required or frequent calls but the result does not depend on responsive data; 3.computed supports getters and setters, which can realize two-way synchronization of data, but methods are not supported; 4. Summary: Use computed first to improve performance, and use methods when passing parameters, performing operations or avoiding cache, following the principle of "if you can use computed, you don't use methods".

First,checkiftheFnkeysettingisinterferingbytryingboththevolumekeyaloneandFn volumekey,thentoggleFnLockwithFn Escifavailable.2.EnterBIOS/UEFIduringbootandenablefunctionkeysordisableHotkeyModetoensurevolumekeysarerecognized.3.Updateorreinstallaudiodriv

This C single-linked example implements insert, traversal and delete operations. 1. Use insertAtBeginning to insert nodes in the head; 2. Use insertAtEnd to insert nodes in the tail; 3. Use deleteNode to delete nodes by value and return boolean results; 4. Use display method to traverse and print the linked list; 5. Free all node memory in the destructor to prevent leakage; the final program output verifies the correctness of these operations, fully demonstrating the basic management method of dynamic data structures.

TagDispatching uses type tags to select the optimal function overload during the compilation period to achieve efficient polymorphism. 1. Use std::iterator_traits to obtain the iterator category tag; 2. Define multiple do_advance overload functions, and process random_access_iterator_tag, bidrectional_iterator_tag and input_iterator_tag respectively; 3. The main function my_advance calls the corresponding version based on the derived tag type to ensure that there is no runtime overhead during the compile period decision; 4. This technology is adopted by standard libraries such as std::advance, and supports extended customization.

If it is iterating when deleting an element, you must avoid using a failed iterator. ①The correct way is to use it=vec.erase(it), and use the valid iterator returned by erase to continue traversing; ② The recommended "erase-remove" idiom for batch deletion: vec.erase(std::remove_if(vec.begin(),vec.end(), condition), vec.end()), which is safe and efficient; ③ You can use a reverse iterator to delete from back to front, the logic is clear, but you need to pay attention to the condition direction. Conclusion: Always update the iterator with the erase return value, prohibiting operations on the failed iterator, otherwise undefined behavior will result.

Use checked exceptions to indicate recovery errors, and unchecked exceptions to indicate programming errors; 2. After catching exceptions, they must be processed, recorded or re-throwed, and must not be ignored; 3. Throw exceptions as soon as possible when errors occur, and delay capture at the top of the call chain; 4. Provide clear context information when throwing exceptions to avoid vague descriptions; 5. Use try-with-resources to automatically manage resource closure to prevent resource leakage; 6. Avoid catching broad exceptions such as Exception or Throwable, and specific exception types should be captured; 7. Custom exceptions should contain semantic error information and context data; 8. Exceptions should not be used to control normal program flow to avoid performance losses; 9. Record exceptions

TheautokeywordinC deducesthetypeofavariablefromitsinitializer,makingcodecleanerandmoremaintainable.1.Itreducesverbosity,especiallywithcomplextypeslikeiterators.2.Itenhancesmaintainabilitybyautomaticallyadaptingtotypechanges.3.Itisnecessaryforunnamed

std::source_location is a class introduced by C 20 to obtain source code location information. 1. You can obtain file name, line number, function name and other information at compile time through std::source_location::current(); 2. It is often used for logging, debugging and error reporting; 3. It can automatically capture the call location in combination with macros; 4. Function_name() may return a mangled name, and it needs to be parsed with abi::__cxa_demangle to improve readability; 5. All information is determined at compile time, and the runtime overhead is extremely small, suitable for integration into logs or test frameworks to improve debugging efficiency.
