How do I declare and initialize an array in Java?

There are several ways to declare and initialize an array in Java.

One way is to use the new operator and specify the size of the array, like this:

int[] array = new int[5];

This creates an array of integers with a size of 5, and initializes all of the elements to their default values (0 for integers).

You can also use the new operator and specify the elements of the array, like this:

int[] array = new int[] {1, 2, 3, 4, 5};

This creates an array of integers with a size of 5, and initializes the elements to the specified values.

You can also use the shortcut syntax to declare and initialize an array, like this:

int[] array = {1, 2, 3, 4, 5};

This creates an array of integers with a size of 5, and initializes the elements to the specified values.

Note that the size of the array is not specified in the shortcut syntax. The size of the array is determined by the number of elements that are initialized.