In this tutorial, we will take a look at ways in which how we can declare and initialize an Array in Java Programming Language,
Part 1: Declaring an Array
Syntax:data-type[ ] variableName;
Example:int[] myIntArray;
Declaring an Array in Java is quite similar to declaring a variable, the only difference you will see is that the datatype has rectangular brackets [] around it, wherever you see these brackets around a datatype in Java it is to signify that its an Array,
More Examples:// Array of Primitive Type
int[] evenNumbers;
double[] measurements;
boolean[] validity;
// Array of Object Type
String[] userNames;
Employee[] employeeArr;
⛏️ You can also declare an Array as int evenNumbers[] where the brackets are placed at the end of the variable name and not the datatype, but this is not the preferred way!
Arrays are Objects in Java, so if you try to use the array Object that's defined with unitized to null, you will get NullPointerException.
public static void main(String[] args) {
int[] evenNumbers = null;
System.out.println(evenNumbers.length);
}
⛔️ Exception in thread "main" java.lang.NullPointerException
at Example.main(Example.java:5)
Part 2: Initilizing an Array
When you just declare an Array there is no memory allocated, so if you try to use it you will get a Compile Time Error,
java: variable evenNumbers might not have been initialized
As Array's in Java are of type Object, you can initialize them using a new keyword followed by the data-type and size of the Array value between the braces,
int[] evenNumbers = new int[10];
double[] measurements = new double[10];
boolean[] validity = new boolean[10];
String[] userNames = new String[10];
If you do not provide the size you will get an compilation time error saying "array dimension missing"
public static void main(String[] args) {
String[] cha = new String[];
}
⛔️ java: array dimension missing

When you print the variables now, you will see that you get [ which implies an Array, followed by I -> Integer Array, D -> Double Type Array, Z -> Boolean Type Array, followed by @ and the Object hash,
System.out.println(evenNumbers);
System.out.println(measurements);
System.out.println(validity);
System.out.println(userNames);
[I@6bdf28bb
[D@6b71769e
[Z@2752f6e2
[Ljava.lang.String;@e580929
When you initialize an Array the elements of the array are initialized to the default value by the JVM,
- integer Array elements default value: 0
- double Array elements default value: 0.0
- boolean Array elements default value:
- char Array elements default value: NUL
- String/User Defined Array elements default value: null
int[] myArray = {10,20,30,40,50}
- 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
- How to reset AirPods or AirPods Pro using iPhone/iPad or iPod - iOS
- Installing Native Chrome Browser App on M1 Mac Device - Chrome
- How to update VIM version on a Mac - vi
- Change CKEditor Table Properties default width - CKEditor
- 12 August - International Youth Day celebrated worldwide - News
- Check macOS free disk space using Terminal command - MacOS
- pwd Command - Print Working Directory - Linux
- How to get list of all Java versions installed on macOS - Java