Previous Next

Java Arrays

Java arrays are employed to store more than one value in a variable.

Syntax:

String[] companies = {"Goggle", "IBM", "TCS"};

Accessing Elements of an Array

Array elements can also be accessed with indexing. In Java, the indexing begins with 0.

Example:

public class Main {
  public static void main(String[] args) {
    String[] companies = {"Goggle", "IBM", "TCS"};
    System.out.println(companies[1]);
  }
}

Output:

IBM

Change an Array Element

To modify the value of a given element, utilize the index number.

Example:

public class Main {
  public static void main(String[] args){
    String[] companies = {"Goggle", "IBM", "Microsoft"};
    companies[2] = "TCS";
    System.out.println(companies[2]);
  }
}

Output:

TCS
Previous Next