Declare an array in java – How to Declare an Array in Java?

Declare an array in java: In this article we will see how to declare an array in Java Programming language..

How to Declare an Array in Java?

Declare array java: Before jumping to the array declaration directly, we will know something about array first so that it will be easy to understand array declaration. So, let’s start exploring the article by going through one by one concept.

Array in Java:

How to declare an array in java: Array is a data structure which stores a fixed size sequential collection of values of single data type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Some Points about Array in Java:

  • Array in Java is a data structure as it provides an organized way of storing, processing and retrieving elements in it and performing the required operations like insertion and deletion of data elements.
  • Array is a non primitive data type where we store similar types of data in a consecutive manner. It is not pre defined and it is declared by the programmer, so it is non primitive data type.
  • In Java Array is an Object.
  • When an array is created first time then all the element values are set to 0.

Types of Array:

There are two types of array

  1. One dimensional array
  2. Multi dimensional array (data stored in matrix form)

Declaring One dimensional Array in Java:

Approach-1

dataType[] arrayName;

Approach-2

dataType []arrayName;

Approach-2

 dataType arrayName[];

We can declare one dimensional array by using above 3 formats.

Where,

  • dataType: Refers to the type of the array, means the array will contain the elements of that type of only.
  • arrayName: Refers to name of the array, means reference name.
  • []: Refers to the symbol of one dimensional array block.

Example:

Example-1 (Declaring one dimensional integer array)

int result[]; (or)
int []result; (or)
int[] result;

Here in the above example int is the type of array and result is the reference variable(array name).

Example-2 (Declaring one dimensional String array)

String city[]; (or)
String []city; (or)
String[] city;

Here in the above example String is the type of array and city is the reference variable(array name).

Declaring Multi dimensional Array in Java:

Approach-1

dataType[][] arrayName;

Approach-2

dataType [][]arrayName;

Approach-3

 dataType arrayName[][];

Approach-4

dataType []arrayName[];

We can declare multi dimensional array by using above 4 formats.

Where,

  • dataType: Refers to the type of the array, means the array will contain the elements of that type of only.
  • arrayName: Refers to name of the array, means reference name.
  • [][]: Refers to the symbol of multi dimensional array block.

Example:

Example-1 (Declaring Multi dimensional integer array)

int result[][]; (or)
int [][]result; (or)
int[][] result; (or)
int []result[];

Here in the above example int is the type of array and result is the reference variable(array name).

Example-2 (Declaring Multi dimensional String array)

String city[][]; (or)
String [][]city; (or)
String[][] city; (or)
String []city[];

Here in the above example String is the type of array and city is the reference variable(array name).

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Articles: