Example of Array Declaration and Initialization

Single Choice
Views 163

Answer:

Declaring and then Initializing:


int[] arr;       // Declares an array of integers
arr = new int[5]; // Initializes the array with 5 elements

Declaration and Initialization Together:


int[] arr = new int[5]; // Declares and initializes an array of 5 integers

Initializing with Values: You can also declare and initialize the array with values at the same time.


int[] arr = {1, 2, 3, 4, 5}; // Declares, initializes, and assigns values

Multi-dimensional Array Declaration: For 2D arrays (or more), the syntax is:


int[][] matrix = new int[3][4]; // Declares a 2D array with 3 rows and 4 columns

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.