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"};
Have Questions? Post them here!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Force convert HTML text input to upper case - Html
- [Solution] Alpine Docker apt-get: not found - Docker
- [Solution] macOS could not be installed on your computer OSInstall.mpkg appears to be missing or damaged - MacOS
- 86 Gmail keyboard shortcuts that you may find Advantageous - Google
- How to Disable Mac Terminal Bell Sound - MacOS
- Save a file as CSV Format TextEdit on Mac - MacOS
- Install Gradle VS Code for Java Projects - Gradle
- How to flatten a nested list in Python - Python