How to declare an Array Variables in Java?


You can declare an array just like a variable −

int myArray[];

You can create an array just like an object using the new keyword −

myArray = new int[5];

You can initialize the array by assigning values to all the elements one by one using the index −

myArray [0] = 101;
myArray [1] = 102;

You can access the array element using the index values −

System.out.println("The first element of the array is: " + myArray [0]);
System.out.println("The first element of the array is: " + myArray [1]);
Alternatively, you can create and initialize an array using the flower braces ({ }):
Int [] myArray = {10, 20, 30, 40, 50}

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 19-Feb-2020

271 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements