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}
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
- How to Schedule Mails in macOS Ventura - MacOS
- Java - Check if array contains the value - Java
- Check Bluetooth is turned on or off on Android device programmatically [Java Code] - Android
- [Solved] Error launching studio - Android-Studio
- How to install npm on Ubuntu - Linux
- Notepad++ select all above or below lines - NotepadPlusPlus
- Remove URL Forward Slash Before Single or Double quotes in php.ini - PHP
- Install Microsoft OneDrive on Mac - MacOS