Java Array Summary

Source: Internet
Author: User

---restore content starts---

1. Overview

Since Java is a typical static language, the arrays in Java are also static, and Java array variables are reference-type variables, (references in Ps:java are somewhat like pointers in C + + and C) when we use Java arrays, there are 2 main parts of the operation , respectively, is to define a reference variable (equivalent to defining a pointer (null) in c++/c) and instantiating an array object.

In the Java memory allocation mechanism, the definition of local variables and reference variables is the application of a block of space in the corresponding method stack memory, whereas the object (class object, array object) to which the reference variable refers is the application of memory in the heap memory, the stack memory is easy to access (direct access), Heap memory is not easy to access (requires reference variables to access heap memory).

2. Initialization of the array initialization Java array is divided into 2 types, namely static initialization and dynamic initialization.

Static initialization:

The array object is initialized at the same time it is defined, but it is not possible to assign a value to the group length .

Example: int []a=new int[]{1,2,3,4};

String []b=new string[]{"Kylin", "Shanshan"};

Dynamic initialization:

An array is defined with only an assignment of length, and no specific array object is initialized (initialized by default by the system)

For example: int []c=new int[4];

String []d=new string[3];

The value of the instantiated object for the C array, respectively, is c[0]==0 c[1]==0 c[2]==0 c[3]==0

The values of the D array object are NULL NULL NULL, respectively;

Why is that? This is because Java has a default initialization mechanism, which describes the initial values that Java allocates for an array element:

Integral type (byte,short,int,double) default initial value is 0

Floating-point type (double,float) default initial value 0.0

Boolean type (Boolean) default initial value False

Reference type (array, class, interface, String) default initial value is NULL

Character type (char) default initial value ' \u0000 '

3, two-dimensional array (multidimensional)

When initializing multiple arrays, you can initialize the leftmost dimension first, and Java allows the multidimensional array to be treated as a one-dimensional array, with no nonsense to say, on the example.

int[][]a=New int[3][]; //The array name of a two-dimensional array in Java represents the first address of a one-dimensional array (as with c++/c)System.out.println (a.length);  for(inti=0;i<a.length;i++)        {            //The element of one-dimensional array is then pointed to a new one-dimensional array, and a two-dimensional (multidimensional) array is formed; Here you can point to many new arrays of different lengthsa[i]=New int[4]; }         for(inti=0;i<4;i++) {System.out.print (a[0][i]+ ""); }

The results of the operation are as follows:

  

   

    

---restore content ends---

Java Array Summary

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.