Java Programming for Beginners: From Introduction To Your First Java Project

Concepts: Explain how to create and access elements in a basic one-dimensional array. Cover the fundamental methods of the String class for manipulating text.

Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array with square brackets [ ] :

String[] colors;

To insert values to it, you can place the values in a comma-separated list, inside curly braces { }:

String[] colors= {“Red”, “Blue”, “Green”, “Orange”};

System.out.println(colors[2]); // Outputs Green

// Array of integers

int[] myInt = {100, 200, 300, 400};

Java Strings
Strings are used for storing text. A String variable contains a collection of characters surrounded by double quotes (“”):

Create a variable of type String and assign it a value:

String alphabets= “ABCDE”;

String Length

A String in Java is actually an object, you can find the length of a string with the length() method:

System.out.println(“The length of the alphabets string is: ” + alphabets.length());

// Outputs 5

Code example:

public class ArrayStringExample {
  public static void main(String[] args) {
   String[] fruits = {“Apple”, “Banana”, “Cherry”};
   System.out.println(“The second fruit is: ” + fruits[1]);

   String message = “Hello, Java!”;
   System.out.println(“The length of the message is: ” + message.length());
   }
}

 

Quiz: Answer questions on array indexing, string length, and other basic string methods.