java array creation

java array creation

In Java, you can create an array by using the new operator and specifying the element type and the length of the array.

Here is an example of how to create an array of integers in Java:

int[] numbers = new int[5];
Source‮ual.www:‬tturi.com

This creates an array of integers with a length of 5, and assigns it to the numbers variable. The array is initialized with the default value for the element type (0 for integers).

You can also create an array and initialize it with a set of values using an array literal:

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

This creates an array of integers with a length of 5, and assigns it to the numbers variable. The array is initialized with the values 1, 2, 3, 4, and 5.

You can also create an array of objects by specifying the element type and using the new operator:

String[] strings = new String[5];

This creates an array of strings with a length of 5, and assigns it to the strings variable. The array is initialized with the default value for the element type (null for strings).

Note that the length of an array in Java is fixed and cannot be changed once the array is created. To change the size of an array, you need to create a new array and copy the elements from the old array to the new array.

Created Time:2017-11-03 00:14:43  Author:lautturi