Arrays in Java

Source: Internet
Author: User

Arrays are supported in almost all programming languages. Using arrays in C and C + + is very dangerous because those arrays are just blocks of memory. If a program accesses an array other than its own memory block, or uses memory (which is a general programming error) before initialization, it can have unpredictable consequences (note ②).

②: in C + +, try not to use arrays, swapping the more secure containers in the Standard Template Library (Standard templatelibrary).

One of the main design goals of Java is security. So many of the problems that plague programmers in C and C + + are not repeated in Java. A Java can be guaranteed to be initialized and cannot be accessed outside its scope. Because the system automatically checks the scope, there must be a cost: For each array, as well as the checksum of the index during run time, a small amount of memory overhead will be incurred. But the result is higher security and higher productivity. It is worthwhile to pay a little price for it.
When you create an array of objects, you actually create an array of handles. And each handle is automatically initialized to a special value with its own keyword: null (NULL). Once Java sees NULL, it knows that the handle does not point to an object. You must assign an object to each handle before you can use it formally. If you try to use a handle that is still null, you will report the problem at run time. As a result, typical array errors are avoided in Java.

You can also create an array of main types. Similarly, the compiler can guarantee initialization of it, because the memory of that array is divided into 0.
The array question will be discussed in more detail in a later section.

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.