Arrays in Java

Arrays in Java

We had earlier learned about storing data into some variable.  Every time we need to store some data, we were creating some variables. Let’s consider an example – we need to store the names of some companies. One way to do this is to create a lot of variables to store the names of companies. You may have a look at the below program –

public class Main {
public static void main(String[] args) {
String company1 = “Facebook”;
String company2 = “Google”;
String company3 = “Tesla”;
}
}

The above program shows that we can comfortably create variables for storing different names of companies. But now let’s see, if there are 10, or 100 such companies? Is it still convenient to create this many variables for storing the names of companies? Again the thing is that if you ever want to access those values, every variable has a different name. So accessing every variable is also not convenient now.

What if I say that we have something better? We have something with help of which we can store a collection of data of similar data types. It is also called Array. So, we can say that the array is a collection of data of similar data types. This means that if we need to store 10, or 100s of integer numbers let’s say, then we can comfortably create an integer array to store those 10 or 100 elements.

So we are going to now explore much about arrays. This was just a brief introduction about what are arrays, why we use them, and so on. Now let’s move further to know about how can we declare an array, or in other words, how can we create an array of some particular data type.