Array in java

Source: Internet
Author: User

Array in java

Array definition: a set of data of the same type, such as int, String, or a class. You can simply regard an array as a container, however, the data stored in this container is of the same type,


Array format:

Element type [] array name = new element type [number of elements or array length]

Instance code:

int[] a = new int[3];System.out.println(a[2]);

There is also a format: specifically, the elements in the array are written.
int[] a = new int[]{1,2,3,4,5,6};
You can also write this statement:

int[] a = {1,2,3,4,5,6};



The int array is defined. The length of the array is 3. When I print the lower three data, it is 0. I'm curious. I will think about it later, because each int value in the int array is initialized with the default value, it is 0. If we want to assign a value to it, do this.

A [0] = 2;

A [1] = 4;

A [2] = 3;

This is to assign values to each of the arrays,

So how does it correspond in the memory? In java JVM, there are usually stack memory and heap memory. So what data does each of them store?

Stack memory:

1: stores local variables. Once the local variables are in the final scope, the variables are automatically released in the stack memory, saving my memory. This is the internal optimization done by the JVM, therefore, stack memory updates are fast.

2: The local variable does not have a default initialization value. You must give it a default value.

Heap memory:

1: stores arrays, sets, and objects

2: all new items are in heap memory.

3: The value in heap memory has a default initialization value, which varies depending on the type of initialization value.

4: when an object is null in memory, the garbage collector in java will automatically recycle the non-pointing object at a certain time, thus saving memory space.







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.