Java Array Memo

Source: Internet
Author: User

Preface

When I recently used the ACM topic in Java, I often forget how the array implements static initialization, so here is the common sense used by the Java array.

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 judged out of bounds, and invalid indexes throw arrayindexoutofboundexception.
    3. An array of contiguous memory spaces stored in the Java heap.
    4. An array is a fixed-length data structure, and once the length of the array is declared, subsequent operations cannot change the length of the array.
    5. Arrays are very fast data structures, and if you can already 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 you can see from the code above, 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 you can see from the code above, dynamic initialization is just specifying the length of the array, and the system assigns the initial values to the arrays.

Different types, the initial value of the system allocation is not the same.

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.