India: +91-4446 311 234 US: +1-6502 652 492 Whatsapp: +91-7530 088 009
Upto 20% Scholarship On Live Online Classes

An array is a data structure in Java that is used to store a collection of elements. These elements can be of any data type, such as integers, strings, or objects. The size of an array is fixed when it is created, and all elements within the array must be of the same data type.

java course with placement in coimbatore will help students to understand the object-oriented programming in a better way. They will get an understanding of how to write a program using Java. The course also includes a session on how to debug programs written in Java.

Creating an array in Java is a simple process. The syntax for creating an array is as follows:

Creating an array in Java

For example, if you wanted to create an array of integers with a size of 5, you would use the following code:


example-of-creating-array

You can also initialize an array when it is created by specifying the values of the elements in the array. The syntax for doing this is as follows:

intilized an array

For example, if you wanted to create an array of strings with the values “apple”, “banana”, and “orange”, you would use the following code:

When you create an array, Java assigns a default value to each element in the array. For example, if you create an array of integers, each element in the array will be assigned the default value of 0. If you create an array of strings, each element in the array will be assigned the default value of null.

Once an array is created, you can access and modify the elements within the array using the array name and the index of the element you want to access. The index of the first element in an array is 0, and the index of the last element in an array is one less than the size of the array.

For example, if you wanted to access the second element in the “fruits” array that was created earlier, you would use the following code:

access second element in array

You can also use the index to modify the value of an element in an array. For example, if you wanted to change the value of the second element in the “fruits” array to “pear”, you would use the following code:

index an array

Java provides several methods for working with arrays, such as the length method, which returns the number of elements in an array, and the sort method, which sorts the elements in an array in ascending order.

Another important feature of arrays in Java is the ability to use a loop to iterate through the elements in an array. The most commonly used loop for working with arrays is the for loop. The syntax for using a for loop to iterate through an array is as follows:

loop iteration in array

For example, if you wanted to print out all the elements in the “fruits” array created earlier, you would use the following code:

example of loop iteration array

In this example, the for loop starts at 0, which is the index of the first element in the array. The loop continues until the value of i is less than the length of the array