Declare and initialize an array named grades
that stores the following grades: 92, 85, 77, 98, 88, 79, 91.
Write the code to declare and initialize the array grades
using the appropriate syntax in Java.
To declare and initialize an array in Java, you need to specify the type of the elements and the size of the array.
// Declare and initialize the array
int[] grades = {92, 85, 77, 98, 88, 79, 91};
In this example, int[]
declares an integer array. The variable name grades
is assigned to the array, and the values are provided inside curly braces { }
separated by commas.
The grades
array will have a size of 7 (the number of elements provided), and the elements will be stored in the order specified.
Note that in Java, arrays are zero-indexed, meaning the first element of the array is accessed using the index 0
, the second element with index 1
, and so on.