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"};
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- [Java] Generate Getters and Setters in VS Code - Java
- Unsupported major.minor version 52.0 in java - Java
- Fix: KeyError: exception in Python - Python
- Mac: How to Merge Cells in Excel - MacOS
- Fix: zsh: command not found: aws (Mac/macOS) - AWS
- Terminal display next month Calendar - Linux
- Java Program: Random Number Generator - Java
- How to Check For Updates on Windows 11 (Step-by-Step) - Java