Java Arrays

Arrays are used to store multiple values under a single variable.

Let me explain it clearly:

For example if you have to declare three different objects with the same name .

Then in general scenario we try to declare three different lines with the same name and objectives different to the variable.

In order to avoid the time for coding arrays can be used which will reduce the lines of coding and also the time to code.

Now let me take you through the coding which will give you a clear picture on what is discussed upto now.

public class Main{
public static void main(String [] args){
String[] fruit = {"Apple", "Mango", "Grapes"};
System.out.println(fruit[0]);
}
}

From the above example it is clear that we are declaring three variables under a single item and calling them when it is required.

Changing element of an array:

public class Main{
public static void main(String[] args){
String[] fruit ={"Apple","Mango","Grapes"};
fruit[1] = "Strawberry";
System.out.println(fruit[1]);
}
}

Calculating Length of array:

public class Main{
public static void main(String[] args){
String[] fruit ={"Apple","Mango","Grapes"};
System.out.println(fruit.length);
}
}

Scroll to top