Java arrays and strings

An array has a fixed length and holds elements of one type, indexed from 0. Reading outside the bounds throws ArrayIndexOutOfBoundsException. A String is an immutable sequence of characters, so its methods return new strings. length, substring, indexOf, split, and String.join cover many everyday tasks.

Java
class Main {
  public static void main(String[] args) {
    int[] scores = {82, 91, 76};
    int sum = 0;
    for (int score : scores) {
      sum += score;
    }
    System.out.println(scores.length + " scores, total " + sum);

    String text = "Programonic";
    System.out.println(text.length() + " " + text.substring(0, 7) + " " + text.indexOf('m'));

    String[] parts = "a,b,,c".split(",");
    System.out.println(parts.length + " " + String.join("-", parts));

    String first = new String("hi");
    String second = "hi";
    System.out.println((first == second) + " " + first.equals(second));

    System.out.println(new StringBuilder(text).reverse());
  }
}
Read the full lesson