In Java, an array is the fundamental data structure that allows you to store multiple values of the same data type in a single variable.
Syntax: Declaring an Array
dataType[] arrayName = new dataType[length];
| datatype | : | The data type of the elements to be stored in the array. |
| [ ] | : | It indicates that a data type is used to declare an array. |
| arrayName | : | It is the identifier, the name that you define for the array you create. |
| new | : | To create an instance of the array. |
| dataType[length] | : | To declare the size/length of the array as type int (max value can be Integer.MAX_VALUE) |
Examples of declaring Arrays in Java
int[] intArray = new int[5]; // an array of type int of size 5
String[] namesArray = new String[10]; // an array of type String of size 10
Object[] objArr = new Object[100]; // an array of type Object of size 100

Let's take a look at a few examples with multi-dimensional arrays declaration,
int[][] intArray = new int[5][10];
String[][] namesArray = new String[5][2];
Object[][][] objArr = new Object[3][2][2];
In the first example we declared a 2D array named intArray that holds integer values. The new int[5][10] statement creates a 2D array with 5 rows and 10 columns, which means it can hold up to 50 integer values.
Let's see this as a diagram how a 2D array looks like.

Similary, you we can have a 3D array, our example Object[3][2][2] can be represented as follows,

objArr:
[
[
[objArr[0][0][0], objArr[0][0][1]],
[objArr[0][1][0], objArr[0][1][1]]
],
[
[objArr[1][0][0], objArr[1][0][1]],
[objArr[1][1][0], objArr[1][1][1]]
],
[
[objArr[2][0][0], objArr[2][0][1]],
[objArr[2][1][0], objArr[2][1][1]]
]
]
Access modifiers and Specifiers and Arrays
- public: To allow the array to be accessed from anywhere in the code.
- private: To restrict the array to be accessed only within the same class.
- protected: To allow the array to be accessed within the same class, subclasses, and classes in the same package.
- default: To allow the array to be accessed within the same package only.
- final: To make the array constant and its value cannot be changed once initialized.
- static: To make the array belong to the class, rather than to an instance of the class.
- volatile: To indicate that the value of the array may change at any time due to concurrent modifications from multiple threads.
- transient: Indicates that the array should not be included in the serialization process.
- synchronized: To allow only one thread at a time to access the array.
In Java, access modifiers and specifiers can be applied to array variables, just like any other variable.
Access Modifiers:
Access Specifiers:
Most important points to remember when declaring Arrays in Java
- In Java Arrays are indexed from 0 and not 1.
- You can declared an Array as instance, local or static variables, or method parameters.
- The length of an array in Java is fixed at the time of creation and cannot be changed later.
- You can create a new array with a different size and copy the elements from the old array if you want to change the size of an array.
- All elements are initialized to a default value of the datatype as you create it.
- The default values of the Object type are set to null.
- The default value for the boolean type is false.
Extra tip:
You can declare and initialize an array in one step as follows,
int[] intArray = {1, 2, 3, 4, 5};
String[] namesArray = {"Sam", "Dean", "Charlie","Bobby","Castiel"};
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!