Java Array Memo

Source: Internet
Author: User

Preface

The recent use of Java in the topic of ACM, often forget how to implement static initialization of arrays, so here is the common sense of Java array use.

Java Array Common sense

An array is an object in Java, and an array instance needs to be created with the new operator.

Common sense Knowledge:

    1. The size of the array can be obtained by Array.Length.
    2. The array index starts with 0 and the negative index is invalid in Java. Also, unlike C-language arrays, arrays in Java are inferred out of bounds. An invalid index throws arrayindexoutofboundexception.
    3. An array of contiguous memory spaces stored in the Java heap.

    4. An array is a fixed-length data structure. Once the length of the array is declared. The subsequent operation can no longer change the length of the array.

    5. An array is a very fast data structure. Assuming you have been able to determine how much data you have, you should use arrays instead of data structures such as ArrayList.
initialization of Java arrays

The initialization of Java arrays is divided into dynamic initialization and static initialization.

As an example of an array of type int, the static initialization and dynamic initialization are described separately.

Static Initialization
intnewint[] {123456};

As can be seen from the above code, static initialization is the initial value of each element of the specified array, and the system determines the length of the array.

Dynamic Initialization
intnewint[6];

As can be seen from the code above, dynamic initialization simply specifies the length of the array, and the system assigns the initial values to the arrays.

Different types. The initial value of the system assignment is also different.

type Initial value
Integer type (byte,short,int,long) 0
Floating-point type (float,double) 0.0
Character type (char) ' \u0000 ' (representing spaces)
Boolean Type (Boolean) False
Reference type (class, interface, array) Null

Java Array Memo

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.