Header Ads Widget

Arrays

Arrays

Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.

Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st index and so on.

Declaring Array Variables

Syntax

dataType[] arrayRefVar;   // preferred way.

or

dataType arrayRefVar[];  // works but not preferred way.

Creating Arrays

You can create an array by using the new operator with the following syntax −

Syntax

arrayRefVar = new dataType[arraySize];

The above statement does two things −

  • It creates an array using new dataType[arraySize].
  • It assigns the reference of the newly created array to the variable arrayRefVar.

Post a Comment

0 Comments