Java methods for protecting memory operations

Source: Internet
Author: User
Tags java throws

1, unlike C + +, in Java, there is no way to directly access memory by using the cast pointer type or by doing pointer operations. When working with objects in Java, you need to strictly adhere to the type rules. If there is a reference to a Mountain class object (similar to a C + + pointer), it can only be used as mountain. This reference cannot be cast to the lava type, not to the lava type, nor to the memory it points to as a lava object. As with pointer arithmetic in C + +, simply adding an offset to a reference is also forbidden. In Java, it is also possible to convert a reference to another type, but only if the object is indeed the new type. For example, if a mountain reference actually points to an instance of a volcano class (a special type of mountain), this mountain reference can be converted to a volcano reference. Because Java enforces strict type rules at runtime, it is simply not possible to manage memory directly in a way that could cause memory conflicts. As a result, there are no specific bugs in Java programs that often reduce the efficiency of C + + programmers.

2. Another way in which Java avoids inadvertently destroying memory is automatic garbage collection. Java programmers do not need to manually remove the useless objects, when the Java program stops a reference to an object, after a period of time, the garbage collector will automatically reclaim the memory occupied by this object. In C + +, you need to manually dispose of objects. If an object that is no longer being used is not released, it can cause a memory overflow.

3. The third way Java protects memory integrity at run time is array bounds checking. The operation of an array in C + + is actually a pointer operation, which leads to a potential memory conflict. Declare an array with a member in C + + and write to the 11th member (although this is the wrong usage, C + + does not limit this). In Java arrays are well-developed objects each time an array is used, Java checks the bounds of the array. When you create an array of 10 members in Java, and then try to write to the 11th member, Java throws an exception. Java will never allow array operations to go out of bounds, causing memory conflicts.

4. Java's example of ensuring program robustness is checking object references, and Java ensures that these references are not null values each time a reference is used. If empty, Java throws a null pointer exception.

Java methods for protecting memory operations

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.