Java Array

In this chapter you will learn:

  1. What are arrays in Java
  2. What is One-Dimensional Arrays
  3. How to allocate memory for array

Introduction to Arrays

A Java array is an ordered collection of primitives, object references, or other arrays. Java arrays are homogeneous: except as allowed by polymorphism, all elements of an array must be of the same type.

Each variable is referenced by array name and its index. Arrays may have one or more dimensions.

One-Dimensional Arrays

A one-dimensional array is a list of similar-typed variables. The general form of a one-dimensional array declaration is:

type var-name[ ];

type declares the array type. type also determines the data type of each array element. The following declares an array named days with the type "array of int":

int days[];

days is an array variable. The value of days is set to null.

Allocate memory for array

You allocate memory using new and assign it to array variables. new is a special operator that allocates memory. The general form is:

arrayVar = new type[size];

There are three parts in the code above.

  • type specifies the type of data being allocated.
  • size specifies the number of elements.
  • arrayVar is the array variable.

The following two statements first create an int type array variable and then allocate memory for it to store 12 int type values.

int days[]; 
days = new int[12];

days refers to an array of 12 integers. All elements in the array is initialized to zero.

Next chapter...

What you will learn in the next chapter:

  1. How to declare an array type and allocate memory
  2. What are the default value for different type of array
  3. What are Alternative Array Declaration Syntax
  4. How to initialize an array during the declaration
Home » Java Tutorial » Array
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array Search
Array sort
Array to List
Convert array to Set
Array fill value
Array to String
Array element reverse
Array element delete
Array shuffle
Array element append
Array min / max value
Sub array search
Get Sub array
Array dimension reflection
Array clone